首先需要一个表单提交的JSP页面,或者HTML页面也行,这个界面要放在你的Web目录下才可以被识别到,并可以正常的访问,不要和Servlet一样放在src目录下
也不是不可以,但是访问的时候会有诸多麻烦,不如直接放在Web目录下
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/10/17
Time: 19:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆界面</title>
</head>
<body>
<form action="<c:url value="/LoginServlet"/>">
<table>
<tr>
<td>用户名:</td><td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td><td><input type="password" name="password"></td>
</tr>
</table>
<button type="submit">提交</button>
</form>
</body>
</html>
然后是一个封装了用户信息的用户类
package Semester_3.Login;
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
一个进行登录验证逻辑的Servlet类
package Semester_3.Login;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "LoginServlet" , value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
if(username.equals("张三") && password.equals("123")){
User user = new User();
user.setPassword(password);
user.setUsername(username);
HttpSession session = req.getSession();
session.setAttribute("User",user);
resp.sendRedirect("/indexServlet");
}else {
resp.getWriter().write("密码错误,登陆失败!"+"<br>");
resp.getWriter().write("<a href='/Login.jsp'>登录</a>");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
一个用来进行登陆后的展示界的Servlet类
package Semester_3.Login;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet(name = "indexServlet" , value = "/indexServlet")
public class indexServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
User user = (User) session.getAttribute("User");
if(user == null){
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("您还没有登录,请<a href='/Login.jsp'>登录</a>");
}else {
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("您好,"+user.getUsername()+"!"+"<br>");
resp.getWriter().write("<a href='/LoginOutServlet'>退出</a>");
Cookie cookie = new Cookie("JSESSIONID",session.getId());
cookie.setMaxAge(60*30);
cookie.setPath("/*");
resp.addCookie(cookie);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
一个用来将用户登陆信息清除的退出登录类
package Semester_3.Login;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "LoginOutServlet" , value = "/LoginOutServlet")
public class LoginOutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("User");
resp.sendRedirect("/indexServlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
具体实现
类的介绍
indexServle
indexServlet类用来展示登陆后的界面,或提示没有登陆
LoginServlet
用来判断输入的用户名和密码是否正确,如果正确转发到显示界面,如果错误,则直接输出错误信息到网页
User
用来格式化存储用户信息
LoginOutServlet
用来将用户信息删除并退出登录状态
Login.jsp
一个包含了表单的jsp文件,用来将用户输入的信息传递到Servlet中进行逻辑操作
具体实现
当用户在表单信息中提交登录的相关信息后,会进入到LoginServlet中进行判断,如果信息正确,则重定向到 对应的展示界面,并将用户的输入的信息存储到用户类中进行格式化存储,并将信息传递到一个Cookie中 如果错误,则输出错误信息,登陆成功后,如果选择退出登录,则将用户信息删除,并重定向到用户登录界面