验证码生成Servlet:
package entity;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.Math.random;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author ding
*/
@WebServlet(name = "identifyCode", urlPatterns = {"/checkcode"})
public class identifyCode extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet identifyCode</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet identifyCode at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
public static final char[] table
= {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'};
public static Random random = new Random();
//随机生成一个字符;
public static String getRandomString() {
StringBuffer buf = new StringBuffer();
buf.append(table[random.nextInt(table.length)]);
return buf.toString();
}
public static Color getRandomColor(int fc, int bc) {
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
Font myFont = new Font("Arial Black", Font.PLAIN, 16);
int width = 100, height = 30;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
random = new Random();
g.setColor(getRandomColor(200, 250));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(myFont);
g.setColor(getRandomColor(150, 200));
for (int i = 0; i < 150; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(6) + 1;
int y1 = random.nextInt(12) + 1;
g.drawLine(x, y, x + x1, y + y1);
}
for (int i = 0; i < 70; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(12) + 1;
int y1 = random.nextInt(6) + 1;
g.drawLine(x, y, x - x1, y - y1);
}
String str = "";
for (int i = 0; i < 4; i++) {
String temp = getRandomString();
str += temp;
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.drawString(temp, 15 * i + 20, 20);
}
request.getSession().setAttribute("str", str);
g.dispose();
// ServletOutputStream out = response.getOutputStream();
ImageIO.write(image, "JPEG", response.getOutputStream());
// out.flush();
}
验证码页面:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
function reloadImage() {
document.getElementById("Img").src = 'checkcode?now=' + new Date().getTime();
}
</script>
<style>
.one{
position: absolute;
top:30%;
left:30%;
}
</style>
</head>
<body>
<div class="one">
<form action="deal.jsp" method="post" id="registe">
输入验证码:
<input type="text" name="verCode" id ="verCode" />
<img src="checkcode" id="Img" align="absmiddle" οnclick="reloadImage()" />
<input type ="submit" name="btn" value="check" />
<p>${result}</p>${str}
<%
session.invalidate();
%>
</div>
</form>
</body>
</html>
验证结果页面:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String result = "";
if(request.getParameter("verCode").toUpperCase().equals(session.getAttribute("str"))) {
result="验证码正确!";
session.setAttribute("result", result);
response.sendRedirect("index.jsp");
}
else{
result = "验证码不正确,请重新输入!";
session.setAttribute("result", result);
response.sendRedirect("index.jsp");
}
%>
</body>
</html>