开发者

What object can be used to intercept all requests to a web application before they reach the JSP or Servlet?

What object can be used to intercept all requests to a web application before they reach the JSP or Servlet? I think the answer might be a filter, but I'm not sure if there is a m开发者_JAVA百科ore specific answer.


The filter is the way to go. You can map a filter much like a servlet so that it can cover the entire web site, or just portions.

Edit for comment -

Yes, that's pretty much what you need to do. Filters are the first part of the request chain. Well, after dispatch by the container, but the first part you as a developer have access to.

Filters can be chained, and each one can pre-empt the request chain (thus never hitting any later Filters or Servlets (JSPs are Servlets), they can proceed down the request chain, and they can usurp the results of the request chain (that is they can change or reject the output from components farther down. You can consider it an implementation of the Decorator pattern.

Some typical filters include security filters (checking whether the request has access to the desired resource), compression filters, request modification filters (the functionality of Apaches mod_rewrite has been implemented as a Filter, for example). Filters can completely replace the actual request and response object (many do so by wrapping the existing ones), so they really do have complete control over the request chain, and eventual Servlet or static resource has no idea the Filter is there.

Filters are a wonderful aspect of the Servlet stack.


The filter is the best way to route all the request to the jsp/servlet ,

Usage: 1. To check whether user has valid session. 2. Add a attribute to the request. 3. Compress the outputstream incase of file download. 4. anlayse request, i.e. frequently visited page for buisness analyst.


Yes Filter is the Right answer . Just wanted to add in a MVC design pattern similiar thing can be done with a controller servlet. Which first takes all the requests before moving somewhere else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