实现用户登录验证功能:
(1)登录页面包含用户名、密码、验证码功能。
(2)用户提交后由 servlet 处理;如果验证成功进入欢迎页面;否则跳转至登录页面, 并给出提示错误!
(3)验证码功能由单独的 servlet 实现。
完整代码如下:
1、login.jsp
<%--
Created by IntelliJ IDEA.
User: manchi520
Date: 2021/11/21
Time: 20:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<form action="/loginServlet" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"></td>
<td style="color: red"><%=request.getAttribute("login_error") == null ? "" : request.getAttribute("login_error") %></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"></td>
<td style="color: red"><%=request.getAttribute("login_error") == null ? "" : request.getAttribute("login_error") %></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="inputCode"></td>
<td style="color: red"><%=request.getAttribute("cc_error") == null ? "" : request.getAttribute("cc_error") %></td>
</tr>
<tr>
<td colspan="2"><img id="img" src="/checkCodeServlet" alt=""></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
<td><input type="button" value="注册" onclick="reg()" ></td>
</tr>
</table>
</form>
<%--<div><%=request.getAttribute("cc_error") == null ? "" : request.getAttribute("cc_error")%></div>--%>
<%--<div><%=request.getAttribute("login_error") == null ? "" : request.getAttribute("login_error") %></div>--%>
</body>
</html>
2、checkCodeServlet
package servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import javax.imageio.ImageIO;
@WebServlet("/checkCodeServlet")
public class checkCodeServlet extends HttpServlet{
//产生随机的字体
private Font getFont(){
//创建random对象,用于生成随机数
Random random = new Random();
//创建字体数组,用于封装不同字体的Font对象
Font font[] = new Font[5];
font[0] = new Font("Ravie", Font.PLAIN, 24);
font[1] = new Font("Antique Olive Compact", Font.PLAIN, 24);
font[2] = new Font("Forte", Font.PLAIN, 24);
font[3] = new Font("Wide Latin", Font.PLAIN, 24);
font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, 24);
return font[random.nextInt(5)];
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/jpeg");
OutputStream os = response.getOutputStream();
int width = 83, height = 30;
//建立指定宽、高和BufferedImage对象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics g = image.getGraphics();//将画笔绘制于image上
Color c= g.getColor();//保存当前画笔的颜色
g.fillRect(0, 0, width, height);//填充矩形
char[] ch = "abcdefghigklmnopqrstuvwxyz2345678901".toCharArray();//随机产生字符串
int length = ch.length;//随机字符串长度
String sRand = "";//保存随机产生的字符串
Random random = new Random();
for (int i=0; i<4; i++){
g.setFont(getFont());//设置字体
String rand = new Character(ch[random.nextInt(length)]).toString();//随机生成0~9的数字
sRand += rand;
//设置随机颜色
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
g.drawString(rand, 20*i+6, 25);
}
request.getSession().setAttribute("check_code",sRand);
//保存生成的验证码到session中
//产生随机干扰点
for(int i=0; i<20; i++){
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
g.drawOval(x1, y1, 2, 2);
}
g.setColor(c);//重置画笔颜色
g.dispose();//释放此图形的上下文及使用的所有系统资源
request.getSession().setAttribute("check_code", sRand);//验证码记录到session
ImageIO.write(image, "JPEG", os);//输出图像到页面
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
3、loginServlet
package servlet;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import myBean.User;
import java.io.IOException;
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// 1.获取参数
String user=request.getParameter("username");
String password=request.getParameter("password");
String inputCode=request.getParameter("inputCode");
// 2.获取生成的验证码
String login_code=(String) request.getSession().getAttribute("check_code");
request.getSession().removeAttribute("check_code");
// 3.比较验证码
if(login_code.equalsIgnoreCase(inputCode)==true){
//验证码正确
//比较用户名是否正确
if(user.equals("huangjie")&&password.equals("123456")){
//用户信息正确
//存储信息,用户信息
request.getSession().setAttribute("name",user);
//用于测试的输出语句
System.out.println("1111111111111111");
//response.sendRedirect(request.getContextPath()+"/success.jsp");
request.getRequestDispatcher("success.jsp").forward(request,response);
}
else {
//用户信息错误
//用于测试的输出语句
//System.out.println("22222222222");
request.setAttribute("login_error","用户名或密码错误");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
else {
//验证码错误
//用于测试的输出语句
System.out.println("33333333333");
request.setAttribute("cc_error","验证码错误");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
4、success.jsp
<%--
Created by IntelliJ IDEA.
User: manchi520
Date: 2021/11/21
Time: 20:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>success</title>
</head>
<body>
<%
String username = (String) request.getSession().getAttribute("name").toString();
%>
<h1>hello,<%=username%> 欢迎回来!</h1>
</body>
</html>
验证码错误提示:
用户名或密码错误提示:
登录成功界面显示: