JSP第十三周作业

控制层代码改用为servlet实现

dologin.java

 1 package doservlet;
 2  
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9  
10 import dao.UsersDao;
11  
12 /**
13  * Servlet implementation class dologin
14  */
15 @WebServlet("/dologin")
16 public class dologin extends HttpServlet {
17     private static final long serialVersionUID = 1L;
18         
19     /**
20      * @see HttpServlet#HttpServlet()
21      */
22     public dologin() {
23         super();
24         // TODO Auto-generated constructor stub
25     }
26  
27     /**
28      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
29      */
30     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
31         // TODO Auto-generated method stub
32         doPost(request, response);
33     }
34  
35     /**
36      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
37      */
38     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
39         // TODO Auto-generated method stub
40         String uname = request.getParameter("uname");
41         String upwd = request.getParameter("upwd");
42         UsersDao ud=new UsersDao();
43         if(ud.login(uname, upwd)){
44             request.getSession().setAttribute("uname", uname);
45             request.getRequestDispatcher("main.jsp").forward(request, response);
46         }else{
47             response.getWriter().print("登陆失败,即将跳回登陆页.....");
48             response.setHeader("refresh", "5;url=login.jsp");
49         }
50     }
51  
52 }

doreg.java

 1 package doservlet;
 2  
 3 import java.io.IOException;
 4  
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10  
11 import dao.UsersDao;
12  
13 /**
14  * Servlet implementation class doreg
15  */
16 @WebServlet("/doreg")
17 public class doreg extends HttpServlet {
18     private static final long serialVersionUID = 1L;
19         
20     /**
21      * @see HttpServlet#HttpServlet()
22      */
23     public doreg() {
24         super();
25         // TODO Auto-generated constructor stub
26     }
27  
28     /**
29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
30      */
31     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
32         // TODO Auto-generated method stub
33         doPost(request, response);
34     }
35  
36     /**
37      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
38      */
39     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40         // TODO Auto-generated method stub
41         String uname = request.getParameter("uname");
42         String upwd = request.getParameter("upwd");
43         UsersDao ud=new UsersDao();
44         ud.reg(uname, upwd);
45         request.getRequestDispatcher("login.jsp").forward(request, response);
46     }
47  
48 }

dodelete.java

 1 package doservlet;
 2  
 3 import java.io.IOException;
 4  
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10  
11 import dao.MsgDao;
12  
13 /**
14  * Servlet implementation class dodelete
15  */
16 @WebServlet("/dodelete")
17 public class dodelete extends HttpServlet {
18     private static final long serialVersionUID = 1L;
19         
20     /**
21      * @see HttpServlet#HttpServlet()
22      */
23     public dodelete() {
24         super();
25         // TODO Auto-generated constructor stub
26     }
27  
28     /**
29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
30      */
31     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
32         // TODO Auto-generated method stub
33         doPost(request, response);
34     }
35  
36     /**
37      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
38      */
39     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40         // TODO Auto-generated method stub
41         int id=Integer.parseInt(request.getParameter("id"));
42         MsgDao md=new MsgDao();
43         md.delMail(id);
44         response.sendRedirect("main.jsp");
45     }
46  
47 }

dowrite.java

 1 package doservlet;
 2  
 3 import java.io.IOException;
 4  
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10  
11 import dao.MsgDao;
12 import entity.Msg;
13  
14 /**
15  * Servlet implementation class dowrite
16  */
17 @WebServlet("/dowrite")
18 public class dowrite extends HttpServlet {
19     private static final long serialVersionUID = 1L;
20         
21     /**
22      * @see HttpServlet#HttpServlet()
23      */
24     public dowrite() {
25         super();
26         // TODO Auto-generated constructor stub
27     }
28  
29     /**
30      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
31      */
32     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33         // TODO Auto-generated method stub
34         doPost(request, response);
35     }
36  
37     /**
38      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
39      */
40     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41         // TODO Auto-generated method stub
42         request.setCharacterEncoding("utf-8");
43         String uname=(String)request.getSession().getAttribute("uname");
44         String addressee=request.getParameter("addressee");
45         String title=request.getParameter("title");
46         String content=request.getParameter("content");
47          
48         Msg m=new Msg();
49         m.setContent(content);
50         m.setSender(uname);
51         m.setAddressee(addressee);
52         m.setTitle(title);
53          
54         MsgDao md=new MsgDao();
55         md.addMsg(m);
56          
57         response.getWriter().print("发送成功.....");
58         response.setHeader("refresh", "5;url=main.jsp");
59     }
60  
61 }

 

posted @ 2022-05-29 18:20  唐一南  阅读(1)  评论(1编辑  收藏  举报