javaweb:网页登录案例(用户名密码+验证码)
javaweb:⽹页登录案例(⽤户名密码+验证码)
实现⽤户登录验证功能:
(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 ><%=Attribute("login_error")== null ? "": Attribute("login_error") %></td> </tr>
<tr>
<td>密码</td>
<td><input type="text"name="password"></td>
<td ><%=Attribute("login_error")== null ? "": Attribute("login_error") %></td> </tr>
<tr>
<td>验证码</td>
<td><input type="text"name="inputCode"></td>
<td ><%=Attribute("cc_error")== null ? "": Attribute("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><%=Attribute("cc_error")== null ? "": Attribute("cc_error")%></div>--%>
<%--<div><%=Attribute("login_error")== null ? "": Attribute("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 Int(5)];
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/jpeg");
OutputStream os = OutputStream();
int width =83, height =30;
//建⽴指定宽、⾼和BufferedImage对象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics g = Graphics();//将画笔绘制于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(Int(length)]).toString();//随机⽣成0~9的数字
sRand += rand;
//设置随机颜⾊
g.setColor(new Int(255), Int(255), Int(255)));
g.drawString(rand, 20*i+6, 25);
}
//保存⽣成的验证码到session中
//产⽣随机⼲扰点
for(int i=0; i<20; i++){
int x1 = Int(width);
int y1 = Int(height);
g.drawOval(x1, y1, 2, 2);
}
g.setColor(c);//重置画笔颜⾊
g.dispose();//释放此图形的上下⽂及使⽤的所有系统资源
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 Parameter("username");
String Parameter("password");
String Parameter("inputCode");
// 2.获取⽣成的验证码
String login_code=(String) Session().getAttribute("check_code");
// 3.⽐较验证码
if(login_code.equalsIgnoreCase(inputCode)==true){
//验证码正确
//⽐较⽤户名是否正确
if(user.equals("huangjie")&&password.equals("123456")){
//⽤户信息正确
鹿鹿鱼鱼形容什么
//存储信息,⽤户信息
/
/⽤于测试的输出语句
System.out.println("1111111111111111");
//response.ContextPath()+"/success.jsp");
}
else{
//⽤户信息错误
//⽤于测试的输出语句
//System.out.println("22222222222");
request.setAttribute("login_error","⽤户名或密码错误");
}
}
else{
//验证码错误
//⽤于测试的输出语句
System.out.println("33333333333");
request.setAttribute("cc_error","验证码错误");
}
}
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) Session().getAttribute("name").toString(); %>
<h1>hello,<%=username%>欢迎回来!</h1>
</body>
</html>
验证码错误提⽰:
⽤户名或密码错误提⽰:
登录成功界⾯显⽰:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。