Utils类
package com.xiaobear;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import java.awt.*;
public class ImageUtils {
private Random random = new Random();
private static int WIDTH = 100;
private static int HEIGHT = 50;
Color bk = new Color(255,255,255);
private String text;
String chars = "23456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private Color randomColor() {
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
return new Color(r, g, b);
}
private static Font getRandomFont() {
String[] fonts = {"Georgia", "Verdana", "Arial", "Tahoma", "Time News Roman", "Courier New", "Arial Black", "Quantzite"};
int fontIndex = (int)Math.round(Math.random() * (fonts.length - 1));
int fontSize = (int) Math.round(Math.random() * 4 + 16);
return new Font(fonts[fontIndex], Font.PLAIN, fontSize);
}
public void threeLine(BufferedImage img){
Graphics2D g = (Graphics2D) img.getGraphics();
for (int i = 0; i < 10; i++) {
int x = random.nextInt(WIDTH);
int y = random.nextInt(HEIGHT);
int x1 = random.nextInt(WIDTH);
int y1 = random.nextInt(HEIGHT);
g.setColor(randomColor());
g.drawLine(x, y, x1, y1);
}
}
private char randomChar(){
int len = chars.length();
int index = random.nextInt(len);
char c = chars.charAt(index);
return c;
}
public BufferedImage createImage(){
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(bk);
graphics.fillRect(0,0,WIDTH,HEIGHT);
return image;
}
public BufferedImage getImage(HttpServletRequest request, HttpServletResponse response) {
BufferedImage image = createImage();
Graphics2D g = (Graphics2D) image.getGraphics();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 4; i++) {
String s = randomChar() + "";
builder.append(s);
float v = i * 1.0f * WIDTH / 4;
g.setFont(getRandomFont());
g.setColor(randomColor());
g.drawString(s,v,HEIGHT-2);
}
text = builder.toString();
request.getSession().setAttribute("value",text);
threeLine(image);
return image;
}
public String getText() {
return text;
}
public static void output(BufferedImage img, OutputStream out) throws IOException {
ImageIO.write(img,"JPEG",out);
}
}
测试类
@WebServlet(name = "ImageOutServlet",urlPatterns = "/imageOut")
public class ImageOutServlet extends HttpServlet {
@Override
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
ImageUtils imageUtils = new ImageUtils();
BufferedImage image = imageUtils.getImage(request, response);
ImageUtils.output(image,response.getOutputStream());
}
}
登录页面login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
login.jsp
<%
Object msg = request.getAttribute("msg");
if (msg == null) {
response.getWriter().write("");
}else {
response.getWriter().print(msg);
}
%>
<%
Object msg1 = request.getAttribute("msg1");
if (msg1 == null) {
response.getWriter().write("");
}else {
response.getWriter().print(msg1);
}
%>
登录页面验证码验证(从Session中获取值)
@WebServlet(name = "LoginServlet",urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Paragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String checkKey = request.getParameter("checkKey");
if ("xiaobear".equals(username) && "111111".equals(password) ){
if (request.getSession().getAttribute("value").equals(checkKey)){
response.getWriter().write("恭喜你,登录成功!");
}else {
request.setAttribute("msg","验证码错误,请重新输入!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}else {
request.setAttribute("msg1","用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}