servlet简单的验证码登录验证

package com.bjsxt.check;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;<pre name="code" class="java">package com.bjsxt.check;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Check extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String checkCode = request.getParameter("checkCode");
		String name = request.getParameter("uname");
		String pwd = request.getParameter("pwd");
		
		String checkCode1 = (String) request.getSession().getAttribute("checkCode");
		//验证生成的验证码和用户输入的验证码是否一致,一致则登陆成功,否则失败
		if(checkCode!=null&&checkCode.equals(checkCode1)){
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/checkcode.jsp").forward(request, response);
		}

	}

}

<%@ 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 'checkcode.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" src="js/jquery-1.9.1.min.js"></script>
  <script type="text/javascript">
  	$(function(){
  		$("#check").click(function(){
  			$("#check").attr("src",null);
  			$("#check").attr("src","/checkcode_03/checkCode?"+Math.random());
  		});
  		$("#change").click(function(){
  			$("#check").attr("src",null);
  			$("#check").attr("src","/checkcode_03/checkCode?"+Math.random());
  			return false;
  		});
  	});
  
  
  </script>
  
  <body>
  	<form action="check" method="post">
  		<table>
  			<tr>
				<th>用户名</th><td><input type="text" value="" name="uname"/></td>			
  			</tr>
  			<tr>
				<th>密码</th><td><input type="password" value="" name="pwd"/></td>			
  			</tr>
  			<tr>
				<th>验证码</th><td><input type="text" value="" name="checkCode"/></td>			
  			</tr>
  			<tr>
				<th colspan="2"><img alt="验证码" src="/checkcode_03/checkCode" id="check"><a href="#" id="change" >看不清,换一张</a></th>			
  			</tr>
  			<tr>
				<th colspan="2"><input type="submit" value="提交"/>     <input type="reset" value="重置"/></th>			
  			</tr>
  		</table>
  	</form>
  </body>
</html>

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStreamImpl;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class CheckCode extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final int WIDTH = 100;
	private static final int HEIGHT = 30;
	private char[] checkCode = new char[4];
	private Random random = new Random();
	// 随机产生二维码字符四个
	public char[] getCheckCode() {
		String str = "abcdefghijklmnopqrstuvwxyz0123456789";
		for (int i = 0; i < 4; i++) {
			int index = random.nextInt(str.length());
			checkCode[i] = str.charAt(index);
			}
		return checkCode;}
	// 设置图片背景
	public void setBackGround(Graphics g) {
		// 设置背景色
		g.setColor(Color.GRAY);
		// 填充背景色
		g.fillRect(0, 0, WIDTH, HEIGHT);
		for (int i = 0; i < 120; i++) {
			// 设置干扰点颜色
			g.setColor(new Color(random.nextInt(255), random.nextInt(255),random.nextInt(255)));
			// 设置干扰点
			g.drawOval(random.nextInt(WIDTH), random.nextInt(HEIGHT), 1, 0);}
		}
	// 将验证码写在图片上
	public void DrawFont(Graphics g, char[] checkCode) {
		for (int i = 0; i < checkCode.length; i++) {
			g.setColor(new Color(random.nextInt(255), random.nextInt(255),random.nextInt(255)));
			g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 30));g.drawString("" + checkCode[i], (int) (Math.random() + 25 * i),(int) (Math.random() * 5 + 27));
			}
		}
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		response.setContentType("image/jpeg");HttpSession session = request.getSession();
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		char[] checkCode = getCheckCode();
		setBackGround(g);
		DrawFont(g, checkCode);
		g.dispose();
		ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
		ImageIO.write(image, "JPEG", arrayOutputStream);
		byte[] buf = arrayOutputStream.toByteArray();
		ServletOutputStream sos = response.getOutputStream();
		sos.write(buf);response.setContentLength(buf.length);
		// 关闭各种流释放资源
		arrayOutputStream.close();
		sos.close();
		session.setAttribute("checkCode", new String(checkCode));
		}
	}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值