JSP第十三周作业

1.第十二周上机作业(邮件功能)的控制层代码改用为servlet实现。

1 package com.sk.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.annotation.WebServlet;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import javax.servlet.http.HttpSession;
13 
14 import com.sk.dao.UsersDao;
15 @WebServlet("/dologin.do")
16 public class LoginServlet extends HttpServlet {
17 
18     /**
19      * Constructor of the object.
20      */
21     public LoginServlet() {
22         super();
23     }
24 
25     /**
26      * Destruction of the servlet. <br>
27      */
28     public void destroy() {
29         super.destroy(); // Just puts "destroy" string in log
30         // Put your code here
31     }
32 
33     /**
34      * The doGet method of the servlet. <br>
35      *
36      * This method is called when a form has its tag value method equals to get.
37      * 
38      * @param request the request send by the client to the server
39      * @param response the response send by the server to the client
40      * @throws ServletException if an error occurred
41      * @throws IOException if an error occurred
42      */
43     public void doGet(HttpServletRequest request, HttpServletResponse response)
44             throws ServletException, IOException {
45         doPost(request, response);
46     }
47 
48     /**
49      * The doPost method of the servlet. <br>
50      *
51      * This method is called when a form has its tag value method equals to post.
52      * 
53      * @param request the request send by the client to the server
54      * @param response the response send by the server to the client
55      * @throws ServletException if an error occurred
56      * @throws IOException if an error occurred
57      */
58     public void doPost(HttpServletRequest request, HttpServletResponse response)
59             throws ServletException, IOException {
60              response.setContentType("text/html;charset=utf-8");
61             request.setCharacterEncoding("utf-8");
62             response.setCharacterEncoding("utf-8");
63             String uname = request.getParameter("uname");
64             String upwd = request.getParameter("upwd");
65             UsersDao ud = new UsersDao();
66             HttpSession session = request.getSession();
67             PrintWriter out = response.getWriter();
68             if (ud.login(uname, upwd)) {
69                 session.setAttribute("uname", uname);
70                 request.getRequestDispatcher("main.jsp").forward(request, response);
71             } else {
72                 out.print("登录失败,即将跳回登录页.....");
73                 response.setHeader("refresh", "2;url=login.jsp");
74             }
75     }
76 
77     /**
78      * Initialization of the servlet. <br>
79      *
80      * @throws ServletException if an error occurs
81      */
82     public void init() throws ServletException {
83         // Put your code here
84     }
85 
86 }

LoginServlet
1 package com.sk.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import com.sk.dao.UsersDao;
13 import com.sk.entity.Users;
14 @WebServlet("/doregister.do")
15 public class RegisterServlet extends HttpServlet {
16 
17     /**
18      * Constructor of the object.
19      */
20     public RegisterServlet() {
21         super();
22     }
23 
24     /**
25      * Destruction of the servlet. <br>
26      */
27     public void destroy() {
28         super.destroy(); // Just puts "destroy" string in log
29         // Put your code here
30     }
31 
32     /**
33      * The doGet method of the servlet. <br>
34      *
35      * This method is called when a form has its tag value method equals to get.
36      * 
37      * @param request the request send by the client to the server
38      * @param response the response send by the server to the client
39      * @throws ServletException if an error occurred
40      * @throws IOException if an error occurred
41      */
42     public void doGet(HttpServletRequest request, HttpServletResponse response)
43             throws ServletException, IOException {
44         doPost(request, response);
45     }
46 
47     /**
48      * The doPost method of the servlet. <br>
49      *
50      * This method is called when a form has its tag value method equals to post.
51      * 
52      * @param request the request send by the client to the server
53      * @param response the response send by the server to the client
54      * @throws ServletException if an error occurred
55      * @throws IOException if an error occurred
56      */
57     public void doPost(HttpServletRequest request, HttpServletResponse response)
58             throws ServletException, IOException {
59              response.setContentType("text/html;charset=utf-8");
60             request.setCharacterEncoding("utf-8");
61             response.setCharacterEncoding("utf-8");
62             String uname = request.getParameter("uname");
63             String upwd = request.getParameter("upwd");
64             PrintWriter out = response.getWriter();
65             Users u = new Users();
66             u.setUname(uname);
67             u.setUpwd(upwd);
68             UsersDao ud = new UsersDao();
69             int r = ud.reg(u);
70             if(r >= 1){
71                 request.getRequestDispatcher("login.jsp").forward(request, response);
72             }else{
73                 out.print("注册失败,即将跳回登录页.....");
74                 response.setHeader("refresh", "3;url=register.jsp");
75             }
76     }
77 
78     /**
79      * Initialization of the servlet. <br>
80      *
81      * @throws ServletException if an error occurs
82      */
83     public void init() throws ServletException {
84         // Put your code here
85     }
86 
87 }

