Javaweb-登录界面-filter配合案例-2023-04-11

1、新建login.jsp 登录界面 响应的路径


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
</head>
<body>

<h1>登录界面</h1>
<hr>
<form action="/servlet/login" method="post">
    <input type="text" name="username">
    <input type="submit">
</form>

</body>
</html>

2、写对应的loginServlet

package com.feijian.servlet;

import com.feijian.util.constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        if (username.equals("admin")){
            req.getSession().setAttribute(constant.USER_SESSION,req.getSession().getId());
            resp.sendRedirect("/sys/success.jsp");
        }else {
            resp.sendRedirect("/sys/error.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

3、设置sucess.jsp,和error.jsp页面。

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/4/11
  Time: 20:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主页</title>
</head>
<body>

<%--<%--%>
<%--    Object userName = request.getSession().getAttribute("USER_NAME");--%>
<%--    if (userName==null){--%>
<%--        response.sendRedirect("/login.jsp");--%>
<%--    }--%>
<%--%>--%>
<hr>
<br>
<h1>登录成功!</h1> <br>
<p><a href="/servlet/logout">注销</a></p>
<hr>
<br>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/4/11
  Time: 20:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>错误界面</title>
</head>
<body>
<h3><span style="color: red">错误页面,登录不成功!</span></h3>
<a href="/login.jsp">返回登录界面</a>
</body>
</html>

4、再写一个logoutServlet

package com.feijian.servlet;

import com.feijian.util.constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object user_session = req.getSession().getAttribute(constant.USER_SESSION);
        if (user_session!=null){
            req.getSession().removeAttribute(constant.USER_SESSION);
            resp.sendRedirect("/login.jsp");
        }else {
            resp.sendRedirect("/login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

5、写一个Filter类

package com.feijian.filter;

import com.feijian.util.constant;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class sysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        if (request.getSession().getAttribute(constant.USER_SESSION)==null){
            response.sendRedirect("/error.jsp");
        }
        chain.doFilter(req,resp);
    }

    @Override
    public void destroy() {

    }
}

6、在web.xml中注册 三个类,loginServlet, logoutServlet, filter 类 并在映射路径上和前端页面上的一致。

注意:传入的值建议写入Session,这样取值即使页面跳转,只要浏览器不关闭就一直存在,不会丢失,如果用request 在重定向的情况下打开新页面就无法页面间传值。

posted @ 2023-04-11 22:00  Rui2022  阅读(21)  评论(0编辑  收藏  举报