session 3 inter-servlet communication & filters - giáo trình bách khoa aptech
DESCRIPTION
TRANSCRIPT
Slide 1 of 25
Filters Inter-Servlet Communication
Slide 2 of 25
Objectives Introduction to Filters Filter API Configuring Filter Manipulating Request and Responses Servlet Communication
Slide 3 of 25
Introduction Filter
Slide 4 of 25
Filters
Components that add functionality to the request and response processing of a Web Application
Intercept the requests and responses that flow between a client and a Servlet/JSP.
Slide 5 of 25
Filter Participation
User FilterWeb Resource(Servlet / JSP)
Request
Response
Filter Participating in Request-Response Process
Slide 6 of 25
Filter Life CycleInstantiation and Loading
Initialization init()
doFilter()
destroy()
Unavailable GC
Slide 7 of 25
Filter Chain Invoke a series of filters If the Calling filter is last filter, will invoke web resource
Filter 1
ClientFilter
2Filter
3
Web Resourc
e
Working of Filter Chain
Filter Chain
Slide 8 of 25
Usage of Filter
Authentication Logging and auditing filter Image conversion filters Data compression Encryption filters Filters that trigger resource access events
Slide 9 of 25
Benefits of Filter
Slide 10 of 25
Compress Filter
Slide 11 of 25
Working of Filter
Slide 12 of 25
Working of Filter
Slide 13 of 25
Filter API
Slide 14 of 25
“Filter” InterfaceA filter is an object than perform filtering tasks
on either the request to a resource (a servlet or static content), or on the response from a resource, or both.
Init() doFilter() Destroy()
Slide 15 of 25
“FilterConfig” Interface A filter configuration object used by a servlet
container used to pass information to a filter during initialization
Methods– getFilterName()– getInitParameter()– getInitParameterNames()– getServletContext()
Slide 16 of 25
“FilterChain” Interface A FilterChain is an object provided by the
servlet container to the developer giving a view into the invocation chain of a filtered request for a resource
Methods– doFilter()
Slide 17 of 25
Configuring Filter<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter> <filter-name>TimerFilter</filter-name> <filter-class>filters.TimerFilter</filter-class> </filter> <filter-mapping> <filter-name>TimerFilter</filter-name> <url-pattern>/TimerFilter</url-pattern> </filter-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>Timer.html</welcome-file> </welcome-file-list></web-app>
17
Slide 18 of 25
Filter Element <icon> <filter-name> <display-name> <description> <filter-class> <init-param>
Slide 19 of 25
Filter-Mapping Element <filter-name> <url-name> <servlet-name>
Slide 20 of 25
Configuring Filter Chain
Slide 21 of 25
Manipulating Request and Response
Slide 22 of 25
Manipulating Request and Response
ServletRequestWrapper ServletResponseWrapper
Slide 23 of 25
Inter-Servlet Communication
A process where two or more Servlets communicates with each other to process the client request.
A Servlet can forward the request to another Servlet to process the client request.
A Servlet can include the output of another Servlet to process the client request.
Slide 24 of 25
Applet – Servlet Communication
Developing Applet front end application Establishing connection between applet and
servlet Communicasting throught object
serialisation Sending objects from applet to servlet Sending objects from servlet to applet
Slide 25 of 25
Summary Introduction to Filters Filter API Configuring Filter Manipulating Request and Responses Servlet Communication