您的位置 首页 java

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

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

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

监听器常见面试题

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

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

  • Servlet ContextListener:对Servlet上下文的创建和销毁进行监听。
  • ②ServletContextAttributeListener:监听Servlet上下文属性的添加、删除和替换。
  • ③HttpSessionListener:对Session的创建和销毁进行监听。
  • session超时(可以在web.xml中通过
  • <session- config>/<session-timeout>标签配置超时时间);
  • 通过调用session对象的invalidate()方 法使session失效。
  • 补 充:session的销毁有两种情况:
  • ④HttpSessionAttributeListener:对Session对象中属性的添加、删除和替换进行监听。
  • ⑤ServletRequestListener:对请求对象的初始化和销毁进行监听。
  • ⑥ServletRequestAttributeListener:对请求对象属性的添加、删除和替换进行监听。

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

过滤器常见面试题

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

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

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> 
 

②配置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> 
 

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

<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> 
 

⑤配置安全认证方式:

<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> 
 

【补 充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(); 
 } 
 }); 
 } 
} 
 

文章来源:

作者:Java3y

文章来源:智云一二三科技

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

文章地址:https://www.zhihuclub.com/177020.shtml

关于作者: 智云科技

热门文章

评论已关闭

106条评论

  1. Thank you! Loads of tips!
    online assignment writing service essay writing service ratings

  2. Awesome tips. Appreciate it!
    pay someone to write a research paper [url=https://seoqmail.com/]pay to get essays written[/url]

  3. Awesome material. Appreciate it.
    what i do in my spare time essay while revising an argumentative essay a writer should

  4. Position effectively regarded!!
    can someone write a song for me write my essay for me website

  5. Well expressed of course! .
    can you write my essay write a paragraph for me using these words

  6. Awesome material, Regards.
    dissertation writing services cost dissertation abstracts international

  7. With thanks. Quite a lot of material!
    can someone do my essay what should i write my persuasive essay on

  8. Regards. A good amount of postings.
    grad school essay writing service writing a good essay

  9. Amazing loads of beneficial information!
    pay someone to write my college essay pay someone to write my college essay

  10. You expressed that fantastically!
    research proposal cover page elements of a research proposal

  11. You expressed that wonderfully!
    dissertation writing services dissertation writing services reviews

  12. You actually expressed this exceptionally well!
    writing a personal essay help me write my essay

  13. Many thanks. Ample information.
    essay writing service reviews professional paper writing service

  14. Really lots of excellent knowledge!
    college essays writing which essay writing service is the best

  15. Regards, Plenty of posts.
    writing a good essay essay writer no plagiarism
    dissertation writing services dissertation abstract
    essay writing jobs

  16. Fine stuff. Many thanks!
    essay writing websites cheap essay writing service us
    write my essay online write my essay online
    how to write an exemplification essay

  17. You’ve made your position extremely effectively..
    do my essay for me essay writer no plagiarism
    cheap essay writing service best finance essay writing service
    help writing a thesis

  18. You suggested it fantastically.
    online check writing service professional essay writing service

  19. Thanks a lot. Plenty of tips.
    professional paper writers write my research paper for me

  20. You explained that well.
    custom handwriting paper pay someone to write a paper
    proposal research research proposal
    pay someone to write my essay

  21. Cheers, A good amount of write ups!
    dissertation assistance writing dissertation
    essay for sale pay someone to write your paper
    good opening sentences for college essays

  22. Seriously loads of beneficial advice.
    do my chemistry homework do my homework for me
    essay writers online pay someone to write my paper
    how to write a funny essay

  23. You made your point.
    coursework online coursework
    essays for sale buy essays
    writing help for college students

  24. Lovely data, Thank you!
    phd weight loss dissertation writing service
    best essay writing service 2016 letter writing service
    national honor society essay help

  25. You actually revealed it superbly!
    college paper writing service professional paper writing service

  26. Cheers. I appreciate this.
    do my homework for me do my homework
    writing essays for money writing a persuasive essay
    writing helper

  27. Appreciate it, A good amount of information!
    term paper research paper writer
    write my essay cheap writing an opinion essay
    custom thesis writing service

  28. Kudos, I enjoy this.
    writing essay service what is the best essay writing service
    custom paper writing service best essay writing
    rutgers essay help

  29. You’ve made your point extremely nicely..
    essay writing tips paper writing service reviews

  30. Good information, Appreciate it.
    linkedin profile writing service custom essay writing service
    paperhelp how to write a college essay
    write a comparison essay

  31. Good stuff. Regards.
    good essay writing service writing a college application essay
    college application essay help writing help
    online custom writing services

  32. You definitely made the point.
    best resume writing service reddit spanish essay writing service

网站地图