servlet串行拦截器实现例子
至于串行过滤器有什么作用,我实在不知。我的理解是它只是说明 过滤器的串行运行方式
需求:当用户没有登录访问更新页面的时候,跳转到登录页面
1.登录页面:login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="UserLoginServlet" method="post"> username:<input type="text" value="<%=null == request.getAttribute("username")?"" : request.getAttribute("username") %>" name="username"><br> password:<input type="text" name="password"><br> authority:<select name="authority"> <option value="1" <%="1".equals(request.getAttribute("authority"))?"selected = selected" : "" %>>common user</option> <option value="2" <%="2".equals(request.getAttribute("authority"))?"selected = selected" : "" %>>adminstrator</option> </select> <br> <input type="submit"> </form> </body> </html>
2.登录页面的servet容器
package com.filter; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.javabean.User; public class UserLoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = (String)req.getParameter("username"); String password = (String)req.getParameter("password"); String authority = (String)req.getParameter("authority"); if("1".equals(authority) && "zhangsan".equals(username) && "123".equals(password)) { User user = new User(); user.setUsername(username); user.setPassword(password); user.setAuthority(authority); HttpSession session = req.getSession(); session.setAttribute("user",user); req.getRequestDispatcher("index.jsp").forward(req, resp); return ; } if("2".equals(authority) && "lisi".equals(username) && "456".equals(password)) { User user = new User(); user.setUsername(username); user.setPassword(password); user.setAuthority(authority); HttpSession session = req.getSession(); session.setAttribute("user",user); req.getRequestDispatcher("index.jsp").forward(req, resp); return ; } System.out.println("meiyou denglu"); resp.sendRedirect("login.jsp?error=true&username="+username+"&authority="+authority); } }
3.主页面index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.javabean.User" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="QueryServletFilter">query</a><br> <% User user = (User)session.getAttribute("user"); if(null == user) { response.sendRedirect("login.jsp"); return; } if("2".equals(((User)session.getAttribute("user")).getAuthority())){ %> <a href="UpdateServletFilter">update</a> <%} %> </body> </html>
4.查询servlet. QueryServletFilter
package com.filter; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class QueryServletFilter extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("QueryServletFilter run"); } }
5.更新servlet容器. UpdateServletFilter
package com.filter; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class UpdateServletFilter extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("UpdateServletFilter run"); } }
6.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>filter</display-name> <servlet> <servlet-name>UserLoginServlet</servlet-name> <servlet-class>com.filter.UserLoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserLoginServlet</servlet-name> <url-pattern>/filter/UserLoginServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>QueryServletFilter</servlet-name> <servlet-class>com.filter.QueryServletFilter</servlet-class> </servlet> <servlet-mapping> <servlet-name>QueryServletFilter</servlet-name> <url-pattern>/filter/QueryServletFilter</url-pattern> </servlet-mapping> <servlet> <servlet-name>UpdateServletFilter</servlet-name> <servlet-class>com.filter.UpdateServletFilter</servlet-class> </servlet> <servlet-mapping> <servlet-name>UpdateServletFilter</servlet-name> <url-pattern>/filter/UpdateServletFilter</url-pattern> </servlet-mapping> <!-- filter start --> <filter> <description>this is the servlet filter example</description> <filter-name>userLoginServletFilter1</filter-name> <filter-class>com.filter.UserLoginServletFilter</filter-class> </filter> <!-- 演示过滤器串行运行 --> <filter> <description>this is the servlet filter example</description> <filter-name>userLoginServletFilter2</filter-name> <filter-class>com.filter.UserLoginServletFilter2</filter-class> </filter> <!-- url-pattern表示只过滤 UpdateServletFilter这一个servlet容器--> <filter-mapping> <filter-name>userLoginServletFilter1</filter-name> <url-pattern>/filter/UpdateServletFilter</url-pattern> </filter-mapping> <!-- url-pattern表示只过滤 UpdateServletFilter这一个servlet容器--> <filter-mapping> <filter-name>userLoginServletFilter2</filter-name> <url-pattern>/filter/UpdateServletFilter</url-pattern> </filter-mapping> <!-- filter end --> </web-app>
7.filter拦截器 UserLoginServletFilter
package com.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class UserLoginServletFilter implements Filter { @Override public void destroy() { System.out.println("destroy"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; HttpSession s = req.getSession(); if(req.getRequestURI().endsWith("login.jsp") || req.getRequestURI().endsWith("UserLoginServlet")) { System.out.println("Filter1 start"); filterChain.doFilter(request, response); System.out.println("filter1 end"); return ; } if(null == s.getAttribute("user")) { System.out.println("Filter1 start.."); ((HttpServletResponse)response).sendRedirect("login.jsp"); return; } else { System.out.println("Filter1 start...."); filterChain.doFilter(request, response); } } @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init"); } }
8.filter拦截器2 UserLoginServletFilter2
package com.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class UserLoginServletFilter2 implements Filter { @Override public void destroy() { System.out.println("destory2"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { System.out.println("doFilter2 start"); filterChain.doFilter(request, response); System.out.println("doFilter2 end"); } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println("init2"); } }
web.xml中url-pattern属性值表示访问的路径。
珍惜现在,珍惜每一分,每一秒。
从不放弃,坚持。。。。。