大致的思路:先用java生成验证码,然后将生成的验证码保存到session中,在表单提交时,将保存到session中的验证码与表单提交的验证码进行比较,然后输出不同的结果。其实还可以实现一个目标,在用户禁用cookie也能验证成功,那就需要url重写了。这个例子只是给了一个简单的验证码的实现,当然可以根据实际的需求写出更加精彩的验证码,比如“汉字”验证码,算术验证码。本文只是给怎样实现验证码提供了一个基本的思路。
ImageCodeUtil.java
package cn.zq.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Random; import javax.imageio.ImageIO; /** * 图形码工具类 * @author zq */ public class ImageCodeUtil { /** * 下面配置的这些常量,最好的办法就是通过配置文件来获取,下面直接写死了 */ /** * 图形码的宽 */ private static final int IMAGE_CODE_WIDTH = 120; /** * 图形码的高 */ private static final int IMAGE_CODE_HEIGHT = 60; /** * 图形码个数 */ private static final int IMAGE_CODE_NUM = 4; /** * 图形码最多干扰线数量 */ private static final int IMAGE_CODE_MAX_LINE_NUM = 15; /** * 图形码最小干扰线数量 */ private static final int IMAGE_CODE_MIN_LINE_NUM = 10; /** * 图片类型 */ private static final String IMAGE_TYPE = "jpg"; /** * 图形码取数 */ private static final String IMAGE_CODE_DATA = "ABCDEFGHIJKLMNOPQRSJUVWXYZabcdefghijklmnopqrsjuvwxyz0123456789"; private static final Random rand = new Random(); /** * 生成图形码 * @param os OutputStream 接受 流 * @return String 图形码 */ public static String generateSimpleImageCode(OutputStream os){ return generateSimpleImageCode(os, true); } /** * 生成图形码 * @param os OutputStream 接受 流 * @param drawLine boolean 是否画干扰线 * @return String 图形码 */ public static String generateSimpleImageCode(OutputStream os, boolean drawLine){ StringBuilder code = new StringBuilder(); try { //创建一张图片 BufferedImage image = new BufferedImage(IMAGE_CODE_WIDTH, IMAGE_CODE_HEIGHT, BufferedImage.TYPE_INT_RGB); //拿到画笔 Graphics g = image.getGraphics(); //画边框 g.drawRect(0, 0, IMAGE_CODE_WIDTH, IMAGE_CODE_HEIGHT); g.setColor(Color.WHITE); g.fillRect(0, 0, IMAGE_CODE_WIDTH, IMAGE_CODE_HEIGHT); //产生随机码 for(int i = 0; i < IMAGE_CODE_NUM; i++){ int index = rand.nextInt(IMAGE_CODE_DATA.length()); String str = String.valueOf(IMAGE_CODE_DATA.charAt(index)); code.append(str); g.setFont(new Font("宋体", Font.PLAIN, 30)); g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))); g.drawString(str, 10 + 30 * i, 40 + rand.nextInt(6)); } if (drawLine) { int randLineNum = IMAGE_CODE_MIN_LINE_NUM + rand.nextInt(IMAGE_CODE_MAX_LINE_NUM - IMAGE_CODE_MIN_LINE_NUM); //画干扰线 for(int i = 0; i < randLineNum; i++){ g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))); g.drawLine(rand.nextInt(IMAGE_CODE_WIDTH), rand.nextInt(IMAGE_CODE_HEIGHT), rand.nextInt(IMAGE_CODE_WIDTH), rand.nextInt(IMAGE_CODE_HEIGHT)); } } ImageIO.write(image, IMAGE_TYPE, os); return code.toString(); } catch (Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) throws Exception{ System.out.println(IMAGE_CODE_DATA.length()); FileOutputStream fos = new FileOutputStream("c:\\imagecode.jpg"); String code = generateSimpleImageCode(fos); fos.close(); System.out.println(code); } }
GetImageCodeServlet.java
package cn.zq.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import cn.zq.util.ImageCodeUtil; public class GetImageCodeServlet extends HttpServlet { private static final long serialVersionUID = 5225185955711424724L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置浏览器不要缓存 response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache"); response.setHeader("expires", "0"); response.setContentType("image/jpeg"); HttpSession session = request.getSession(); ServletOutputStream sos = response.getOutputStream(); String code = ImageCodeUtil.generateSimpleImageCode(sos); session.setAttribute("imageCode", code); } }
FormServlet.java
package cn.zq.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class FormServlet extends HttpServlet { private static final long serialVersionUID = -5699696644228229140L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); //获取客户端提交的图像码 String clientImageCode = request.getParameter("imageCode"); HttpSession session = request.getSession(); //获取服务器端的图像码 String serverImageCode = (String) session.getAttribute("imageCode"); PrintWriter out = response.getWriter(); if(serverImageCode != null){ if(serverImageCode.equalsIgnoreCase(clientImageCode)){ out.print("已经通过验证!"); }else{ out.print("验证码输入有误"); } session.removeAttribute("imageCode"); }else{ out.print("验证码失效!"); } } }
form.html
<span style="color:#000000;"><!DOCTYPE html> <html> <head> <title>输入验证码</title> </head> <body> <form action="formServlet" method="post"> <input type="text" name="imageCode"><img src="getImageCodeServlet" border="1px"> <br/> <input type="submit" value="验证"> </form> </body> </html></span>
web.xml文件的有关配置:
<span style="color:#000000;"><?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>GetImageCodeServlet</servlet-name> <servlet-class>cn.zq.servlet.GetImageCodeServlet</servlet-class> </servlet> <servlet> <servlet-name>FormServlet</servlet-name> <servlet-class>cn.zq.servlet.FormServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>GetImageCodeServlet</servlet-name> <url-pattern>/getImageCodeServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>FormServlet</servlet-name> <url-pattern>/formServlet</url-pattern> </servlet-mapping> </web-app></span>
与验证码类似的经典问题有,怎样防止表单重复提交,这类问题很有意思,防止表单提交的要点就是在表单用隐藏域保存从服务器端获取的值,当表单提交时,拿这个值与服务器端的值进行比较。大致的思路与验证码很类型,都是在服务器端存放值,在从客户浏览器表单提交时获取客户端的值进行比较。源代码可以再这里下载
时间: 2024-11-03 22:32:45