Log in to practice with verification code

package com.hopetesting.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
* @author newcityman
* @date 2019/9/1 - 20:42
*/
@WebServlet("/ checkCodeServlet")
public class CheckCodeServlet the extends the HttpServlet {
protected void the doPost (the HttpServletRequest Request , the HttpServletResponse Response) throws ServletException , IOException {
//. 1, create an object image stored in memory (CAPTCHA objects) int width = 100 ; height = int 50 ; the BufferedImage image = new new the BufferedImage (width , height , the BufferedImage. TYPE_INT_RGB that) ; // 2, landscape image // 2.1, fill the background color Graphics image.getGraphics G = () ; g.setColor (. color PINK) ; g.fillRect ( 0 , 0







, Width , height) ;

// 2.2, Videos rectangular border
g.setColor (Color. Blue) ;
g.drawRect ( 0 , 0 , width - . 1 , height - . 1) ;

// 2.3, randomly generated subscript
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
the Random RAN = new new the Random () ;

// Get the character
g.setColor (Color. RED) ;
the StringBuilder SB = new new the StringBuilder () ;
for ( int I = . 1 ; I <= . 4 ; I ++) {
int index ran.nextInt = (str.length ()) ;
char c = str.charAt(index);
sb.append(c);
g.drawString(c + "", width / 5 * i, height / 2);
}
String checkcode_session = sb.toString();
request.getSession().setAttribute("checkcode_session",checkcode_session);
// 2.4 设置干扰线
g.setColor(Color.BLACK);

for (int i = 0; i < 8; i++) {
int x1 = ran.nextInt(width);
int x2 = ran.nextInt(width);
int y1 = ran.nextInt(height);
int y2 = ran.nextInt(height);
g.drawLine(x1, y1, x2, y2);
}


// 3、将图片输送到页面显示
ImageIO.write(image, "jpg", response.getOutputStream());


}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}


package com.hopetesting.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
* @author newcityman
* @date 2019/9/4 - 23:22
*/
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void the doPost (the HttpServletRequest request , the HttpServletResponse Response) throws ServletException , IOException {
//. 1, encoding settings request
Request.setCharacterEncoding ( "UTF-. 8") ;
// 2, acquiring parameters
String = request.getParameter username ( "username") ;
String = request.getParameter password ( "password") ;
String checkCode = request.getParameter ( "checkCode") ;
//. 3, the generated codes acquired
the HttpSession Request.getSession the session = () ;
String checkcode_session = (String) session.getAttribute ( "checkcode_session") ;
// delete the session code stored
session.removeAttribute ("checkcode_session") ;
//. 4, determines whether the correct verification code IF (! checkcode_session = null && checkcode_session.equalsIgnoreCase (checkCode)) { // correct IF ( "ZMY" .equals (username) && "123" .equals (password )) { // successful login // store user information session.setAttribute ( "username" , username) ; // redirected to success.jsp Response.sendRedirect (request.getContextPath () + "/success.jsp") ; } {the else // failed on landing request.setAttribute ( "login_error" , "user name or password error") ; request.getRequestDispatcher ( "/login.jsp").forward(request, response);












}
} else {
//错误
request.setAttribute("cc_error", "验证码错误,请重试");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
<script>
window.onload = function () {
document.getElementById("img").onclick = function () {
this.src = "/day16/checkCodeServlet?time=" + new Date().getTime();
}
}
</script>

<style>
div{
color: red;
}
</style>
</head>
<body>
<form action="/day16/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 src="/day16/checkCodeServlet" id="img" alt="验证码">
</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>


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1><%=request.getSession().getAttribute("username")%>,恭喜您成功登陆</h1>
</body>
</html>

Guess you like

Origin www.cnblogs.com/newcityboy/p/11462686.html