目录结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bc6ff8f1d550430e4dc304af9d41daa8.png)
流程图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/61e91d534f207f23ec22ae4aeed90b5a.png)
LoginServlet
package com.itheima.web.servlet.session;
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.*;
@WebServlet(urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html;charset=utf-8");
String vc = request.getParameter("verifyCode");
String code_session = (String) request.getSession().getAttribute("code_session");
if(code_session.equalsIgnoreCase(vc)){
String username = request.getParameter("username");
String password = request.getParameter("password");
boolean flag=false;
BufferedReader br = new BufferedReader(new FileReader("d:/stu.txt"));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
String uname = line.split("\\,")[0];
System.out.println(uname);
String pword = line.split("\\,")[1];
System.out.println(pword);
if(username.equals(uname)&&pword.equals(password)){
flag=true;
}
}
if(flag){
request.getSession().setAttribute("username",username);
response.sendRedirect(request.getContextPath()+"/success.html");
}
else{
response.sendRedirect(request.getContextPath()+"/fail.html");
}
}else{
response.getWriter().write("<h1 style='color:red'>验证码校验失败!</h1>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
request.setCharacterEncoding("utf-8");
doGet(request, response);
}
}
CheckcodeServlet(生成验证码)
package com.itheima.web.servlet.session;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
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("/checkcodeServlet")
public class CheckcodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width = 120;
int height = 40;
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.red);
g.drawRect(0, 0, width - 1, height - 1);
String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
Random r = new Random();
String code = "";
for (int i = 0; i < 4; i++) {
g.setFont(new Font("宋体", Font.BOLD, 28));
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
String str = data.charAt(r.nextInt(data.length())) + "";
g.drawString(str, 10 + i * 28, 30);
code = code + str;
}
for (int i = 0; i < 6; i++) {
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
}
System.out.println(code);
request.getSession().setAttribute("code_session", code);
ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
ShowPriServlet(用来校验用户是否是在登陆的时候进入的success.html界面中,并且查看已有的权限)
package com.itheima.web.servlet.session;
import javax.servlet.RequestDispatcher;
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(urlPatterns = "/ShowPriServlet")
public class ShowPriServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html;charset=utf-8");
String username = (String)request.getSession().getAttribute("username");
if(username!=null){
request.getRequestDispatcher("/WEB-INF/pri.html").forward(request,response);
}else{
response.getWriter().write("<h2 style='color:red'>你还未登陆,无法查看已有权限,请先<a href='/day24_cookie/login.html'>登录 </a></h2>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
request.setCharacterEncoding("utf-8");
doGet(request, response);
}
}
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<script>
window.onload=function () {
document.querySelectorAll("img")[0].onclick=function () {
this.src="/day24_cookie/checkcodeServlet?time="+new Date().getTime();
}
}
</script>
</head>
<body>
<form action="/day24_cookie/LoginServlet" method="get">
用户名:<input type="text" name="username"><br/>
密 码:<input type="password" name="password"><br/>
验证码:<input type="text" name="verifyCode"> <!--验证码一般是一个图片-->
<img src="/day24_cookie/checkcodeServlet">
<br/>
<input type="submit" value="登录">
</form>
</body>
</html>
success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录成功页面</title>
</head>
<body>
<h2 style="color: red">恭喜您,登陆成功!</h2>
<a href="/day24_cookie/ShowPriServlet">查看我的权限</a>
</body>
</html>
fail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>失败提示页</title>
</head>
<body>
<h2 style="color:red;">用户名或密码错误</h2>
<a href="login.html">重新登陆</a>
</body>
</html>
pri.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的权限</title>
</head>
<body>
<a href="#">查看学生信息</a><br/>
<a href="#">修改学生信息</a><br/>
<a href="#">添加学生信息</a><br/>
<a href="#">删除学生信息</a><br/>
</body>
</html>
查看已有权限功能没有实现。