JSP13
1 package com.gxl.servlet; 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 import javax.servlet.http.HttpSession; 11 12 import com.gxl.dao.UsersDao; 13 14 @WebServlet("/login") 15 public class LoginServlet extends HttpServlet { 16 17 @Override 18 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 19 throws ServletException, IOException { 20 // TODO Auto-generated method stub 21 req.setCharacterEncoding("UTF-8"); 22 resp.setCharacterEncoding("UTF-8"); 23 doGet(req, resp); 24 } 25 26 @Override 27 protected void doGet(HttpServletRequest request, HttpServletResponse response) 28 throws ServletException, IOException { 29 String uname=request.getParameter("uname"); 30 String upwd=request.getParameter("upwd"); 31 HttpSession session = request.getSession(); 32 UsersDao ud=new UsersDao(); 33 if(ud.login(uname, upwd)){ 34 session.setAttribute("uname", uname); 35 request.getRequestDispatcher("main.jsp").forward(request, response); 36 }else{ 37 response.sendRedirect("no.jsp"); 38 } 39 } 40 41 }
1 package com.gxl.servlet; 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 com.gxl.dao.UsersDao; 12 import com.gxl.entity.Users; 13 14 @WebServlet("/register") 15 public class RegisterServlet extends HttpServlet { 16 @Override 17 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 18 throws ServletException, IOException { 19 // TODO Auto-generated method stub 20 req.setCharacterEncoding("UTF-8"); 21 resp.setCharacterEncoding("UTF-8"); 22 doGet(req, resp); 23 } 24 25 @Override 26 protected void doGet(HttpServletRequest request, HttpServletResponse response) 27 throws ServletException, IOException { 28 String uname=request.getParameter("username"); 29 String upwd=request.getParameter("password"); 30 String email=request.getParameter("email"); 31 32 UsersDao ud=new UsersDao(); 33 Users u=new Users(uname,upwd,email); 34 ud.register(u); 35 response.sendRedirect("login.jsp"); 36 } 37 }
1 package com.gxl.servlet; 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 import javax.servlet.http.HttpSession; 11 12 import com.gxl.dao.MsgDao; 13 import com.gxl.entity.Msg; 14 15 @WebServlet("/write") 16 public class WriteServlet extends HttpServlet { 17 @Override 18 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 19 throws ServletException, IOException { 20 // TODO Auto-generated method stub 21 req.setCharacterEncoding("UTF-8"); 22 resp.setCharacterEncoding("UTF-8"); 23 doGet(req, resp); 24 } 25 26 @Override 27 protected void doGet(HttpServletRequest request, 28 HttpServletResponse response) throws ServletException, IOException { 29 HttpSession session = request.getSession(); 30 String title = request.getParameter("title"); 31 String content = request.getParameter("content"); 32 String receiver = request.getParameter("sendto"); 33 String sender = (String) session.getAttribute("uname"); 34 35 Msg m = new Msg(); 36 m.setSendto(receiver); 37 m.setUname(sender); 38 m.setTitle(title); 39 m.setMsgcontent(content); 40 41 MsgDao md = new MsgDao(); 42 md.writeMsg(m); 43 request.getRequestDispatcher("main.jsp").forward(request, response); 44 } 45 }
1 package com.gxl.servlet; 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 com.gxl.dao.MsgDao; 12 13 @WebServlet("/del") 14 public class DelServlet extends HttpServlet { 15 @Override 16 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 17 throws ServletException, IOException { 18 // TODO Auto-generated method stub 19 req.setCharacterEncoding("UTF-8"); 20 resp.setCharacterEncoding("UTF-8"); 21 doGet(req, resp); 22 } 23 24 @Override 25 protected void doGet(HttpServletRequest request, 26 HttpServletResponse response) throws ServletException, IOException { 27 int mid = Integer.parseInt(request.getParameter("mid")); 28 MsgDao md = new MsgDao(); 29 md.delMsg(mid); 30 request.getRequestDispatcher("main.jsp").forward(request, response); 31 } 32 }