新闻发布系统
先来一个分层架构图:
WeebRoot目录下的页面:
现在,此项目以实现登录,注销,新闻列表,编辑主题》》》》
先来登录部分的关键代码
index.jsp中的代码
userInfoServlet中的全部代码:
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中的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<%@ 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("上传成功"); } } } } %>