Head first Servlets and Jsp Chapter 13 Short Notes

May 26, 2011 at 3:56 am Leave a comment

Filters and Wrappers

Objectives:
1. Write web container request processing model; write and configure filter; create request response wrapper;
Given a design scenario decide bw filter or wrapper
2. Knowing Patterns: Intercepting filters, MVC, Front Controller, Service Locator, Business Delegate and Transfer Object

1. Filters:

* Filters are java classes that are used to process before request reaches container and after the response is sent to client but before it reaches client
* Request and Response filters are possible with Filter interface
* These filters are configured in DD and is in total control with the deployment expert and not the servlet developer
* Filters can be chained

2. Request Filters:
* Perform security checks
* Reformat req. headers and body
* Audit or log requests

3. Response Filters:
* Compress response streams
* append or alter response
* create a different response altogether

4. Filters are like Servlets in 3 ways
* Container knows their API (moment when u implement the Filter interface plain class becomes special class)
* They have life cycle methods like: init(), destroy() and doFilter()
* They are declared in DD

5. Creating a request filter to track who made request:

* MUST implement init() method to save the FilterConfig reference
* implement doFilter(ServletRequest, ServletRespose, FilterChain) method
Forward the request and response by calling chain.doFilter(req,res)
Type case request to HttpServletRequest
Get the user name by calling request.getRemoteUser()
And also use filterConfig.getServletContext().log(“blah blah” + remoteUser)
* MUST implement the destroy() method to do cleanup stuffs

6. Declaring and ordering filters

* Declare the filter
<filter>
BeerRequest
<filter-class>example.web.BeerRequestFilter
<init-param>
LofFileName
<param-value>log.txt
</filter>

* Declaring filter mapping to a URL pattern
<filter-mapping>
<filter-name>BeerRequest</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

* Declaring filter mapping to a Servlet Name

<filter-mapping>
<filter-name>BeerRequest</filter-name>
<servlet-name>BeerAdviceServlet</servlet-name>
</filter-mapping>

If more than one filter is mapped to a Servlet or URL Pattern, all filters are taken up and then executed in the order they appear

* From Servlet spec 2.4, filters are applicable for dispatcher call as well

<filter-mapping>
<filter-name>BeerRequest</filter-name>
<url-pattern>*.do</url-pattern> or <servlet-name>
<dispatcher>REQUEST</dispatcher> or INCLUDE or FORWARD or ERROR where REQUEST is the default when not specified
</filter-mapping>

* When we need to write a response filter, then create one new response class by extending any of the response Wrapper classes
so that the response is not immediately written back to client when the servlet finishes !

Advertisement

Entry filed under: Education, programming, web. Tags: .

Head first Servlets and Jsp Chapter 12 Short Notes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed



Follow

Get every new post delivered to your Inbox.