首页 > 代码库 > Servlet(七)生成验证码
Servlet(七)生成验证码
1、ImageCode.java
package com.hunhun.utils; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; 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 ImageCode */ @WebServlet("/ImageCode") public class ImageCode extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ImageCode() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //禁止浏览器缓存随机图片 response.setDateHeader("Expires", -1); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); //通知浏览器以图片方式显示数据 response.setHeader("Content-Type", "image/png"); //在内存中创建图片 BufferedImage image = new BufferedImage(75,20,BufferedImage.TYPE_INT_RGB); //获取图片上下文 Graphics g = image.getGraphics(); //设置背景色 g.setColor(Color.black); g.fillRect(0, 0, 75, 20); //生成随机类 Random r = new Random(); //生成4位验证码 String srcChars ="abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String number = ""; for(int i=0;i<4;i++){ number +=srcChars.charAt(r.nextInt(srcChars.length())); } //设置字体、颜色 g.setFont(new Font("Arial",Font.BOLD|Font.ITALIC,16));//new Font(name, style, size) g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); // 随机产生15条干扰线,使图象中的认证码不易被其它程序探测到 for (int i = 0; i < 15; i++) { int x = r.nextInt(75); int y = r.nextInt(20); int xl = r.nextInt(12); int yl = r.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.drawString(number,10,15); //保存session request.getSession().setAttribute("code",number); OutputStream os = response.getOutputStream(); ImageIO.write(image, "png", os); // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); // encoder.encode(image); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }
2.login.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="/LittleMall/LoginCServlet" method="post"> <input type="text" name="code" value=http://www.mamicode.com/"">
>图片样式:
Servlet(七)生成验证码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。