Java绘制登陆验证码

来源:互联网 发布:翻译软件 编辑:程序博客网 时间:2024/06/01 20:07

登陆验证码

界面JSP代码
<span style="font-family:Comic Sans MS;font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%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="css/style.css"> --><style type="text/css"></style><script  type="text/javascript">function changeCode(){//多次加载同一张图片,浏览器会缓存,这里所以添加一个参数var time=new Date().getTime();document.getElementById("img").src="<%=request.getContextPath()%>/verificationCode?time"+time;}</script>  </head>  <body>    <div id="myCode">   <form action="Code" method="POST">      验证码 <input type="text" id="codeInput" name="codeInput" >    <img alt="验证码" id="img" src="<%=request.getContextPath()%>/verificationCode">    <a href="javascript:changeCode();"><img id="reflash" style="width: 22px;height: 22px;" src="img/reflash.png"></a><br/><input type="submit" value="提交"/> </form>    </div>  </body></html></span>
我的Servlet代码
<span style="font-family:Comic Sans MS;font-size:18px;">package com.tang.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 verificationCode extends HttpServlet {/** *  */private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request,response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/* BufferedImage是Image的一个子类,BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便的操作这个图片,通常用来做图片修改操作如大小变换、图片变灰、设置图片透明或不透明等。*/BufferedImage bi=new BufferedImage(68,22,BufferedImage.TYPE_INT_RGB);//Graphics 类是所有图形上下文的抽象基类,允许应用程序在组件(已经在各种设备上实现)以及闭屏图像上进行绘制。Graphics g=bi.getGraphics();Color c=new Color(200,150,255);g.setColor(c);/*fillRect(int x,int y,int width, int height) * x - 要绘制矩形的 x 坐标。 * y - 要绘制矩形的 y 坐标。         * width - 要绘制矩形的宽度。 * height - 要绘制矩形的高度。 */g.fillRect(0, 0, 68, 22);char[] ch="ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789".toCharArray();//实例化随机对象Random r=new Random();int len=ch.length,index;StringBuffer sb=new StringBuffer();for(int i=0;i<4;i++){//产生0~len长度的随机数index=r.nextInt(len);System.out.println("index=r.nextInt(len):"+index);//设置字的颜色//new Color(r.nextInt(88),r.nextInt(88),r.nextInt(255))g.setColor(new Color(r.nextInt(88),r.nextInt(88),r.nextInt(255)));//drawString(String str, int x, int y)  使用此图形上下文的当前字体和颜色绘制由指定 string 给定的文本。   g.drawString(ch[index]+"",(i*15)+3,18);   sb.append(ch[index]);}//保存到session中,在服务器中通过获取session对比验证码是否正确request.getSession().setAttribute("pircode", sb.toString());ImageIO.write(bi,"JPG",response.getOutputStream());}}</span>

0 0
原创粉丝点击