RegisterServlet
1 package com.sk.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12 
13 import com.sk.dao.MsgDao;
14 import com.sk.entity.Msg;
15 @WebServlet("/dosend.do")
16 public class SendServlet extends HttpServlet {
17 
18     /**
19      * Constructor of the object.
20      */
21     public SendServlet() {
22         super();
23     }
24 
25     /**
26      * Destruction of the servlet. <br>
27      */
28     public void destroy() {
29         super.destroy(); // Just puts "destroy" string in log
30         // Put your code here
31     }
32 
33     /**
34      * The doGet method of the servlet. <br>
35      *
36      * This method is called when a form has its tag value method equals to get.
37      * 
38      * @param request the request send by the client to the server
39      * @param response the response send by the server to the client
40      * @throws ServletException if an error occurred
41      * @throws IOException if an error occurred
42      */
43     public void doGet(HttpServletRequest request, HttpServletResponse response)
44             throws ServletException, IOException {
45 
46         doPost(request, response);
47     }
48 
49     /**
50      * The doPost method of the servlet. <br>
51      *
52      * This method is called when a form has its tag value method equals to post.
53      * 
54      * @param request the request send by the client to the server
55      * @param response the response send by the server to the client
56      * @throws ServletException if an error occurred
57      * @throws IOException if an error occurred
58      */
59     public void doPost(HttpServletRequest request, HttpServletResponse response)
60             throws ServletException, IOException {
61         response.setContentType("text/html;charset=utf-8");
62         request.setCharacterEncoding("utf-8");
63         response.setCharacterEncoding("utf-8");
64         HttpSession session=request.getSession();
65         String uname = (String)session.getAttribute("uname");//发件人在session中获取
66         String title = request.getParameter("title");
67         String username = request.getParameter("username");
68         String msgcontent = request.getParameter("msgcontent");
69         
70         Msg m = new Msg();
71         m.setTitle(title);
72         m.setMsgcontent(msgcontent);
73         m.setSendto(uname);
74         m.setUsername(username);
75         
76         MsgDao md = new MsgDao();
77         md.addMsg(m);
78         PrintWriter out = response.getWriter();
79         out.print("发送成功,即将跳回首页......");
80         response.setHeader("refresh", "3;url=main.jsp");
81     }
82 
83     /**
84      * Initialization of the servlet. <br>
85      *
86      * @throws ServletException if an error occurs
87      */
88     public void init() throws ServletException {
89         // Put your code here
90     }
91 
92 }

SendServlet
1 package com.sk.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import com.sk.dao.MsgDao;
13 @WebServlet("/dodel.do")
14 public class DelServlet extends HttpServlet {
15 
16     /**
17      * Constructor of the object.
18      */
19     public DelServlet() {
20         super();
21     }
22 
23     /**
24      * Destruction of the servlet. <br>
25      */
26     public void destroy() {
27         super.destroy(); // Just puts "destroy" string in log
28         // Put your code here
29     }
30 
31     /**
32      * The doGet method of the servlet. <br>
33      *
34      * This method is called when a form has its tag value method equals to get.
35      * 
36      * @param request the request send by the client to the server
37      * @param response the response send by the server to the client
38      * @throws ServletException if an error occurred
39      * @throws IOException if an error occurred
40      */
41     public void doGet(HttpServletRequest request, HttpServletResponse response)
42             throws ServletException, IOException {
43         doPost(request, response);
44     }
45 
46     /**
47      * The doPost method of the servlet. <br>
48      *
49      * This method is called when a form has its tag value method equals to post.
50      * 
51      * @param request the request send by the client to the server
52      * @param response the response send by the server to the client
53      * @throws ServletException if an error occurred
54      * @throws IOException if an error occurred
55      */
56     public void doPost(HttpServletRequest request, HttpServletResponse response)
57             throws ServletException, IOException {
58          response.setContentType("text/html;charset=utf-8");
59          request.setCharacterEncoding("utf-8");
60          response.setCharacterEncoding("utf-8");
61          int id = Integer.parseInt(request.getParameter("id"));
62          MsgDao md = new MsgDao();
63           md.delMsg(id);
64           response.sendRedirect("main.jsp");
65     }
66 
67     /**
68      * Initialization of the servlet. <br>
69      *
70      * @throws ServletException if an error occurs
71      */
72     public void init() throws ServletException {
73         // Put your code here
74     }
75 
76 }

