上传,主要第三方的jar
第一方请求必须为post
enctype=”multipart/form-data” 支持多种类型:text 图片,加在form标签中
application随着web的创建而被实例化出来,这个内置对象在整个项目中只实例化一次
pageContext内置对象,只能获取当前页的数据
request 内置对象,作用域局限于服务器里的内部请求(url地址没有发生改变)
session对象 只要在一次会话里都能取到值
application 对象的作用域最大,只要服务器不关闭都能获取到
<!-- 总结访问量 -->
<%
//获取application里的值
Integer count = (Integer) application.getAttribute("count");
//判断count是否为第一次
if (count == null) {
count = 1;
} else {
count++;
}
//把对象存入到application里
application.setAttribute("count", count);
%>
<h1>
点击量:<%=application.getAttribute("count")%></h1>
doUploadAdd.jsp 逻辑处理
<%@page import="com.offcn.dao.StudentDao"%>
<%@page import="com.offcn.dao.impl.StudentDaoImpl"%>
<%@page import="java.io.File"%>
<%@page import="com.offcn.entity.Student"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
/* 设置编码格式 */
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
/* 是否支持上传 */
boolean flag = ServletFileUpload.isMultipartContent(request);
/* 得到服务器上传的路径 (上传到服务器的根目录)*/
String fileUploadPath = request.getSession().getServletContext()
.getRealPath("upload/");
File file1 = new File(fileUploadPath); //把路径转换成文件
if (!file1.exists()) {//判断文件是否存在,不存在就创建这个文件
file1.mkdir();
}
//实例化学生对象
Student student = new Student();
//标记是的上传成功
boolean uploadFlag = false;
//支持上传
if (flag) {
FileItemFactory factory = new DiskFileItemFactory();
//上传控件里的核心对象
ServletFileUpload servletFileUpload = new ServletFileUpload(
factory);
List<FileItem> list = servletFileUpload.parseRequest(request);
/* 把集合转换成迭代器 */
Iterator<FileItem> iterator = list.iterator();
while (iterator.hasNext()) {
/* 得到每行的具体元素 */
FileItem fileItem = iterator.next();
/* 判断input标签的类型是否为text类型 */
if (fileItem.isFormField()) {
String fieldName = fileItem.getFieldName();
if (fieldName.equals("sid")) {
student.setSid(Integer.parseInt(fileItem
.getString()));
} else if (fieldName.equals("sname")) {
student.setSname(fileItem.getString());
} else if (fieldName.equals("sproject")) {
student.setSproject(fileItem.getString());
} else if (fieldName.equals("screateDate")) {
student.setScreateDate(fileItem.getString());
}
} else {
//拿到非text类型的值,其值为传入的文件的对象
String fileName = fileItem.getName();
/* 判断是否为空 */
if (fileName != null && !fileName.equals("")) {
/* 把其转换为一个文件对象 */
File file = new File(fileName);
/* 把最后保存的文件路径创建成一个文件对象 */
File saveFilePath = new File(fileUploadPath,
file.getName());//fileName,file.getName()是同一个结果
fileItem.write(saveFilePath);
/* 把图片路径保存在对象 */
student.setSpic(file.getName());
uploadFlag = true;
}
}
}
}
if (uploadFlag) {
StudentDao studentDao = new StudentDaoImpl();
int num = studentDao.insertStudent(student);
if (num > 0) {
response.sendRedirect("index.jsp");
} else {
request.getRequestDispatcher("goAdd.jsp").forward(request,
response);
}
}
%>
index.jsp 显示
<%@page import="com.offcn.utils.PageUtils"%>
<%@page import="com.offcn.entity.Student"%>
<%@page import="com.offcn.dao.StudentDao"%>
<%@page import="com.offcn.dao.impl.StudentDaoImpl"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%-- 加入下行后就能使用<c:forEach></c:forEach> --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'showStudent.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<!-- 分页显示所有学生信息页面 -->
<body>
<!-- 总结访问量 -->
<%
//获取application里的值
Integer count = (Integer) application.getAttribute("count");
//判断count是否为第一次
if (count == null) {
count = 1;
} else {
count++;
}
//把对象存入到application里
application.setAttribute("count", count);
%>
<h1>
点击量:<%=application.getAttribute("count")%></h1>
<p>
欢迎登入:<%=session.getAttribute("name")%></p>
<h1>
<a href="goAdd.jsp">增加学生信息</a>
</h1>
<form action="showSearch.jsp" method="get">
<p>
请输入学生姓名<input type="text" name="searchInfo">
</p>
<input type="submit" value="模糊查询">
</form>
<table border="1">
<tr>
<td>学生编号</td>
<td>学生姓名</td>
<td>科目</td>
<td>创建日期</td>
<td>操作</td>
</tr>
<%
//第一步先跳转doIndex.jsp获取数据
if (request.getAttribute("pageUtils") == null) {
request.getRequestDispatcher("studentServlet").forward(request,
response);
return;
}
PageUtils pagetUtils = (PageUtils) request
.getAttribute("pageUtils");
List<Student> list = pagetUtils.getStudentList();
/* 把数据存到内置对象中 */
request.setAttribute("list", list);
%>
<!-- var代表遍历的对象,items代表遍历的集合,varStatus获取其索引值 -->
<c:forEach var="student" items="${list}" varStatus="sta">
<!-- 隔行变色 -->
<tr
<c:if test="${sta.index %2==0}">style="background-color:red"</c:if>>
<td>${student.sid}</td>
<td>${student.sname}</td>
<td>${student.sproject}</td>
<td>${student.screateDate}</td>
<td><a href="showImage.jsp?spic=${student.spic}">${student.spic}</a>
</td>
<td><a href="deleteServlet?sid=${student.sid}">删除</a> <a
href="goUpdate.jsp?sid=${student.sid}&sname=${student.sname}&sproject=${student.sproject}&screateDate=${student.screateDate}">修改</a>
</td>
</tr>
</c:forEach>
</table>
<a href="studentServlet?pageIndex=1">首页</a>
<%
if (pagetUtils.getCurrentNo() > 1) {
%>
<a href="studentServlet?pageIndex=<%=pagetUtils.getCurrentNo() - 1%>">上一页</a>
<%
}
%>
<%
if (pagetUtils.getCurrentNo() < pagetUtils.getTotalPageSize()) {
%>
<a href="studentServlet?pageIndex=<%=pagetUtils.getCurrentNo() + 1%>">下一页</a>
<%
}
%>
<a href="studentServlet?pageIndex=<%=pagetUtils.getTotalPageSize()%>">末页</a>
<a>共<input type="text" value="<%=pagetUtils.getTotalPageSize()%>">页
第<input type="text" value="<%=pagetUtils.getCurrentNo()%>">页 </a>
</body>
</html>