Java绘制登陆验证码

登陆验证码

界面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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值