十三、過濾器,監聽器的常見應用
阿新 • • 發佈:2022-12-04
十三、過濾器,監聽器的常見應用
-
使用者登入之後,向Session中放入使用者的資料
-
進入主頁面的時候要判斷使用者是否已經登入;要求:在過濾器中實現
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; if (request.getSession().getAttribute(Constant.USER_SESSION) == null) { response.sendRedirect("/error.jsp"); } filterChain.doFilter(request,response); }
小專案程式碼實現:
-
編寫工具類
package com.lyh.util; public class Constant { public final static String USER_SESSION = "USER_SESSION"; }
-
寫登入頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>登入頁面</h1> <form action="/servlet/login" method="post"> <input type="text" name="username"> <input type="submit"> </form> </body> </html>
-
寫登入頁面的servlet(LoginServlet)
package com.lyh.servlet; import com.lyh.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"); //如果你輸入admin就會登入成功 if (username.equals("admin")){ req.getSession().setAttribute(Constant.USER_SESSION,req.getSession().getId()); resp.sendRedirect("/sys/success.jsp"); }else {//登入失敗 resp.sendRedirect("/error.jsp"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
-
在web.xml 中註冊servlet映象
<!-- 當/servlet/*路徑下的所以執行的伺服器都會被過濾--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>com.lyh.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/servlet/*</url-pattern> </filter-mapping> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.lyh.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/servlet/login</url-pattern> </servlet-mapping>
-
登入成功,登入失敗,跳轉頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>登入成功</h1> <a href="/servlet/logout">登出</a> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>錯誤</h1> <a href="/Login.jsp">返回登入頁面</a> </body> </html>
-
登出會有一個按鈕(對應一個Servlet,編寫登出的Servlet為LogoutServlet)
package com.lyh.servlet; import com.lyh.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 { //這時我們還有會話記錄 if (req.getSession().getAttribute(Constant.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); } }
-
登出的servle(LogoutServlet)t,也在web.xml 中註冊一下
<servlet> <servlet-name>LogoutServlet</servlet-name> <servlet-class>com.lyh.servlet.LogoutServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LogoutServlet</servlet-name> <url-pattern>/servlet/logout</url-pattern> </servlet-mapping>
-
成功頁面中登出點選就跳轉到Login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>登入成功</h1> <a href="/servlet/logout">登出</a> </body> </html>
-
寫過濾器Filter
package com.lyh.filter; import com.lyh.util.Constant; import javax.servlet.*; 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 servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; if (request.getSession().getAttribute(Constant.USER_SESSION) == null) { response.sendRedirect("/error.jsp"); } filterChain.doFilter(request,response); } @Override public void destroy() { } }
-
在web,xml中註冊過濾器
<filter> <filter-name>SysFilter</filter-name> <filter-class>com.lyh.filter.SysFilter</filter-class> </filter> <filter-mapping> <filter-name>SysFilter</filter-name> <url-pattern>/sys/*</url-pattern> </filter-mapping>
-
補充一點:我們這個過濾器是過濾頁面的,sys這個包下面的頁面都會被我們的過濾器過濾一遍