@WebServlet("/indexServlet") //使用注解方式提供资源路径
public class god1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(false); //判断有没有session没有的话就跳转到登录界面
if (session==null){
resp.sendRedirect("login.html"); //前端登录界面
}
}
这里我们先实现没有账号登录,我们首次登录账号情况功能实现
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form method="post" action="login"> //提交表单后将url请求传输到该资源路径
用户名 <input type="text" name="username">
<br>
密码 <input type="password" name="password">
<br>
<input type="submit" value="登录">
form>
body>
html>
注意:前端表单的提交地址为"login"资源路径
@WebServlet("/login")
public class god2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username"); //通过HttpServletRequest 对象的getParameter方法
String password = req.getParameter("password");
HttpSession session = req.getSession(true); //强制创建一个session
if(username.equals("god")&&password.equals("skr")){ //(判断用户名和密码是否正确)这里我用的是固定的用户名和密码
session.setAttribute("username","god"); //(给session添加对象属性,便于待会判断是否自动登录)
resp.sendRedirect("index"); //重定向,跳转到index资源路径,也就是我们项目的主界面
}else {
resp.setStatus(403); //设置错误信息
resp.setContentType("text/html; charset=utf8"); //设置返回的文字编码格式
resp.getWriter().write("登陆失败, 用户名或者密码错误");//提示词
}
}
}
@WebServlet("/index")
public class god3 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(false); //获取session对象
if(session == null){ //如果这个session为null,说明他的attribute里面没有对象属性
resp.setStatus(403); //报错
resp.setContentType("text/html; charset=utf8");
resp.getWriter().write("您尚未登录, 不能访问主页!");
return;
}
String username = (String)session.getAttribute("username"); //获取登录用户名
resp.setContentType("text/html; charset=utf8");
resp.getWriter().write("欢迎来到主页! " + username); //设置登录成功提示
String html = "" +
"" +
"" +
" " +
" 主界面 " +
" " +
"" +
"" +
"+
""+
""+
"" ;
resp.getWriter().append(html);
}
}
最后面那段html代码是一个退出按钮,也就是退出当前登录的账号
退出功能不是简单的关闭页面,同时要清除缓存的session和cookie值
代码如下
@WebServlet("/logout")
public class god4 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(false);
if (session != null) {
session.invalidate(); // 销毁 Session
Cookie cookie = new Cookie("JSESSIONID", ""); // 同时销毁 浏览器的 Cookie 数据
cookie.setMaxAge(0);
resp.addCookie(cookie);
}
resp.sendRedirect("login.html"); //重定向到登录界面
}
}
还是我们的第一个用户请求资源路径indexServlet中加一些代码
@WebServlet("/indexServlet")
public class god1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(false); //判断有没有session没有的话就跳转到登录界面
if (session==null){
resp.sendRedirect("login.html");
}else {
String username = (String) session.getAttribute("username"); //这里是验证我们的session的对象属性中是否成功存储了god账号
if (username.equals("god")){
resp.sendRedirect("index"); //验证成功直接跳转到项目主界面,不用登录
}
}
}
}
运行tomcat,进入资源路径
浏览器进入 http://localhost:8080/indexServlet
注意地址栏,资源路径改变,说明当前god账号没有登录,重定向成功
输入我们的账号密码 god skr
登录成功
在god账号登录的同时 我们在打开一个网页进入http://localhost:8080/indexServlet
你会发现你不同登录,直接就进入到主界面了
点击退出按钮
重定向到登录界面
此时我们再回到第一次打开的项目主界面(index资源路径)
因为我们把god账号的session和cookie信息抹除了,god账号的所有页面都不能用了