过滤器监听器面试题都在这里(修订版)

 
 

前言

只有光头才能变强。

文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y

以下我是归纳的过滤器监听器知识点图:


640?wx_fmt=png

(原图可以在公众号下回复 “脑图” 获取)

图上的知识点都可以在我其他的文章内找到相应内容。

监听器常见面试题

监听器有哪些作用和用法?

监听器有哪些作用和用法?

Java Web开发中的监听器(listener)就是application、session、request三个对象创建、销毁或者往其中添加修改删除属性时自动执行代码的功能组件,如下所示:

常见的监听器用途主要包括:网站在线人数技术、监听用户的行为(管理员踢人)

过滤器常见面试题

过滤器有哪些作用和用法?

过滤器有哪些作用和用法?

Java Web开发中的过滤器(filter)是从Servlet 2.3规范开始增加的功能,并在Servlet 2.4规范中得到增强。对Web应用来说,过滤器是一个驻留在服务器端的Web组件,它可以截取客户端和服务器之间的请求与响应信息,并对这些信息进行过 滤。当Web容器接受到一个对资源的请求时,它将判断是否有过滤器与这个资源相关联。如果有,那么容器将把请求交给过滤器进行处理。在过滤器中,你可以改 变请求的内容,或者重新设置请求的报头信息,然后再将请求发送给目标资源。当目标资源对请求作出响应时候,容器同样会将响应先转发给过滤器,再过滤器中, 你可以对响应的内容进行转换,然后再将响应发送到客户端。

常见的过滤器用途主要包括:对用户请求进行统一认证、对用户的访问请求进行记录和审核、对用户发送的数据进行过滤或替换、转换图象格式、对响应内容进行压缩以减少传输量、对请求或响应进行加解密处理、触发资源访问事件、对XML的输出应用XSLT等

和过滤器相关的接口主要有:Filter、FilterConfig、FilterChain

Java Web常见面试题

web.xml 的作用?

web.xml 的作用?

答:用于配置Web应用的相关信息,如:监听器(listener)、过滤器(filter)、 Servlet、相关参数、会话超时时间、安全验证方式、错误页面等。例如:

①配置Spring上下文加载监听器加载Spring配置文件:

<context-param>     <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <listener>     <listener-class>       org.springframework.web.context.ContextLoaderListener     </listener-class>  </listener>  
   <param-name>contextConfigLocation</param-name>  
  <param-value>classpath:applicationContext.xml</param-value>  
</context-param>  

<listener>  
   <listener-class>  
     org.springframework.web.context.ContextLoaderListener  
   </listener-class>  
</listener>  

②配置Spring的OpenSessionInView过滤器来解决延迟加载和Hibernate会话关闭的矛盾:

<filter>    <filter-name>openSessionInView</filter-name>    <filter-class>       org.springframework.orm.hibernate3.support.OpenSessionInViewFilter    </filter-class>  </filter>  <filter-mapping>    <filter-name>openSessionInView</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  
  <filter-name>openSessionInView</filter-name>  
  <filter-class>  
     org.springframework.orm.hibernate3.support.OpenSessionInViewFilter  
  </filter-class>  
</filter>  

