原生态的网页验证码(Servlet)

通过Servlet制作原生态的网页验证码

一、网页验证码的显示过程

(一)Jsp页面请求Servlet

1.创建Jsp页面(验证码的生成在ImageServlet中)
<head>
<script type="text/javascript">
		function reloadCode(){
			var time = new Date().getTime();
			document.getElementById("imagecode").src="<%=request.getContextPath() %>/servlet/ImageServlet?d="+time;
		}
	</script>
</head>
<body>
	<h1>验证码</h1>
	<hr>
	<form action="<%=request.getContextPath() %>/servlet/LoginServlet" method="get">
	请输入验证码:<input type="text" name="checkCode"/>
	<img alt="验证码" id="imagecode" src="<%=request.getContextPath() %>/servlet/ImageServlet"></img>
	<a href="javascript: reloadCode();">看不清楚</a><br>
	<input type="submit" value="提交"/>
	</form>
</body>

(二)Servlet保存验证码并返回给页面(保存在Session中,通过response响应返回给页面)

1.创建Servlet(求情是求求请求是通过doget()方式)

import java.awt.image.BufferedImage;
import java.io.IOException;
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;

import com.sun.prism.Graphics;
import com.sun.prism.paint.Color;

public class ImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public ImageServlet() {
        super();
        
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BufferedImage bi=new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);//获取BuffereImage类
		java.awt.Graphics g= bi.getGraphics();				       //获取Graphice类
		java.awt.Color color=new java.awt.Color(200, 150, 255);		       //设置颜色
		g.setColor(color);
		g.fillRect(0, 0, 68, 22);						//画一个长方形背景
		
		char[] ch="ABCDEFGHIJKLNMOPQRSTUVWXYZ0123456789".toCharArray();		//验证码内容
		int len=ch.length,index;
		Random random=new Random();						//随机产生四个数
		
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<4;i++){
			index=random.nextInt(random.nextInt(len));
			g.setColor(new java.awt.Color(random.nextInt(88),random.nextInt(188),random.nextInt(255)));
			g.drawString(ch[index]+"", (i*15)+3, 18);			//将每个验证码写入长方形图中
			sb.append(ch[index]);					//将每个验证码保存在StringBuffere流中,以待验证
		}
		request.getSession().setAttribute("piccode", sb.toString());		//将StringBuffere流保存在Session中
		ImageIO.write(bi, "JPG", response.getOutputStream());			//通过response将验证码图片输到<img>标签中
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

(三)配置Web.xml文件

  <servlet>
  	<servlet-name>ImageServlet</servlet-name>
  	<servlet-class>servlet.ImageServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>ImageServlet</servlet-name>
  	<url-pattern>/servlet/ImageServlet</url-pattern>
  </servlet-mapping>

(四)写验证的Servlet

public class LoginServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		String piccode = (String) request.getSession().getAttribute("piccode");//获取保存在Session中的验证码
		String checkcode = request.getParameter("checkCode");			//获取文本框中输入的验证码
		checkcode = checkcode.toUpperCase();					//变为大写
		response.setContentType("text/html;charset=gbk");			//设置响应的字符类型为“gbk”格式
		PrintWriter out = response.getWriter();		
		if(checkcode.equals(piccode)){
			out.println("验证码输入正确!");
		}else{
			out.println("验证码输入错误!!!");
		}
		out.flush();
		out.close();
		
	}
}

(注:本文的图片资源与代码资源来源与慕课网,代码通过小编一定的修改)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值