9.1. Understand the sevlet lifecycle
The lifecycle of a servlet is controlled by the container in with the servlet has been deployed.
1.a. Container loads the servlet class.
1.b. Create instance servlet class
1.c. Initialization (init method).
2. Service method invoke.
3. Destroy method - finalizes and remove.
9.2. Describe and use more advanced elements of the servlets APIs
Table from docs.oracle.com/javaee/6/tutorial/doc/javaeetutorial6.pdf
9.3. Create filter and use them in web applications
The objective of filters is to intercept request from user (or the response after servlet is completed). Defining URL patterns, the request that matches would be intercept before arriving to the resource (or before arriving the client in the case of intercepting response).
Filter is an interface with the following methods:
- public void init(FilterConfig filterConfig)
- public void destroy()
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain). This method should include: chain.doFilter(request,response) the method doFilter() of FilterChain invokes the next filter.
Filters in DD
<filter>
<filter-name>NewFilter</filter-name>
<filter-class>example.NewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>NewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The mapping can be declared using:
-URLpattern
-Servlet name
Also can be applied for request dispatchers (REQUEST, INCLUDE, FORWARD, ERROR). If the tag <dispatcher> is not present, by default is REQUEST
Filters are invoked in the order they are defined in the deployment descriptor (first mathing by URL pattern and then matching servlet-name).
Example of a filter for authentication:
Annotations
@WebFilter:
@WebFilter(filterName = "NewFilter",
urlPatterns = {"/*"},
initParams = {
@WebInitParam(name = "user", value = "guest")})
Filter utilities:
-Authentication, authorization,
-Request/response compression
-Launch triggers
-Logging
etc.
OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification
No comments:
Post a Comment