Servlet 過濾器Filter判斷使用者是否登陸(jsp頁面)
阿新 • • 發佈:2019-02-20
過濾器Servlet類:
修改Web核心配置檔案:package servlet; import java.io.IOException; //import java.io.PrintWriter; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; //import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginFilter implements Filter { private static final String LOGON_URI = "LOGON_URI"; private static final String REGISTER_URI = "REGISTER_URI"; private String logon_page; private String register_page; public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; resp.setContentType("text/html;"); resp.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); // PrintWriter out = resp.getWriter(); // 得到使用者請求的URI String request_uri = req.getRequestURI(); // 得到web應用程式的上下文路徑 String ctxPath = req.getContextPath(); // 去除上下文路徑,得到剩餘部分的路徑 String uri = request_uri.substring(ctxPath.length()); // 判斷使用者訪問的是否是登入頁面 if (uri.equals(logon_page) || uri.equals(register_page)) { chain.doFilter(request, response); return; } else { // 如果訪問的不是登入頁面,則判斷使用者是否已經登入 if (null != session.getAttribute("curUser") && "" != session.getAttribute("curUser")) { chain.doFilter(request, response); return; } else { String msg = "您未登入!"; String url = "Errorer"; req.setAttribute("err", msg); RequestDispatcher dis = req.getRequestDispatcher(url); dis.forward(req, resp); } //out.println("<script language=/"javaScript/">" // + "parent.location.href='" + //ctxPath + logon_page + "'" // + "</script>"); // return; // } } } public void init(FilterConfig config) throws ServletException { // TODO Auto-generated method stub // 從部署描述符中獲取登入頁面和首頁的URI logon_page = config.getInitParameter(LOGON_URI); register_page = config.getInitParameter(REGISTER_URI); // System.out.println(logon_page); if (null == logon_page || null == register_page) { throw new ServletException("沒有找到登入頁面或主頁"); } } }
注:儲存後需要重啟tomcat伺服器
Errorer錯誤資訊處理類:<filter> <filter-name>loginFilter</filter-name> <filter-class>servlet.LoginFilter</filter-class> <init-param> <param-name>LOGON_URI</param-name> <param-value>/login.jsp</param-value> </init-param> <init-param> <param-name>REGISTER_URI</param-name> <param-value>/register.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>charseterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>loginFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>
package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Error */ public class Errorer extends HttpServlet { private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String url="login.jsp"; try { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet Error</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2 align=center>" + request.getAttribute("err") + "</h2>"); response.setHeader("refresh", "2;url='login.jsp'"); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("請登入!頁面將在3秒後自動跳轉,如果沒有,請點選<a href='login.jsp'>回到登入頁面</a>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); RequestDispatcher dis = request.getRequestDispatcher(url); dis.forward(request, response); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }
效果展示: