----------------------过滤器filter简单登录和乱码过滤----
生活随笔
收集整理的这篇文章主要介绍了
----------------------过滤器filter简单登录和乱码过滤----
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
web.xml里面的代码
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name></display-name><!-- 编码过滤器 --><filter><filter-name>CharactorEncodingFilter</filter-name><filter-class>cn.happy.filter.CharacterEncodingFilter</filter-class></filter><!-- 鉴权过滤器 --><filter><filter-name>SessionFilter</filter-name><filter-class>cn.happy.filter.SessionFilter</filter-class><!-- 初始化参数 --><init-param><param-name>execuldeUrl</param-name><param-value>/servlet/(Login|Register)Servlet</param-value></init-param></filter><!-- 按照书写先后顺序 ,执行顺序按照Sevlet-Mapping 书写顺序 --><filter-mapping><filter-name>CharactorEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 需要权限的页面 --><filter-mapping><filter-name>SessionFilter</filter-name><url-pattern>/jsp/*</url-pattern></filter-mapping><filter-mapping><filter-name>SessionFilter</filter-name><url-pattern>/servlet/*</url-pattern></filter-mapping><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>LoginServlet</servlet-name><servlet-class>cn.happy.servlet.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>LoginServlet</servlet-name><url-pattern>/servlet/LoginServlet</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app>servlet里面的代码
package cn.happy.filter;import java.io.IOException; import java.util.regex.Pattern;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;public class SessionFilter implements Filter {Pattern patternUrl;//能从配置文件获取内容public void init(FilterConfig config) throws ServletException {String url=config.getInitParameter("execuldeUrl"); ///servlet/LoginServletif(url!=null&&!url.equals("")){ //配置了例外//为了获取到一个正则对象 patternUrl= Pattern.compile(url); //我们定制的正则规范 /servlet/LoginServlet}}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {HttpServletRequest req=(HttpServletRequest)request;HttpServletResponse res=(HttpServletResponse)response;//路径中包含 /servlet/LoginServlet 方向String servletPath = req.getServletPath();System.out.println(servletPath+"===========================");if(patternUrl.matcher(servletPath).matches()){chain.doFilter(request, response);return;}Object sessionObj = req.getSession().getAttribute("uname");if(sessionObj!=null){ //证明用户已经登录了//放行chain.doFilter(request, response);}else{System.out.println("session filter not session data");//证明用户没有登录,返回到登录 /Jsp-Day05Filterres.sendRedirect(req.getContextPath()+"/login.jsp");}}public void destroy() {}}
转载于:https://www.cnblogs.com/laosunlaiye/p/7383832.html
《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读总结
以上是生活随笔为你收集整理的----------------------过滤器filter简单登录和乱码过滤----的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: redis cluster 集群重新启动
- 下一篇: HDU 6143 Killer Name