实现功能简单,后续慢慢完善
功能:
1.显示验证码图片,验证码来自A-za-z0-9任意选6个组成的
2.点击切换验证码
3.添加了输入验证码验证,忽略大小写一致则页面返回验证成功,不一致,返回输入验证码不正确
servlet
package web;
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;
import utils.StringUtils;
import utils.image.ImageUtils;
/**
* Servlet implementation class CheckCodeServlet
*/
@WebServlet("/CheckCodeServlet")
public class CheckCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final int width = 80;
private static final int height = 30;
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(ImageUtils.getRandColor());
g.fillRect(0, 0, width, height);
g.setColor(new Color(0,0,0));
g.setFont(new Font(null, Font.BOLD|Font.ITALIC, 18));
//生成验证码
String code = StringUtils.random(6);
g.drawString(code, 10, 20);
Random rand = new Random();
//画干扰线
for(int i=0;i<10;i++){
int x = rand.nextInt(width);
int y = rand.nextInt(height);
int x1 = rand.nextInt(width);
int y1 = rand.nextInt(height);
g.drawLine(x, y, x1, y1);
}
response.setContentType("image/jpeg");
ImageIO.write(image, "jpeg", response.getOutputStream());
}
}
验证码验证servlet
package web;
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 CheckCodeServlet
*/
@WebServlet("/CheckCodeServlet")
public class VerityCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String input = request.getParameter("checkcode");
String value =(String) request.getSession().getAttribute("checkcode");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
if(value!=null&&value.equalsIgnoreCase(input)){
out.print("验证成功");
}else{
out.print("输入验证码不正确");
}
out.close();
}
}
StringUtilsjava
public static String random(int a,int b){
Random rand = new Random();
return String.valueOf((char)(a + rand.nextInt(b-a)));
}
public static String random(int length){
String code = "";
Random rand = new Random();
for(int i=0;i<length;i++){
String s = random(65,90);
String s1 = random(97,122);
String s2 = random(48,57);
String[] array = {s,s1,s2};
code+=array[rand.nextInt(3)];
}
return code;
}
jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=request.getContextPath() %>/veritycode" method="post">
验证码:<input type="text" name="checkcode"/>
<img id="checkcode" alt="" src="checkcode" οnclick="this.src='checkcode?'+Math.random()" >
<a href="javascript:;" οnclick="document.getElementById('checkcode').src='checkcode?'+Math.random()">看不清,换一张</a>
<input type="submit" value="验证"/>
</form>
</body>
</html>
web.xml配置
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0">
<servlet>
<servlet-name>checkcode</servlet-name>
<servlet-class>web.CheckCodeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>veritycode</servlet-name>
<servlet-class>web.VerityCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>checkcode</servlet-name>
<url-pattern>/checkcode</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>veritycode</servlet-name>
<url-pattern>/veritycode</url-pattern>
</servlet-mapping>
</web-app>