javaWeb验证码

在web开发中,我们经常会用到,验证码的用法。现在就给大家写一个例子。


注:利用到的技术:主要是HttpServlet


jsp主页面:


<%@ 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="styles.css">
	-->
  </head><!-- 该函数无法执行的话,可能是没有通知浏览器不要缓存的原因 -->
  <script type="text/javascript">
  	function toreal(){
  		document.location.href="index.jsp";
  	}
  	
  </script>
  
  <body>
    <form action="" method="post">
    	<input type="text" name="username"><br/>
    	<input type="password" name="password"><br/>
    	<input type="text" name="code">
    	<img alt="验证码" src="/java_Code/servlet/code">
    	<a href="javascript:toreal()">看不清</a>
    	<br/>
    	<input type="submit" value="登录"><br/><!-- 内部自动发送请求,加载验证码 -->
    	
    </form>
  </body>
</html>

servlet页面生成验证码的图片代码:

package com.dp.java.code;

import java.awt.Color;
import java.awt.Font;
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 code extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//通知浏览器不要缓存
		response.setHeader("Expires", "-1");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "-1");
		
		int height=25;
		int width=120;
		//得到一个内存图像BufferedImage
		BufferedImage img=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		//得到一个画笔
		Graphics g=img.getGraphics();
		//画边框drawRect绘制指定矩形的边框。
		g.drawRect(0, 0, width, height);
		//填充颜色
		g.setColor(Color.RED);
		g.fillRect(1, 1, width-2, height-2);
		//画干扰线
		g.setColor(Color.BLACK);
		Random r=new Random();
		for(int i=0;i<20;i++)
		g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
		//生成随机数字
		g.setColor(Color.BLUE);
		g.setFont(new Font("微软雅黑", Font.BOLD|Font.ITALIC, 20));//BOLD加粗,ITALIC斜体
		int d=15;
		for(int j=0;j<4;j++){
			g.drawString(r.nextInt(10)+"", d, 20);
			d+=20;
		}
		            		
		//输出打web页面
		ImageIO.write(img, "jpg", response.getOutputStream());
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

最后是xml映射文件:

<?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">
  <display-name>java_Code</display-name>
  <servlet>
    <servlet-name>code</servlet-name>
    <servlet-class>com.dp.java.code.code</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>code</servlet-name>
    <url-pattern>/servlet/code</url-pattern>
  </servlet-mapping>
 
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

结果:

                    



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值