javaweb之验证码验证技术

时间:2022-01-06 19:04:05

今天学习了一个验证码校验技术,所以就写下了一些笔记,方便日后查看。首先创建web工程

1、然后在src目录下创建一个Servlet类,此类用来显示登录页面和错误信息提示

 package com.LHB;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class Login
*/
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter(); //返回一个界面 out.println("<h1>用户登录</h1>");
out.println("<form action='/CheckCode/LoginCl' method='get'>");
out.println("用户名:<input type='text' name='id'><br>");
out.println("密码:<input type='password' name='password'><br>");
out.println("验证码:<input type='text' name='checkcode'><img src='/CheckCode/CreateCode'><br>");//加载验证码图片是只要将产生验证码的对应类路径指定到img标签中即可
out.println("<input type='submit' value='登录'><br>"); out.println("</form>");
String err=(String) request.getAttribute("err");
if(err!=null) {
out.println(err);
}
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

2、继续创建一个Servlet,用来显示校验成功后的页面。

 package com.LHB;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class LoginCl
*/
@WebServlet("/LoginCl")
public class LoginCl extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public LoginCl() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter(); //获取用户的id/password/输入验证码
String id = request.getParameter("id");
String passwd = request.getParameter("password");
String checkCode = request.getParameter("checkcode");
//取出session中验证码
String checkcode2 = (String) request.getSession().getAttribute("checkcode"); //1.先验证码
if(checkCode.equals(checkcode2)) {
//验证码ok
request.getRequestDispatcher("/OK").forward(request, response);
//到数据库去验证
}else {
request.setAttribute("err", "验证码错误");
request.getRequestDispatcher("/Login").forward(request, response);
} } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

3、最后创建验证码产生的Servlet,用来产生随机数校验码图片,并将该验证码图片返回到客户端。此类比较关键。

 package com.LHB;

 import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random; 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; /**
* Servlet implementation class CreateCode
*/
@WebServlet("/CreateCode")
public class CreateCode extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public CreateCode() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //禁止浏览器缓冲随机图片
response.setDateHeader("Expires", -1);//防止启动浏览器时取先前缓冲的数据
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache"); //通知客户机以图片方式打开发送过去的数据
response.setHeader("Content-Type", "image/jpeg"); //在内存中创建一副图片
BufferedImage image = new BufferedImage(80, 30, BufferedImage.TYPE_INT_BGR); //想图片上写数据
Graphics graphics = image.getGraphics(); //设置背景色
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, 80, 30); //设置写入数据的颜色和字体
graphics.setColor(Color.BLACK);
graphics.setFont(new Font(null, Font.BOLD, 20)); //像图片上写数据
String num = makeNum(); //把随机生成的数值,保存到session中
request.getSession().setAttribute("checkcode", num);
graphics.drawString(num, 0, 20); //把写好数据的图片输出给浏览器
ImageIO.write(image, "jpg", response.getOutputStream()); } /**
* 功能:随机生成7位数字
* @return
*/
private String makeNum() {
Random random = new Random();
//9999999可以生成7位数
String num = random.nextInt(9999999)+"";
StringBuffer stringBuffer = new StringBuffer();
//如果不够指定位数则补0
for(int i=0;i<7-num.length();i++) {
stringBuffer.append("0");
}
num = stringBuffer.toString() + num;
return num;
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

4

javaweb之验证码验证技术

javaweb之验证码验证技术

javaweb之验证码验证技术