JavaWeb登陆验证码

本测试通过简单servlet实现

页面JS:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function refershCheckCode(){
		var image=document.getElementById("check");
		image.src="checkCodeServlet?time="+new Date().getTime();
	}
	<%
		if(request.getParameter("code")!=null){
			out.println("alert('验证码错误')");
		}
	%>
</script>
</head>
<body>
	<div style="position:absolute; width:300px;left:50%;margin-left:-150px;top:200px;">
		<form action="loginServlet">
			<table width="100%" cellpadding="6">
				<tr>
					<td>账号</td>
					<td>
						<input type="text" name="userName" style="width:170px">
					</td>
				</tr>
				<tr>
					<td>密码</td>
					<td>
						<input type="password" name="userPwd" style="width:170px">
					</td>
				</tr>
				<tr>
					<td>验证码</td>
					<td>
						<input type="text" name="inputCode" style="width:80px">
						<a href="javascript:refershCheckCode()"><img id="check" style="position: absolute;margin-left:10px"alt="" src="checkCodeServlet"></a>
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="submit" value="提交">
						<input type="reset" value="取消">
					</td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>
验证码Servlet

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
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 CheckCodeServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int width=80,height=22;
		//创建一个图片对象
		BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		//得到图片画布
		Graphics g=image.getGraphics();
		Random r = new Random();
		//通过画布对象来绘制当前图片
		//填充一个矩形
		g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
		g.fillRect(0, 0, width, height);
		
		//绘制干扰线
		for(int i=0;i<4;i++){
			g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
			g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
		}
		
		String base="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String code="";
		String code1="";
		for(int i=0;i<4;i++){
			int index=r.nextInt(base.length());
			if(i==3){
				code+=base.charAt(index)+"";
			}else{
				code+=base.charAt(index)+" ";
			}
			code1+=base.charAt(index)+"";
		}
		
		request.getSession().setAttribute("checkCode", code1);
		g.setFont(new Font("宋体",Font.BOLD,20));
		g.drawString(code, 2, 20);
		
		ImageIO.write(image, "JPEG", response.getOutputStream());
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
参数传值

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String inputCode = request.getParameter("inputCode");
		String checkCode = request.getSession().getAttribute("checkCode").toString();
		if(inputCode.equalsIgnoreCase(checkCode)){
			response.sendRedirect("index.jsp");
		}else{
			response.sendRedirect("login.jsp?code=0");
		}
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值