<filter-mapping>  
  <filter-name>openSessionInView</filter-name>  
  <url-pattern>/*</url-pattern>  
</filter-mapping>  

③配置会话超时时间为10分钟:

<session-config>    <session-timeout>10</session-timeout>  </session-config>  
  <session-timeout>10</session-timeout>  
</session-config>  

④配置404和Exception的错误页面:

[html] view plaincopy在CODE上查看代码片派生到我的代码片 <error-page>    <error-code>404</error-code>    <location>/error.jsp</location>  </error-page>  <error-page>    <exception-type>java.lang.Exception</exception-type>    <location>/error.jsp</location>  </error-page>  <error-page>  
  <error-code>404</error-code>  
  <location>/error.jsp</location>  
</error-page>  

<error-page>  
  <exception-type>java.lang.Exception</exception-type>  
  <location>/error.jsp</location>  
</error-page>  

⑤配置安全认证方式:

<security-constraint>    <web-resource-collection>      <web-resource-name>ProtectedArea</web-resource-name>      <url-pattern>/admin/*</url-pattern>      <http-method>GET</http-method>      <http-method>POST</http-method>    </web-resource-collection>    <auth-constraint>      <role-name>admin</role-name>    </auth-constraint>  </security-constraint>  <login-config>    <auth-method>BASIC</auth-method>  </login-config>  <security-role>    <role-name>admin</role-name>  </security-role>  
  <web-resource-collection>  
    <web-resource-name>ProtectedArea</web-resource-name>  
    <url-pattern>/admin/*</url-pattern>  
    <http-method>GET</http-method>  
    <http-method>POST</http-method>  
  </web-resource-collection>  
  <auth-constraint>  
    <role-name>admin</role-name>  
  </auth-constraint>  
</security-constraint>  

<login-config>  
  <auth-method>BASIC</auth-method>  
</login-config>  

<security-role>  
  <role-name>admin</role-name>  
</security-role>  

【补 充1】从Servlet 3开始,可以不用在web.xml中部署Servlet(小服务)、Filter(过滤器)、Listener(监听器)等Web组件,Servlet 3提供了基于注解的部署方式,可以分别使用@WebServlet、@WebFilter、@WebListener三个部署小服务、过滤器、监听器。

【补充2】如果Web提供了有价值的商业信息或者是敏感数据,那么站点的安全性就是必须考虑的问题。安全认证是实现安全性的重要手段,认证就是要解决“Are you who you say you are?”的问题。认证的方式非常多,简单说来可以分为三类:

A.What you know?  --口令

B.What you have? --数字证书(U盾、密保卡)

C.Who you are? -- 指纹识别、虹膜识别

在Tomcat中可以通过建立安全套接字层(Secure Socket Layer, SSL)以及通过基本验证或表单验证来实现对安全性的支持。

Servlet 3中的异步处理指的是什么?

Servlet 3中的异步处理指的是什么?

答: 在Servlet 3中引入了一项新的技术可以让Servlet异步处理请求。有人可能会质疑,既然都有多线程了,还需要异步处理请求吗?答案是肯定的,因为如果一个任务处 理时间相当长,那么Servlet或Filter会一直占用着请求处理线程直到任务结束,随着并发用户的增加,容器将会遭遇线程超出的风险,这这种情况下 很多的请求将会被堆积起来而后续的请求可能会遭遇拒绝服务,直到有资源可以处理请求为止。异步特性可以帮助应用节省容器中的线程,特别适合执行时间长而且 用户需要得到结果的任务,如果用户不需要得到结果则直接将一个Runnable对象交给Executor(如果不清楚请查看前文关于多线程和线程池的部 分)并立即返回即可

开启异步处理代码:

@WebServlet(urlPatterns = {"/async"}, asyncSupported = true)  public class AsyncServlet extends HttpServlet {      private static final long serialVersionUID = 1L;      @Override      public void doGet(HttpServletRequest req, HttpServletResponse resp)               throws ServletException, IOException {          // 开启Tomcat异步Servlet支持          req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);          final AsyncContext ctx = req.startAsync();  // 启动异步处理的上下文          // ctx.setTimeout(30000);          ctx.start(new Runnable() {              @Override              public void run() {                  // 在此处添加异步处理的代码                  ctx.complete();              }          });      }  }  "/async"}, asyncSupported = true)  
public class AsyncServlet extends HttpServlet {  
    private static final long serialVersionUID = 1L;  

    @Override  
    public void doGet(HttpServletRequest req, HttpServletResponse resp)   
            throws ServletException, IOException 
{  
        // 开启Tomcat异步Servlet支持  
        req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED"true);  

        final AsyncContext ctx = req.startAsync();  // 启动异步处理的上下文  
        // ctx.setTimeout(30000);  
        ctx.start(new Runnable() {  

            @Override  
            public void run() {  
                // 在此处添加异步处理的代码  

                ctx.complete();  
            }  
        });  
    }  
}  


最后

乐于输出干货的Java技术公众号:Java3y。公众号内有200多篇原创技术文章、海量视频资源、精美脑图,不妨来关注一下!

640?wx_fmt=jpeg

有帮助?好看!转发! 640


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值