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