DelServlet
1 package com.sk.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12 @WebServlet("/doquit.do")
13 public class QuitServlet extends HttpServlet {
14 
15     /**
16      * Constructor of the object.
17      */
18     public QuitServlet() {
19         super();
20     }
21 
22     /**
23      * Destruction of the servlet. <br>
24      */
25     public void destroy() {
26         super.destroy(); // Just puts "destroy" string in log
27         // Put your code here
28     }
29 
30     /**
31      * The doGet method of the servlet. <br>
32      *
33      * This method is called when a form has its tag value method equals to get.
34      * 
35      * @param request the request send by the client to the server
36      * @param response the response send by the server to the client
37      * @throws ServletException if an error occurred
38      * @throws IOException if an error occurred
39      */
40     public void doGet(HttpServletRequest request, HttpServletResponse response)
41             throws ServletException, IOException {
42         doPost(request, response);
43     }
44 
45     /**
46      * The doPost method of the servlet. <br>
47      *
48      * This method is called when a form has its tag value method equals to post.
49      * 
50      * @param request the request send by the client to the server
51      * @param response the response send by the server to the client
52      * @throws ServletException if an error occurred
53      * @throws IOException if an error occurred
54      */
55     public void doPost(HttpServletRequest request, HttpServletResponse response)
56             throws ServletException, IOException {
57         response.setContentType("text/html;charset=utf-8");
58         request.setCharacterEncoding("utf-8");
59         response.setCharacterEncoding("utf-8");
60         HttpSession session=request.getSession();
61         session.invalidate();
62         PrintWriter out = response.getWriter();
63         out.print("退出成功,即将跳回登录页......");
64         response.setHeader("refresh", "1;url=login.jsp");
65     }
66 
67     /**
68      * Initialization of the servlet. <br>
69      *
70      * @throws ServletException if an error occurs
71      */
72     public void init() throws ServletException {
73         // Put your code here
74     }
75 
76 }

QuitServlet
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6     <title>登录</title>
 7   </head>
 8   <body>
 9   <div align="center">
10       <script type="text/javascript">
11           function validate(){
12               if(loginForm.uname.value == ""){
13               alert("账号不能为空!");
14               return;
15               }
16               if(loginForm.upwd.value == ""){
17               alert("密码不能为空!");
18               return;
19               }
20               loginForm.submit();
21           }
22       </script>
23       <h1>请登录</h1>
24        <form action="dologin.do" name="loginForm" method="post">
25         <table  border="0"  cellpadding="10">
26             <tr><td>用户名:</td>
27                 <td><input type="text" name="uname" value="zc"></td>
28             </tr>
29             <tr><td>密码:</td>
30                 <td><input type="password" name="upwd"value="92"></td>
31             </tr>
32             <tr>
33                 <td><input type="button" value="登录" onclick="validate()"></td>
34                 <td><input type="button" value="注册" onclick="location.href='register.jsp'"></td>
35             </tr>
36         </table>
37     </form>
38     </div>
39   </body>
40 </html>

login
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6     <title>注册</title>
 7   </head>
 8   <body>
 9   <div align="center">
10        <script type="text/javascript">
11           function validate(){
12               if(regForm.uname.value == ""){
13                   alert("账号不能为空!");
14                   return;
15               }
16               if(regForm.upwd.value == ""){
17                   alert("密码不能为空!");
18                   return;
19               }
20               regForm.submit();
21           }
22       </script>
23       <form action="doregister.do" name="regForm" method="post">
24           <table>
25               <tr>
26                   <td>用户名:</td>
27                   <td><input type="text" name="uname"></td>
28               </tr>
29               <tr>
30                   <td>密码:</td>
31                   <td><input type="password" name="upwd"></td>
32               </tr>
33           </table>
34           <input type="button" value="提交" onclick="validate()">
35           <input type="reset" value="重置">
36       </form>
37   </div>
38   </body>
39 </html>

