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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import java.io.IOException; | |
import javax.servlet.Filter; | |
import javax.servlet.FilterChain; | |
import javax.servlet.FilterConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletRequest; | |
import javax.servlet.ServletResponse; | |
import javax.servlet.annotation.WebFilter; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
/** | |
* Servlet Filter implementation class NewFilter | |
*/ | |
@WebFilter("/NewFilter") | |
public class NewFilter implements Filter { | |
/** | |
* Default constructor. | |
*/ | |
public NewFilter() { | |
// TODO Auto-generated constructor stub | |
} | |
/** | |
* @see Filter#destroy() | |
*/ | |
public void destroy() { | |
// TODO Auto-generated method stub | |
} | |
/** | |
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) | |
*/ | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | |
// TODO Auto-generated method stub | |
// place your code here | |
HttpServletRequest req = (HttpServletRequest) request; | |
HttpSession session = req.getSession(); | |
if (session != null && session.getAttribute("user") == null){ | |
((HttpServletResponse)response).sendRedirect("Login"); | |
} | |
else{ | |
// pass the request along the filter chain | |
chain.doFilter(request, response); | |
} | |
} | |
/** | |
* @see Filter#init(FilterConfig) | |
*/ | |
public void init(FilterConfig fConfig) throws ServletException { | |
// TODO Auto-generated method stub | |
} | |
} |
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