Requirements:
1. Access the login page with the code of the login.jsp
2. user to enter a user name, password and a verification code.
* If the username and password entered incorrectly, skip the login page Note: user name or password is incorrect
* If the code is entered incorrectly, jump login page Tip: Codes error
* If all input is correct, then jump to the home page success.jsp, displayed: user name, you are welcome
login.jsp page code:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>login</title> <script>
/ * Analysis: Click on the image, the need for a 1. Picture bound to the click event 2. Re-set image src attribute value * /
window.onload = function(){
document.getElementById("img").onclick = function(){
this.src="/yanzhengma_war_exploded/checkCodeServlet?time="+new Date().getTime();
}
}
</script>
<style> div{ color: red; } </style> </head> <body> <form action="/yanzhengma_war_exploded/loginServlet" method="post"> <table> <tr> <td>用户名</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password"></td> </tr> <tr> <td>验证码</td> <td><input type="text" name="checkCode"></td> </tr> <tr> <td colspan="2"><img id="img" src="/yanzhengma_war_exploded/checkCodeServlet"></td> </tr> <tr> <td colspan="2"><input type="submit" value="登录"></td> </tr> </table> </form>
<div><%=request.getAttribute("cc_error") == null ? "" : request.getAttribute("cc_error")%></div> <div><%=request.getAttribute("login_error") == null ? "" : request.getAttribute("login_error") %></div> </body> </html>
Generating code verification code:
@WebServlet ( "/ checkCodeServlet" ) public class CheckCodeServlet the extends the HttpServlet { protected void the doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { // set verification box width, height, int width = 100 ; int height = 50 ; / / 1. Create an object in the image memory (CAPTCHA objects) the BufferedImage image = new new the BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); // 2. landscape image @ 2.1 fill the background color Graphics g = image.getGraphics (); //Brush object g.setColor (Color.PINK); // Set brush color g.fillRect (0,0 , width, height); // 2.2 Videos border g.setColor (Color.BLUE); // width and height of a diminished , half of all the border style not manifest g.drawRect (0,0, width -. 1, height -. 1 ); String STR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789" ; // generating a random angle scaling the random RAN = new new the random (); the StringBuilder SB = new new the StringBuilder (); // this code number 4, so repeated four times for ( int I =. 1; I <= 4; I ++) { Int index = ran.nextInt (str.length ()); // Get the character char CH = str.charAt (index); // random characters sb.append (CH); // 2.3 write codes g.drawString (CH + "", width / I. 5 *, height / 2 ); } String checkCode_session = sb.toString (); // the codes stored in the session . Request.getSession () the setAttribute ( "checkCode_session" , checkCode_session); / / 2.4 Videos interference line g.setColor (Color.green); // randomly generated coordinate point for ( int I = 0; I <10; I ++) { Int X1 = ran.nextInt (width); int X2 = ran.nextInt (width); int Y1 = ran.nextInt (height); int Y2 = ran.nextInt (height); // points is determined line, point (x1, y1) to the point (x2, y2) form a line g.drawLine (X1, Y1, X2, Y2); } // 3. the image output to the display page ImageIO.write (image, "jpg", response .getOutputStream ());
loginServlet Code :( logged-on user related to operation of the database, DAO will be omitted here to simplify writing)
@WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.设置request编码 request.setCharacterEncoding("utf-8"); //2.获取参数 String username = request.getParameter("username"); String password = request.getParameter("password"); String checkCode = request.getParameter("checkCode"); //3.To get a verification code generated Session = the HttpSession request.getSession (); String checkCode_session = (String) session.getAttribute ( "checkCode_session" ); // delete code stored in the session, in order to prevent the return to the original landing page code is still available session.removeAttribute ( " checkCode_session " ); // 3. verification code to determine whether the correct iF (! checkCode_session = null && checkCode_session.equalsIgnoreCase (checkCode)) { // case-insensitive comparison // code correctly // determine the user name and password are the same iF ( "zhangsan" .equals (username) && "123".the equals (password)) { // need to call UserDao query the database //Successful login // store information, user information session.setAttribute ( "User" , username); // redirected to success.jsp Response.sendRedirect (request.getContextPath () + "/ success.jsp" ); } the else { / / login fails // stored message to the Request request.setAttribute ( "login_error", "user name or password error" ); // forwarded to the login page request.getRequestDispatcher ( "/ the login.jsp" ) .forward (Request, the Response ); } } the else { //Code is inconsistent // stored message to the Request request.setAttribute ( "cc_error", "Error Code" ); // forwarded to the login page request.getRequestDispatcher ( "/ the login.jsp" ) .forward (Request, the Response); } } protected void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { the this .doPost (Request, Response); } }
success.jsp page code:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1><%=request.getSession().getAttribute("user")%>,欢迎您</h1> </body> </html>