您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
24_案例_验证码_代码实现(equalsIgnoreCase在比较字符时可以忽略大小写)
发布时间:2022-09-04 17:50:21编辑:雪饮阅读()
可以直接使用学过的session知识来实现验证码的基于session的具体校验。equalsIgnoreCase比较不错,可以不用单独将两个比较的字符串转换为统一的大写或小写,而是直接比较时候忽略大小写。但比php简单吗?未必哦。
登录页面login.jsp:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/9/4
Time: 0:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/ServletLogin" method="post" style="display: flex;justify-content: center">
<div>
<div style="display: flex;justify-items: center;">
<div style="display: flex; flex-direction: column;justify-content: center;">
<label>验证码:</label>
</div>
<div style="display: flex;justify-items: center;">
<input name="code"/> <img src="<%=request.getContextPath()%>/ServletImgCode" style="height: 100%;"/>
</div>
</div>
<div style="color:red"><%=request.getAttribute("login_err")%></div>
<div style="display: flex;justify-content: center">
<button type="submit">提交</button>
</div>
</div>
</form>
</body>
</html>
登录成功页面LoginSuccess.jsp:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/9/4
Time: 0:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
登录成功
</body>
</html>
所需要的包含session存储的验证码图片ServletImgCode.java:
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;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
@WebServlet("/ServletImgCode")
public class ServletImgCode extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width=100;
int height=50;
//创建一个在内存中的图片对象
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//若仅仅是图像对象,不对该对象进行其它绘制操作,则默认是黑色背景的一个图片哈
//所以咱们要填充下
//拿到画笔对象
Graphics g=image.getGraphics();
//设置画笔颜色为粉色
g.setColor(Color.PINK);
//绘制一个矩形出来(填充一个矩形出来)
g.fillRect(0,0,width,height);
//然后咱们再画一个边框吧(空矩形罢了)
g.setColor(Color.YELLOW);
g.drawRect(0,0,width-1,height-1);
//验证码字符池
String str="xyKsumiAyaniMomiji";
Random ran=new Random();
g.setColor(Color.WHITE);
//生成4位字符的验证码
String realy_code="";
for (int i = 1; i <=4 ; i++) {
//Random类的nextInt可以获取一个指定大于等于0,但小于等于其入参值(数字类型)的随机数
int index=ran.nextInt(str.length());
char ch=str.charAt(index);
realy_code+=ch;
//写字 字,字的x偏移,字的y偏移
g.drawString(ch+"",width/5*i,height/2);
}
//然后也画上随机干扰线哈,这里假定画4条吧
g.setColor(Color.BLACK);
for (int i = 0; i <4 ; i++) {
int x1=ran.nextInt(width);
int x2=ran.nextInt(width);
int y1=ran.nextInt(height);
int y2=ran.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}
request.getSession().setAttribute("img_code",realy_code);
ImageIO.write(image,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
具体的登录逻辑ServletLogin.java:
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/ServletLogin")
public class ServletLogin extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Boolean CodeCheck=request.getParameter("code").equalsIgnoreCase((String) request.getSession().getAttribute("img_code"));
if(CodeCheck){
request.getRequestDispatcher("/LoginSuccess.jsp").forward(request,response);
}
else{
//验证码不正确
request.setAttribute("login_err","验证码不正确");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
最后访问如http://localhost:8080/session20_war_exploded/ServletLogin进行测试
关键字词:案例,验证码,代码,实现,equalsIgnoreCase,比较,字符,忽略,大小