<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> function reloadCode(){ var time=new Date().getTime(); document.getElementById("imagecode").src="<%=path %>/servlet/ImageServlet?d="+time; } </script> </head> <body> 验证码:<input type="text" name="checkcode" /> <img alt="验证码" id="imagecode" src="<%=path %>/servlet/ImageServlet" /> <a href="javascript:reloadCode();">看不清楚</a><br/> </body> </html>
package com.code; import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ImageServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedImage bImage=new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB); //画图 Graphics graphics=bImage.getGraphics(); Color color=new Color(220,150,130); graphics.setColor(color); //给图片画个框 graphics.fillRect(0, 0, 78, 27); /** * 在图片中显示的内容 * 字母和数字的组合 */ char[] ch="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); //随机获取里面的内容 Random random=new Random(); int len=ch.length,index; StringBuffer sb=new StringBuffer(); for(int i=0;i<4;i++){ index=random.nextInt(len); graphics.setColor(new Color(random.nextInt(88),random.nextInt(188),random.nextInt(255))); graphics.drawString(ch[index]+"",(i*15)+3, 18); sb.append(ch[index]); } //将验证码保存到session为以后比较使用 request.getSession().setAttribute("piccode", sb.toString()); ImageIO.write(bImage, "JPG", response.getOutputStream()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }ImageServlet类写完之后,需要在web.xml里面配置该servlet
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>ImageServlet</servlet-name> <servlet-class>com.code.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageServlet</servlet-name> <url-pattern>/servlet/ImageServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
原文地址:http://blog.csdn.net/smilelifesky/article/details/44450237