1、Servlet:
package Servlet;
import org.apache.tools.ant.taskdefs.condition.Http;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//创建一张图片
/**
* 宽200像素,高100像素,TYPE_INT_RGB类型的图片
*/
BufferedImage bi = new BufferedImage(200,100,BufferedImage.TYPE_INT_RGB);
//创建画板
Graphics2D graphics = bi.createGraphics();
//填充画板颜色
graphics.setColor(Color.gray);
graphics.fillRect(0,0,200,100);
//生成随机数
graphics.setFont(new Font("微软雅黑",Font.BOLD|Font.ITALIC,45));
Color[] strColor = {Color.CYAN,Color.GREEN,Color.LIGHT_GRAY,Color.ORANGE,Color.BLUE,Color.RED};
//验证码
List<String> list = new ArrayList<>();
for (int i = 0;i<4;i++){
float r = (float)Math.random(); //取值范围0~0.99999....
int round = Math.round(r*10); //整数的向下取整
list.add(""+round);
int color = new Random().nextInt(strColor.length);
graphics.setColor(strColor[color]);
graphics.drawString(round+"",i*50+5,60+(Math.round(r*21) - 10));
}
//画出验证码的干扰线
graphics.setColor(Color.CYAN);
graphics.drawLine(0,80,160,10);
graphics.setColor(Color.LIGHT_GRAY);
graphics.drawLine(0,8,160,90);
//获取请求的字节输出流,使用ImageIO的write方法写出操作
//此处的代码决定了将创建的图片写出到浏览器客户端
ServletOutputStream outputStream = res.getOutputStream();
ImageIO.write(bi,"png",outputStream);
//将验证码存入到session中
HttpSession session = req.getSession();
session.setAttribute("verCode",list.get(0)+list.get(1)+list.get(2)+list.get(3));
}
}
2、JSP:
<%--
Created by IntelliJ IDEA.
User: xingfuhu
Date: 2020/4/14
Time: 23:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>用户登录页面</title>
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("a").click(function(){
$("img").attr("src","login?date="+new Date());
return false;
});
})
</script>
</head>
<body>
<h5>用户登录</h5>
<form action="login" method="post">
<table border="1">
<tr>
<td align="center" style="font-size: 20px;">登录名:</td>
<td colspan="2"><input type="text" align="left" style="font-size: 16px;"/></td>
</tr>
<tr>
<td align="center" style="font-size: 20px;">登录密码:</td>
<td colspan="2"><input type="text" align="left" style="font-size: 16px;"/></td>
</tr>
<tr>
<td align="center" style="font-size: 20px;">
验证码:
</td>
<td><input type="text" align="left" style="font-size: 16px;"/></td>
<td align="center" style="text-align: center;width: 120px">
<img src="login" style="height: 30px;width: 60px;"/><a id="see" style="font-size: 20px;">看不清</a>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" align="center" style="font-size: 25px;">
<input type="reset" align="center" style="font-size: 25px;">
</td>
</tr>
</table>
</form>
</body>
</html>