JAVA基础14-JavaWeb(三十)验证码

(1)randomCodeServlet

package com.suning.randomCode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.Provider.Service;
import java.util.Random;
import java.util.UUID;

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;

@WebServlet("/randomCode")
public class randomCodeServlet extends HttpServlet{
	
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//1:生成验证码随机数
		String randomCode = UUID.randomUUID().randomUUID().toString().substring(0, 5);
		//2:把随机数存储到session中
		req.getSession().setAttribute("RANDOMCODE_IN_SESSION", randomCode);
		//3:创建图片对象
		int width = 80 ;
		int height = 40 ;
		int imageType = BufferedImage.TYPE_INT_BGR;
		BufferedImage image = new BufferedImage(width, height, imageType);
		//4:创建画板
		Graphics g = image.getGraphics();
		g.setColor(Color.yellow);
		//5:绘制实心的矩形
		g.fillRect(1, 1, width - 2 , height - 2);
		//6:将随机数添加到图片中
		g.setColor(Color.black);//设置随机数的颜色
		Font font = new Font("宋体", Font.BOLD + Font.ITALIC,20);
		g.setFont(font);
		g.drawString(randomCode, 10, 20);
		//7:设置干扰线
		g.setColor(Color.GRAY);
		Random r = new Random();
		for(int i = 0; i < 100 ; i ++ ){
			g.fillRect(r.nextInt(width), r.nextInt(height), 2, 2);
		}
		//8:关闭
		g.dispose();
		//9:把图片对象以流的方式保存出去
		ImageIO.write(image, "jpg", resp.getOutputStream());
	}
}

randomCodeLoginServlet

package com.suning.randomCode;

import java.io.IOException;

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

@WebServlet("/radnomLogin")
public class randomCodeLoginServlet extends HttpServlet{
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//1、接受请求参数
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String randomCode = req.getParameter("randomcode");
		//2、获取session中的随机数
		String randomCodeInSession = (String)req.getSession().getAttribute("RANDOMCODE_IN_SESSION");
		if(!randomCode.equalsIgnoreCase(randomCodeInSession)){
			req.setAttribute("errorMsg", "验证码不正确过期");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
			return;
		}
		req.getSession().removeAttribute("RANDOMCODE_IN_SESSION");
	}
}

login.jsp

<%@ 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>苏宁易购</title>
	<script type="text/javascript">
		function change(){
			//重新设置img元素的属性
			document.getElementById("randomCodeIMG").src="/randomCode"+new Date().getTime();
		}
	</script>
</head>
<body>
	<h3>用户登陆</h3>
	<span style="color:red;">${errorMsg}</span>
	<form action="/radnomLogin">
		账&emsp;户<input type="text" name="useranme" required/><br>
		密&emsp;码<input type="text" name="password" required/><br>
		验证码:<input type="text" name="randomcode" size="5" maxlength="5" />
				<img src="/randomCode" id="randomCodeIMG" title="看不清,换一种"
				style="cursor: pointer;" onclick="change();"/><br>
		<input type="submit" vlue="我要登陆了"/>
	</form>
	
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值