前台jsp页面与servlet传输图片验证码

我就是我 2022-05-16 08:38 219阅读 0赞

70效果图

<%@page import=”java.util.*“%>
<%@ page language=”java” contentType=”text/html; charset=UTF-8” pageEncoding=”UTF-8”%>
<!DOCTYPE html>




Insert title here





account:
password:
messyzm:
看不清





验证码类

package cn.itcast.vcode.utils;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class VerifyCode {
private int w = 70;
private int h = 35;
private Random r = new Random();
// {“宋体”, “华文楷体”, “黑体”, “华文新魏”, “华文隶书”, “微软雅黑”, “楷体_GB2312”}
private String[] fontNames = {“宋体”, “华文楷体”, “黑体”, “微软雅黑”, “楷体_GB2312”};
private String codes = “23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ”;
private Color bgColor = new Color(255, 255, 255);
private String text ;

  1. private Color randomColor () \{
  2. int red = r.nextInt(150);
  3. int green = r.nextInt(150);
  4. int blue = r.nextInt(150);
  5. return new Color(red, green, blue);
  6. \}
  7. private Font randomFont () \{
  8. int index = r.nextInt(fontNames.length);
  9. String fontName = fontNames\[index\];
  10. int style = r.nextInt(4);
  11. int size = r.nextInt(5) + 24;
  12. return new Font(fontName, style, size);
  13. \}
  14. private void drawLine (BufferedImage image) \{
  15. int num = 3;
  16. Graphics2D g2 = (Graphics2D)image.getGraphics();
  17. for(int i = 0; i < num; i++) \{
  18. int x1 = r.nextInt(w);
  19. int y1 = r.nextInt(h);
  20. int x2 = r.nextInt(w);
  21. int y2 = r.nextInt(h);
  22. g2.setStroke(new BasicStroke(1.5F));
  23. g2.setColor(Color.BLUE);
  24. g2.drawLine(x1, y1, x2, y2);
  25. \}
  26. \}
  27. private char randomChar () \{
  28. int index = r.nextInt(codes.length());
  29. return codes.charAt(index);
  30. \}
  31. private BufferedImage createImage () \{
  32. BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE\_INT\_RGB);
  33. Graphics2D g2 = (Graphics2D)image.getGraphics();
  34. g2.setColor(this.bgColor);
  35. g2.fillRect(0, 0, w, h);
  36. return image;
  37. \}
  38. public BufferedImage getImage () \{
  39. BufferedImage image = createImage();
  40. Graphics2D g2 = (Graphics2D)image.getGraphics();
  41. StringBuilder sb = new StringBuilder();
  42. // 向图片中画4个字符
  43. for(int i = 0; i < 4; i++) \{
  44. String s = randomChar() + "";
  45. sb.append(s);
  46. float x = i \* 1.0F \* w / 4;
  47. g2.setFont(randomFont());
  48. g2.setColor(randomColor());
  49. g2.drawString(s, x, h-5);
  50. \}
  51. this.text = sb.toString();
  52. drawLine(image);
  53. return image;
  54. \}
  55. public String getText () \{
  56. return text;
  57. \}
  58. public static void output (BufferedImage image, OutputStream out)
  59. throws IOException \{
  60. ImageIO.write(image, "JPEG", out);
  61. \}

}

package com.zhang.test;

import java.awt.image.BufferedImage;
import java.io.IOException;
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 cn.itcast.vcode.utils.VerifyCode;

/**
* Servlet implementation class GetImgServlet
*/
@WebServlet(urlPatterns= {“/GetImgServlet”,”/login.action”})
public class GetImgServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public GetImgServlet() {
super();

  1. \}
  2. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException \{
  3. String type=request.getRequestURI();
  4. if(type.endsWith("GetImgServlet")) \{
  5. VerifyCode vc = new VerifyCode();
  6. BufferedImage bi = vc.getImage();
  7. request.getSession().setAttribute("yzm", vc.getText());
  8. vc.output(bi, response.getOutputStream());
  9. \}else \{
  10. String mess=request.getSession().getAttribute("yzm").toString().toLowerCase();
  11. String qtyzm=request.getParameter("qtyzm").toLowerCase();
  12. System.out.println(mess+"\*\*\*"+qtyzm);
  13. if(mess.equals(qtyzm)) \{
  14. response.getWriter().write("successLogin");
  15. \}else \{
  16. response.getWriter().write("failLogin");
  17. \}
  18. \}
  19. \}
  20. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException \{
  21. doGet(request, response);
  22. \}

}
因为浏览器缓存问题所以点击更换验证码要加new Date().getTime();

并导入java包

发表评论

表情:
评论列表 (有 0 条评论,219人围观)

还没有评论,来说两句吧...

相关阅读

    相关 jsp使用servlet实现验证

    在进行表单设计中,验证码的增加恰恰可以实现是否为“人为”操作,增加验证码可以防止网站数据库信息的冗杂等... 现在,小编将讲述通过servlet实现验证码: 验证码作为一个