- JavaScript 的表单判断登录的基本函数
function login(){
var form = document.form1;
if(form.username.value == ""){
alert("用户名不可为空");
form.username.focus();
return;
}
if(form.pswd.value==""){
alert("密码不能为空");
form.pswd.focus();
}
//发送到HttpServlet中进行验证
form.action="<%=path%>/servlet/LoginAction";
//表单提交
form.submit();
}
- JavaScript直接跳转页面
window.location.href="链接";
- JSP中获取网页相对路径,应用的根路径
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!--
request.getSchema()可以返回当前页面使用的协议
request.getServerName()可以返回当前页面所在的服务器的名字
request.getServerPort()可以返回当前页面所在的服务器使用的端口
request.getContextPath()可以返回当前页面所在的应用的名字
--!>
- FORM表单提交POST 除了提交文本格式的数据还可以提交文件格式的数据
<form name="form1" action="" method="post" enctype="multipart/form-data">
<input name="username" type="text" />
<input name="image" type="file" class="text2" size="60">
</form>
Tip:标准的MVC框架
服务层、控制层、数据访问层
UUID通用唯一识别码 (Universally Unique Identifier)
public class UUIDUtils(){
public UUIDUtils(){
}
public static string getUUID(){
UUID uuid = UUID.randomUUID();
return uuid.ToString().replaceAll("-","").subString(0,6);
/*将UUID的横杆全部替换掉 并且去UUID值的前7位,用于程序员维护数据库的主键值,标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12)*/
}
}
- Servlet 中对网页上传文件进行处理 使用的导入包来自:Apache commons FileUpload 的公共类
DiskFileItemFactory(采用工厂模式)
ServletFileUpload(文件上传类)
//设置允许的最大单个上传文件的大小
servletFileUpload.setFileSizeMax();
//设置一个完整请求(表单内容数据)的最大长度
servletFileUpload.setSizeMax();
//解析request的请求,获取表单中所有的input标签,返回的是一个List<FileItem>
servletFileUpload.parseRequest(request);
List<FileItem> list = servletFileUpload.parseRequest(request);
//input 表单字段遍历
for(FileItem fileItem:list);
fileItem.isFormField();// input字段是否为一个简单的字段,返回值为true/false
fileItem.getFiledName();// 获取<input name = "textInput" type="text"/>中的 name 的值
fileItem.getString("utf-8");// 获取input字段中的value值
//若不是简单的表单字段 则为 <input name="fileInput" type="file"/>
fileItem.getName();//获得上传文件名称
fileItem.getRealPath("url");//虚拟目录映射为实际目录
**文件上传
File real_path = new File(upload_path + "/" + image);// io流
fileItem.write(real_path);// 写到服务器中
response.sendRedirect(request.getContextPath()+"/index.jsp");//response 响应跳转
Servlet部分代码实现,源自老罗
private void addProduct(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 表单内含有除了文本以外格式的文件要上传
String path = request.getContextPath();
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
// 构建一个文件上传类
ServletFileUpload servletFileUpload = new ServletFileUpload(
diskFileItemFactory);
servletFileUpload.setFileSizeMax(3 * 1024 * 1024);
servletFileUpload.setSizeMax(6 * 1024 * 1024);
List<FileItem> list = null;
List<Object> params = new ArrayList<Object>();
params.add(UUIDTools.getUUID());
try {
// 解析request的请求 获取表单中所有的Input的标签
list = servletFileUpload.parseRequest(request);
for (FileItem fileItem : list) {
if (fileItem.isFormField()) {
if (fileItem.getFieldName().equals("proname")) {
params.add(fileItem.getString("utf-8"));
}
if (fileItem.getFieldName().equals("proprice")) {
params.add(fileItem.getString("utf-8"));
}
if (fileItem.getFieldName().equals("proaddress")) {
params.add(fileItem.getString("utf-8"));
}
} else {
try {
String image = fileItem.getName();// 获得文件的名称
params.add(image);
String upload_path = request.getRealPath("/upload");
System.out.println("------>" + upload_path);
File real_path = new File(upload_path + "/" + image);
fileItem.write(real_path);
//把数据插入到数据库
boolean flag = service.addProduct(params);
if(flag){
response.sendRedirect(path+"/product/"+"2_1_5.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
- 数据库批处理(案例来自老罗)
/* 传入的String[] sql 数组是进行修改的sql语句*/
public boolean delByBatch(String[] sql){
boolean flag = false;
Statement stmt = connection.createStatement();
if(sql != null){
for(int i = 0;i<sql.length;i++){
//循环添加sql语句,预批处理.
stmt.addBatch(sql[i]);
}
}
//返回影响多少行
int[] count = stmt.executeBatch();
if(count != null){
flag = true;
}
return flag;
}
- 分页类
resultCount 已知数据库查询总条数
eachPageCount 用户自定义设置每页显示条数
currentPage 当前页的数据记录
1、通过像数据库里查询语句从而达到分页的效果
limit ?,?
例如:
第一页通过查询 select * from db limit 0,5;
显示出5条记录
第二页则通过查询 select * from db limit 5,10;
显示第二页的5条记录
通过代码
设置开始 (currentPage - 1) * eachPageCount;
设置结束 return eachPageCount;
2、通过获得数据库总条数来进行分页代码实现
public int getRealPage(int resultCount){
int realPage = 0;
//能整除的情况下
realPage = resultCount / eachPageCount;
int other = resultCount % eachPageCount;
if(other != 0){
//总条数不能被每页的数量给整除,则相应的会多出一页
result++;
}
return resultCount == 0 ? 1 : realPage;
}
3、解决上一页/下一页溢出问题
public void setCurrentPage(int currentPage){
int temp = currentPage <= 0 ? 1 : currentPage;
temp = currentPage > getPageCount() ? getPageCount(): temp;
this.currentPage = temp;
}
4、Jsp中的form表单中嵌入javascript代码进行提交操作
<a href="javascript:next()">下一页</a>
<script type="text/javascript">
function next(){
var form = document.form1;
form.action="<%=path%>/servlet/ProductAction?action_flag=list&pageNum=<%=divide.getCurrentPage()+1%>";
form.submit();
}
</script>
分页类代码(老罗)
package product.util;
public class DivideUtil {
private int pageSize;// 一页显示的条数
private int recordSize;// 总记录条数
private int currentPage;// 当前页
public DivideUtil(int pageSize, int recordSize, int currentPage) {
this.pageSize = pageSize;
this.recordSize = recordSize;
setCurrentPage(currentPage);
}
public DivideUtil(int pageSize, int recordSize) {
this(pageSize, recordSize, 1);
}
public int getPageCount() {
int result = 0;
result = recordSize / pageSize;
int mod = recordSize % pageSize;
if (mod != 0) {
result++;
}
return recordSize == 0 ? 1 : result;
}
//开始记录条数
public int getFromIndex(){
return (currentPage-1) * pageSize;
}
public int getTOIndex(){
return pageSize;
}
public int getCurrentPage(){
return currentPage;
}
public void setCurrentPage(int currentPage){
int validPage = currentPage <= 0 ?1 : currentPage;
validPage = validPage > getPageCount()?getPageCount():validPage;
this.currentPage = validPage;
}
public int getPageSize(){
return pageSize;
}
public void setPageSize(int pageSize){
this.pageSize = pageSize;
}
public int getRecordSize() {
return recordSize;
}
public void setRecordSize(int recordSize) {
this.recordSize = recordSize;
}
}