jsp---》》》新闻发布系统的项目跟踪+++++++文件上传
先来一个分层架构图:
WeebRoot目录下的页面:
现在,此项目以实现登录,注销,新闻列表,编辑主题》》》》
先来登录部分的关键代码
index.jsp中的代码
userInfoServlet中的全部代码:
package cn.news.Servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.news.dao.INewsDAO; import cn.news.dao.impl.NewsInfoDAOImpl; import cn.news.entity.NewsInfo; import cn.news.entity.UserInfo; import cn.news.service.INewsInfoService; import cn.news.service.IUserInfoService; import cn.news.service.impl.NewInfoServiceImpl; import cn.news.service.impl.UserInfoServiceImpl; public class UserInfoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost( request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //准备新闻数据 /*INewsInfoService newsservice =new NewInfoServiceImpl(); try { List<NewsInfo> list=newsservice.getAllNews(); //放入request作用域 request.setAttribute("newsList", list); } catch (SQLException e) { }*/ INewsDAO dao=new NewsInfoDAOImpl(); List<NewsInfo> list=null; try { list = dao.getAllNews(); //System.out.println(list.size()); request.setAttribute("newsList", list); } catch (SQLException e1) { e1.printStackTrace(); } String action=request.getParameter("action"); if("logout".equals(action)){ //执行注销 //第一步:先清除session request.getSession().removeAttribute("uname"); //第二步:跳转到index.jsp(登录页面) /*response.sendRedirect("/NewsManagerSystem/index.jsp");*/ request.getRequestDispatcher("/index.jsp").forward(request, response); }else if("login".equals(action)){ //code write here //1.解决乱码 request.setCharacterEncoding("utf-8"); //2.解析前台传递的表单数据 String uname=request.getParameter("uname"); String upwd=request.getParameter("upwd"); //拼接成一个用户对象 UserInfo info=new UserInfo(); info.setUname(uname); info.setUpwd(upwd); IUserInfoService service=new UserInfoServiceImpl(); try { boolean flag = service.isLogin(info); if (flag) { //登录成功 //3.记录session request.getSession().setAttribute("uname", uname); //4.转发或者重定向 转发不需要加项目名称 request.getRequestDispatcher("/newspages/admin.jsp").forward(request, response); }else { response.sendRedirect("/NewsManagerSystem/index.jsp"); } } catch (SQLException e) { e.printStackTrace(); } }else{ //第一次访问首页 request.getRequestDispatcher("/index.jsp").forward(request,response); } } }
TopicServlet中的代码
package cn.news.Servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.news.dao.impl.TopicDAOImpl; import cn.news.entity.Topic; import cn.news.service.ITopicService; import cn.news.service.impl.TopicServiceImpl; public class TopicServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //调度Topic的service ITopicService service=new TopicServiceImpl(); try { List<Topic> list=service.getAllTopic(); request.setAttribute("topiclist", list); //System.out.println(list.size()); //转发到列表页 request.getRequestDispatcher("/newspages/topic_list.jsp").forward(request, response); } catch (SQLException e) { } } }
topic_list中的代码
文件上传
需要用到commons-fileupload-1.3.2.jar和commons-io-2.5.jar两个jar文件
需求:点击提交上传文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> </head> <body> <%=path %> <form enctype="multipart/form-data" action="<%=path %>/test.jsp" method="post"> 姓 名: <input name="username"/> 选择文件:<input type="file" name="myfile" /> <input type="submit" value="上传"/> </form> </body> </html>
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="java.io.File"%> <%@page import="org.apache.commons.fileupload.FileItem"%> <%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%> <%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%> <% request.setCharacterEncoding("UTF-8"); //将用户传递过来的request中的内容解析出来,判定其类型,如果是常规的普通表单字段,执行处理方式一 //否则就代表的是文件域,那么执行处理方式二 //01.判定是普通表单,还是用来上传文件的表单 boolean flag= ServletFileUpload.isMultipartContent(request); if(flag){ //开始找到一个解析器,来解析请求中的各个项目 DiskFileItemFactory factory=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload(factory);//解析器upload创建成功 List<FileItem> fileList=upload.parseRequest(request); //每一个FileItem都代表一个表单元素 Iterator<FileItem> myitor=fileList.iterator(); while(myitor.hasNext()){ FileItem item=myitor.next(); if(item!=null){ //对当前item进行验证,看它是普通表单还是上传用的文件域 if(item.isFormField()){//证明是普通的表单字段 //获取该表单元素的内容(value值) String name=item.getFieldName();//这是表单元素的name属性,而不是value值 if(name.equals("username")){ out.print(item.getString("UTF-8")); } }else{ //是一个文件域 String fileName=item.getName();//ef.doc //扔到服务器的目录下 String path="/WEB-INF/upload";//文件上传不能用相对路径 //将相对路径转换成绝对路径 String abosolutePath=this.getServletContext().getRealPath(path); File file=new File(fileName); File uploadFile=new File(abosolutePath,file.getName()); item.write(uploadFile); out.print("上传成功"); } } } } %>