- com.stu.demo.Entity
package com.stu.com.stu.entity;
public class User {
private String uid;
private String pwd;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"uid='" + uid + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (uid != null ? !uid.equals(user.uid) : user.uid != null) return false;
return pwd != null ? pwd.equals(user.pwd) : user.pwd == null;
}
@Override
public int hashCode() {
int result = uid != null ? uid.hashCode() : 0;
result = 31 * result + (pwd != null ? pwd.hashCode() : 0);
return result;
}
}
- com.stu.demo.Login
package com.stu.demo;
import com.stu.com.stu.entity.User;
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;
import java.io.PrintWriter;
@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
// 读取session
User user = (User) session.getAttribute("user");
if (user != null) {
System.out.println(user);
out.print("<h1>已经登陆</h1><br>");
out.print("<a href='" + request.getContextPath() + "/doLogin'>退出登陆</a>");
} else {
out.print("<form action='/doLogin' method='post'>");
out.print("<label>账号:<input type='text' placeholder='账号' name='uid'></label><br>");
out.print("<label>密码:<input type='password' placeholder='密码' name='pwd'></label><br>");
out.print("<button>登陆</button><br>");
out.print("</form>");
}
}
}
- com.stu.demo.DoLogin
package com.stu.demo;
import com.stu.com.stu.entity.User;
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;
import java.io.PrintWriter;
@WebServlet(name = "DoLoginServlet")
public class DoLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
// 获取账号密码
String uid = request.getParameter("uid");
String pwd = request.getParameter("pwd");
if (uid.equals("123") && pwd.equals("321")) {
// 登陆成功,将用户信息保存到session
User user = new User();
user.setUid(uid);
user.setPwd(pwd);
session.setAttribute("user", user);
session.setMaxInactiveInterval(60 * 15);
out.print("<h1>登陆成功</h1>");
} else {
// 登陆失败
out.print("<h1>账号或密码不匹配</h1>");
response.setHeader("refresh", "2;url=" + request.getContextPath() + "/login");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
// 清除session
session.removeAttribute("user");
// 或使用(删除整个session)
// session.invalidate();
out.print("<h1>退出成功</h1>");
response.setHeader("refresh", "2;url=" + request.getContextPath() + "/login");
}
}