servlet filter
DESCRIPTION
Creating and using Servlet Filters.TRANSCRIPT
![Page 1: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/1.jpg)
SERVLET AND JSP FILTERS
Prof. AshishSingh Bhatia
1Prof. AshishSingh Bhatia
![Page 2: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/2.jpg)
What is Filter?
Filter is a small program that run on the server before the servlet or JSP page with which it is associated.
Filter can be attached to one or more servlets or JSP pages and can examine the request information going into these resources.
Filter can Invoke the resource in normal manner.
Filter can Invoke the resource with modified request information.
Filter can Invoke the resource but modify response before sending it to the client.
Filter can Prevent resource from being invoked and instead redirect to a different resource, return a particular status code, or generate replacement output.
Prof. AshishSingh Bhatia 2
![Page 3: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/3.jpg)
What it can be used for ?
Authentication blocking request based on user identity.
Logging and auditing – Tracking uses of a web application.
Image Conversion – Scaling maps and so on.
Data Compression – Making downloads smaller.
Localization – Targeting a request and response in particular locale.
XSL/T Transformation of XML content -
Prof. AshishSingh Bhatia 3
![Page 4: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/4.jpg)
One or many filters
Prof. AshishSingh Bhatia 4
![Page 5: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/5.jpg)
Creating a Basic Filter
1. Create a class that implements the Filter Interface. [ init, doFilter , destroy ]
2. Put the filtering behavior in the doFilter method.
3. Call the doFilter method of the FilterChain object.
4. Register the filter with the appropriate servlets and JSP page.
5. Disable the invoker servlet. **
Prof. AshishSingh Bhatia 5
![Page 6: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/6.jpg)
Create a class that implements the Filter Interface
Must implement javax.servlet.Filter
public void init(FilterConfig config) throws ServletException
public void doFilter(ServletRequest request, ServletResponse response, FilterChainchain) throws ServletException, IOException Executed each time servlet is invoked.
FilterChain is used to invoke next filter that is associated with the servlet or jsp page, if no more filters it will invoke servlet or jsp page itself.
public void destroy()
Prof. AshishSingh Bhatia 6
![Page 7: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/7.jpg)
Register the Filter
Use filter and filter-mapping tags to register filter in servlet.
<filter>
<filter-name> … </filter-name>
<filter-class> … </filter-class>
</filter>
<filter-mapping>
<filter-name> … </filter-name>
<url-pattern> … </url-pattern>
</filter-mapping>
Prof. AshishSingh Bhatia 7
![Page 8: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/8.jpg)
Understanding filter and mapping tags
In filter element we can have icon, filter-name, display-name, filter-class, init-param.
In filter-mapping element filter-name, url-pattern, servlet-name, dispatcher.
dispatcher : Optional used to specifies what type of request this filter-mapping should apply to.
Possible values are REQUEST, FORWARD, INCLUDE and ERROR
Prof. AshishSingh Bhatia 8
![Page 9: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/9.jpg)
Example : Reporting Filter
Prof. AshishSingh Bhatia 9
![Page 10: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/10.jpg)
Code for Servlet
Prof. AshishSingh Bhatia 10
![Page 11: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/11.jpg)
web.xml
Prof. AshishSingh Bhatia 11
![Page 12: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/12.jpg)
Valid too
Prof. AshishSingh Bhatia 12
![Page 13: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/13.jpg)
Better Approach
Lets log in to the file [ Recall logs directory ]
For accessing log we need context object.
context.log(String) to log in to file.
Prof. AshishSingh Bhatia 13
![Page 14: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/14.jpg)
Prof. AshishSingh Bhatia 14
![Page 15: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/15.jpg)
Log File
Prof. AshishSingh Bhatia 15
![Page 16: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/16.jpg)
Initialization Parameter in Filter
Developers, End Users, Deployers
<filter>
<filter-name>SomeFilter</filter-name>
<filter-class>somePackage.SomeFilterClass</filter-class>
<init-param>
<param-name>param1</param-name>
<param-value>value1</param-value>
</init-param>
</filter>
Prof. AshishSingh Bhatia 16
String val1 = config.getInitParameter("param1");
![Page 17: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/17.jpg)
Example : An Access Time Filter
To log if access is at unusual time Say if its accessed between 2 to 10 log should be made.
To do this two init parameter start time 2 and end time as 10
In Filter we will read the init parameter as starttime and endtime
We will get the accessed time as
GregorianCalendar calendar = new GregorianCalendar();
int currentTime = calendar.get(Calendar.HOUR_OF_DAY);
If current time is greater than starttime and less than endtime log will be done.
Prof. AshishSingh Bhatia 17
![Page 18: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/18.jpg)
Blocking the Response
What we do in a filter
public void doFilter(ServletRequest request,ServletResponse response, FilterChainchain) throws ServletException, IOException
{
HttpServletRequest req = (HttpServletRequest)request;
context.log(req.getRemoteHost() + " tried to access " +
req.getRequestURL() +" on " + new Date() + ".");
chain.doFilter(request,response);
}
Prof. AshishSingh Bhatia 18
This will call next filter or servlet or jsp
![Page 19: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/19.jpg)
Blocking the Response
public void doFilter(ServletRequest request,ServletResponse response, FilterChainchain) throws ServletException, IOException
{
HttpServletRequest req = (HttpServletRequest)request;
context.log(req.getRemoteHost() + " tried to access " +
req.getRequestURL() +" on " + new Date() + ".");
if(condition==true)
response.sendRedirect(“some other page / site”);
else
chain.doFilter(request,response);
}
Prof. AshishSingh Bhatia 19
![Page 20: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/20.jpg)
Modifying the response
Prof. AshishSingh Bhatia 20
FilterdoChain(request,response)
Servlet / JSPGenerate Response
And closes the stream !
We will create our own space / buffer and give it to servlet and can
manipulate the same once Servlet / JSP is done with response
![Page 21: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/21.jpg)
Creating our Wrapper Class
Prof. AshishSingh Bhatia 21
![Page 22: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/22.jpg)
In Filter Class
Prof. AshishSingh Bhatia 22
![Page 23: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/23.jpg)
In Web.xml
Prof. AshishSingh Bhatia 23
Target will be replace by replacement
Test.com will be replace by new.com
![Page 24: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/24.jpg)
Modification Method
Prof. AshishSingh Bhatia 24
![Page 25: Servlet Filter](https://reader034.vdocuments.site/reader034/viewer/2022052321/55581593d8b42a25588b4624/html5/thumbnails/25.jpg)
END OF SESSION
25Prof. AshishSingh Bhatia