第三十五章——过滤器和监听器
Posted 缘来狠狂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三十五章——过滤器和监听器相关的知识,希望对你有一定的参考价值。
过滤器代码示例:
package com.hanqi.maya.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.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet Filter implementation class SessionFilter */ @WebFilter(filterName="sessionFilter", value={"/*"}, initParams={@WebInitParam(name="initp", value="message.jsp;login.jsp;LoginOrRegisterServlet")}) public class SessionFilter implements Filter { private String initParams; /** * Default constructor. */ public SessionFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)resp; String[] ss = initParams.split(";"); String reqUrl = request.getRequestURI(); for(String s : ss) { if(reqUrl.indexOf(s)!=-1) { chain.doFilter(request, response); return; } } Object obj = request.getSession().getAttribute("currentUser"); if(obj==null) { response.sendRedirect("login.jsp"); } else { chain.doFilter(request, response); } } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { initParams = fConfig.getInitParameter("initp"); } }
监听器:
以上是关于第三十五章——过滤器和监听器的主要内容,如果未能解决你的问题,请参考以下文章