(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">
账 户<input type="text" name="useranme" required/><br>
密 码<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>