先來一個分層架構圖:html
WeebRoot目錄下的頁面:java
如今,此項目以實現登陸,註銷,新聞列表,編輯主題》》》》sql
先來登陸部分的關鍵代碼apache
index.jsp中的代碼服務器
userInfoServlet中的所有代碼:架構
TopicServlet中的代碼jsp
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中的代碼post
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("上傳成功"); } } } } %>