analysis
LoginServlet class
@WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置request编码 request.setCharacterEncoding("utf-8"); //获取参数 String username = request.getParameter("username"); String password = request.getParameter("password"); String checkcode = request.getParameter("checkcode"); determine whether the correct verification code// The session = the HttpSession request.getSession (); // get a code value String checkCode_session = (String) session.getAttribute ( "checkCode" ); // a complete acquisition, delete it immediately, in order to ensure a one-time authentication code session. removeAttribute ( "checkCode" ); iF (! checkCode_session = null && checkCode_session.equalsIgnoreCase (checkcode)) { // case-insensitive comparison // code is correct // 1. Analyzing codes and passwords are consistent iF ( "zhangsan" .equals (username) && "123".the equals (password)) { // perfect userDao need to call to query the database // landing successfully //1. The user information stored session.setAttribute ( "User" , username); // 2. redirection success.jsp Response.sendRedirect (request.getContextPath () + "/ success.jsp" ); } the else { // login failed // 1. storage information to request domain request.setAttribute ( "login_error", "user name or password is incorrect" ); // 2. forwarding request.getRequestDispatcher ( "/ the login.jsp" ) .forward (request, the Response ); } } The else { // this code is not correct @1.存储信息到request域中 request.setAttribute("cc_error","验证码不正确"); //2.转发 request.getRequestDispatcher("/login.jsp").forward(request,response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
Servlet class codes
@WebServlet("/checkCodeServlet") public class CheckCodeServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int width=100; int height=50; BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setColor(Color.green); g.drawRect(0,0,width,height); g.setColor(Color.yellow); g.fillRect(0,0,width-1,height-1); String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random=new Random(); g.setColor(Color.magenta); StringBuilder sb = new StringBuilder(); for (int i=1;i<=4;i++){ int index = random.nextInt(str.length()); char ch=str.charAt(index); g.drawString(ch+"",i*width/5,height/2); sb.append(ch); } String checkCode_session = sb.toString(); HttpSession session=request.getSession(); session.setAttribute("checkCode",checkCode_session); System.out.println("验证码是 : " + checkCode_session); //画干扰线 g.setColor(Color.green); for (int i = 0; i < 16; i++) { int x1=random.nextInt(width); int x2=random.nextInt(width); int y1=random.nextInt(height); int y2=random.nextInt(height); g.drawLine(x1,y1,x2,y2); } ImageIO.write(image,"jpg",response.getOutputStream()); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>login</title> <script> window.onload=function () { var img = document.getElementById('img'); img.onclick=function () { var date=new Date().getTime(); // img.src="/session/checkCodeServlet?"+date this.src="/session/checkCodeServlet?time="+date } } </script> <style> div{ color: red; } </style> </head> <body> <form action="/session/loginServlet"> <table> <tr> <td>用户名</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password"></td> </tr> <codes>TD<>TR </td> <td><input type="text" name="checkcode"></td> </tr> <tr> <td colspan="2"><img id="img" src="/session/checkCodeServlet"></td> </tr> <tr> <td colspan="2"><input type="submit" value="登录"></td> </tr> </table> </form> <div><%= request.getAttribute("login_error")==null?"":request.getAttribute("login_error")%></div> <div><%= request.getAttribute("cc_error")==null?"":request.getAttribute("cc_error")%></div> </body> </html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录成功</title> </head> <body> 欢迎登录,<h1><%=request.getSession().getAttribute("user") %></h1> </body> </html>