register
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <title>send</title>
 6 </head>
 7 <body>
 8 <div align="center">
 9     <h3>编写你要发送的邮件</h3>
10     <h3><a href="main.jsp" >返回上一页</a></h3>
11     <form action="dosend.do" method="post">
12         <table>
13             <tr>
14                 <td>收件人:</td>
15                 <td><input type="text" name="username" value="<%=request.getParameter("reply")%>"></td>
16             </tr>
17             <tr>
18                 <td>标题:</td>
19                 <td><input type="text" name="title"></td>
20             </tr>
21             <tr>
22                 <td>内容:</td>
23                 <td><textarea rows="6" cols="18" name="msgcontent"></textarea></td>
24             </tr>
25         </table>
26         <table>
27             <tr>
28                 <td><input type="submit" value="提交"> </td>
29                 <td>    </td>
30                 <td><input type="reset" value="重置"></td>
31             </tr>
32         </table>    
33     </form>
34 </div>
35 </body>
36 </html>

send
1 <%@page import="com.sk.entity.Msg"%>
 2 <%@page import="com.sk.dao.MsgDao"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 
 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 6 <html>
 7 <head>
 8 <title>首页</title>
 9 </head>
10 <body>
11 <div align="center">
12     <%
13         MsgDao md = new MsgDao();
14         String uname = (String) session.getAttribute("uname");
15         List<Msg> list = md.getMailByReceiver(uname);
16     %>
17     <h1>欢迎进入邮箱首页!<%=uname %></h1>
18     <a href="unread.jsp">未读邮件</a>
19     <a href="send.jsp">编写邮件</a>
20     <a href="doquit.do">退出登录</a>
21     <h3>收件箱</h3>
22     <table border="1">
23         <tr>
24             <th>发件人</th>
25             <th>标题</th>
26             <th>状态</th>
27             <th>时间</th>
28             <th colspan="2">操作</th>
29         </tr>
30         <%
31             for (int i = 0; i < list.size(); i++) {
32         %>
33         <tr>
34             <td><%=list.get(i).getSendto()%></td>
35             <td><a href="detail.jsp?id=<%=list.get(i).getMsgid()%>"><%=list.get(i).getTitle()%></a></td>
36             <td>
37                 <%if(list.get(i).getState() == 0){%>
38                       <img src="image/weidu.png">
39                 <%}else{%>
40                     <img src="image/yidu.png">
41                 <%}%>
42             </td>
43             <td><%=list.get(i).getMsg_create_date()%></td>
44             <td><a href="send.jsp?reply=<%=list.get(i).getSendto()%>">回复邮件</a></td>
45             <td><a href="dodel.do?id=<%=list.get(i).getMsgid()%>">删除邮件</a></td>
46         </tr>
47         <%
48             }
49         %>
50     </table>
51 </div>
52 </body>
53 </html>

main
1 <%@page import="com.sk.entity.Msg"%>
 2 <%@page import="com.sk.dao.MsgDao"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 
 5 
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>  
 9     <title>邮件详情</title>
10   </head> 
11   <body>
12       <div align="center">
13           <%
14               int msgid = Integer.parseInt(request.getParameter("id"));
15             MsgDao md = new MsgDao();
16             md.updateState(msgid);
17             Msg m = new Msg();
18             m = md.details(msgid);
19         %>
20           <h3>邮件详情</h3>
21           <h3><a href="main.jsp">返回上一页</a></h3>
22         <table border="1">
23             <tr>
24                 <th>发件人</th>
25                 <th>标题</th>
26                 <th>时间</th>
27                 <th>内容</th>
28                 <th colspan="2">操作</th>
29             </tr>
30             <tr>
31                 <td><%=m.getSendto()%></td>
32                 <td><%=m.getTitle()%></td>
33                 <td><%=m.getMsg_create_date()%></td>
34                 <td><%=m.getMsgcontent()%></td>
35                 <td><a href="send.jsp?reply=<%=m.getSendto()%>">回复邮件</a></td>
36                 <td><a href="del.jsp?id=<%=m.getMsgid()%>">删除邮件</a></td>
37             </tr>
38         </table>
39     </div>
40   </body>
41 </html>

detail

 

posted @ 2022-05-29 21:24  小忽悠1  阅读(17)  评论(0编辑  收藏  举报