b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

jsp - filter过滤器使用详解(6)

电脑杂谈  发布时间:2017-12-25 20:08:08  来源:网络整理

定义SecurityFilter过滤器,让它过滤匹配/admin/*的所有请求,/admin/路径下的所有请求都会接受SecurityFilter的检查

jsp -_jsp<%%>_jsp动作组件

因为Filter本来设计成为多种协议服务,http协议仅仅是其中一种,将ServletRequest和ServletResponse转换成HttpServletRequest和HttpServletResponse才能进行下面的session操作和页面重定向。

得到了http请求之后,可以获得请求对应的session,判断session中的username变量是否为null,如果不为null,说明用户已经登录,就可以调用doFilter继续请求访问的资源。如果为null,说明用户还没有登录,禁止用户访问,并使用页面重定向跳转到failure.jsp页面显示提示信息。

因为/failure.jsp的位置在/admin/目录的上一级,所以加上两个点才能正确跳转到failure.jsp,两个点(..)代表当前路径的上一级路径。

public class LoggingFilter implements Filter {
  private FilterConfig filterConfig = null;
  public void init(FilterConfig config) throws ServletException {
   this.filterConfig = config;
 }

 //下面是向服务器控制台输出log,这里做的是演示,更多的是使用log4j
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
   String address = request.getRemoteAddr();
   filterConfig.getServletContext().log("User IP: " + address);
   chain.doFilter(request, response);
 }
 public void destroy() {
 }
}

web.xml配置

  <filter>
   <filter-name>LoggingFilter</filter-name>
   <filter-class>samjava.filter.LoggingFilter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>LoggingFilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>

请求映射filter-mapping和servlet-mapping都是将对应的filter或servlet映射到某个url-pattern上,当客户发起某一请求时,服务器先将此请求与web.xml中定义的所有url-pattern进行匹配,然后执行匹配通过的filter和servlet。

你可以使用三种方式定义url-pattern。

直接映射一个请求。


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-55857-6.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...