Java Servlet Filter Annotation.
What is Servlet Annotation?
- Up to Servlet version 2.5 you had to set the sequential execution of filters and the filters URL in the web descriptor, web.xml.
- It is the sequence of the filter-mapping tags that decides the chain - not the filter tags.
Example of a such configuration in web.xml:
... <filter> <filter-name>LogB</filter-name> <filter-class>web.LogB</filter-class> </filter> <filter> <filter-name>LogA</filter-name> <filter-class>web.LogA</filter-class> </filter> <filter-mapping> <filter-name>LogA</filter-name> <servlet-name>Login</servlet-name> </filter-mapping> <filter-mapping> <filter-name>LogB</filter-name> <servlet-name>Login</servlet-name> </filter-mapping> <servlet> <servlet-name>Login</servlet-name> <servlet-class>web.Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/Login</url-pattern> </servlet-mapping> ...
- The Request in filter LogA executes.
- The Request in filter LogB executes.
- The Request in servlet Login executes.
-
Java EE 6 came with a new servlet version 3.0 that provides the ability to specify this configuration by using annotation, @WebFilter().
The syntax of @WebFilter() :
@WebFilter { description=""; // Descriptive text about the servlet. WebInitParam[] initParams={}, // Used to pass filter config parameters. filterName="", // Name of the filter. [] servletNames={}, // Array of Servlet names to which this Filter applies. [] urlPatterns={}, // Used for multiple URL and other attributes set (Required). DispatcherType[] dispatcherTypes= {DispatcherType.REQUEST}, // Look at table below. boolean asyncSupported=false} // Specifies asynchronous processing or not. /* Possible dispatcher types: REQUEST: Only when the request comes directly from the client. (DEFAULT) ASYNC: Only when the asynchronous request comes from the client FORWARD: Only when the request has been forwarded to a component INCLUDE: Only when the request is being processed by a component that has been included ERROR: Only when the request is being processed with the error page mechanism */
Here is an example:@WebFilter(name = "LogA", urlPatterns = {"/Login"}, initParams = { @WebInitParam(name = "param1", value = "value1"), @WebInitParam(name = "param2", value = "value2")}) public class SimpleServlet extends HttpServlet { .... }
Example of Servlet Filter Annotation.
As selected we use Netbeans IDE and Glassfish Server.
here (needed tools can be found in the right menu on this page).
If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is FilterAnnotation).
-
We start with the Login servlet:
package web; import javax.servlet.http.*; import java.io.*; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebServlet; @WebServlet(name = "Login", urlPatterns = {"/Login"}) public class Login extends HttpServlet { @Override public void doPost(HttpServletRequest request, HttpServletResponse response) { System.out.println("Start doPost in Login"); String username = request.getParameter("username"); try { response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.println("<html><body>"); writer.println("Thank you, " + username + ". You are now logged into the system."); writer.println("</body></html>"); writer.close(); System.out.println("End doPost in Login"); } catch (Exception e) { e.printStackTrace(); } } }
For those who participate in the review: create a Servlet in Netbeans and replace generated code for the servlet with that shown above (the servlet name is Login).
- Servlet expect to get a POST type of request that has been handled by filters before this servlet.
- We set the Content type to "text/html" for the response to the client (browser) and use a PrintWriter on the response object to return text and html codes back to the client.
- We fetch the username from the request parameter, create some html code and text, include the username, and return all as a response.
-
All filters in front will handle the response before it is delivered to the client (browser).
Here is the second filter, LogB, which is the last filter before the servlet:
package web; import javax.servlet.annotation.WebFilter; @WebFilter(filterName = "LogB", urlPatterns = {"/Login"}) public class LogB implements javax.servlet.Filter { @Override public void init(javax.servlet.FilterConfig filterConfig) {} @Override public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) { System.out.println("Entered LogB doFilter()"); System.out.println("protocol is " + request.getProtocol()); System.out.println("remote host is " + request.getRemoteHost()); System.out.println("content type is " + request.getContentType()); System.out.println("content length is " + request.getContentLength()); System.out.println("username is " + request.getParameter("username")); try { chain.doFilter(request, response); } catch (Exception e) { e.printStackTrace(); } } @Override public void destroy() { } }
For those who participate in the review: create a Filter in Netbeans and replace generated code for the filter with that shown above (the filter name is LogB).
- The Web container starts to call the init() method when it is created and then call the doFilter() for any request.
- We prints on the Server log some information about data received through the request object.
- To call the Servlet in the chain we use the doFilter() method that exists in the FilterChain interface. The ServletRequest interface reference and the ServletResponse interface reference are passed to this method.
-
When the method is carried out, we will have information in response object provided by the servlet.
Filtering starts with the LogA filter:
package web; import javax.servlet.annotation.WebFilter; @WebFilter(filterName = "LogA", urlPatterns = {"/Login"}) public class LogA implements javax.servlet.Filter { private javax.servlet.ServletContext context; @Override public void init(javax.servlet.FilterConfig filterConfig) { context = filterConfig.getServletContext(); } @Override public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) { System.out.println("LogA passing request to next filter"); try { chain.doFilter(request, response); } catch (Exception e) { e.printStackTrace(); } System.out.println("The servlet has finished processing the request"); System.out.println("LogA filter is now working to process the response"); } @Override public void destroy() { } }
For those who participate in the review: create a Filter in Netbeans and replace generated code for the filter with that shown above (the filter name is LogA).
- The Web container starts to call the init() method when it is created and then call the doFilter() for any request.
- We prints on the Server log some information.
- We could of course add or change some information in the ServletRequest interface as an verification to succeeding filter or servlet, but for now we do nothing.
- To call the logB filter in the chain we use the doFilter() method that exists in the FilterChain interface. The ServletRequest interface reference and the ServletResponse interface reference are passed to this method.
-
When the method is carried out, we will have information in response object provided by the servlet and the LogB filter.
The html startup file, login.html, for the browser can be like this:
<!DOCTYPE HTML> <html> <head> <title>Login</title> </head> <body> <h1>Login</h1> Please enter your username and password <form action="/FilterAnnotation/Login" method="POST"> <p><input type="text" name="username" style="width: 100px;"> <p><input type="password" name="password" style="width: 100px;"> <p><input type="submit" value="Submit"> </form> </body> </html>
For those who participate in the review: create a HTML page in Netbeans and replace generated code for the html file with that shown above (the name of the html should be login and places in the folder web).
Creating Deployment descriptor.
- To run this Servlet with filters you have to deploy it to a web-server or a Application server. To deploy means to install the Servlet and Filters with some instruction to a such server.
-
The instructions are mainly defined to be deployment descriptors.
The standard part of the deployment descriptor should be in an
XML-file with the name web.xml. In the later version of java it is
possible to specify this in the form of annotations in front of the Servlet.
You may need to create a Deployment descriptor file, web.xml in Netbeans.
-
The contents of the web.xml file regarding servlet and Filters
should look like this:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <filter-mapping> <filter-name>LogA</filter-name> <servlet-name>Login</servlet-name> </filter-mapping> <filter-mapping> <filter-name>LogB</filter-name> <servlet-name>Login</servlet-name> </filter-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list> </web-app>
- This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a Servlet.
- As we use annotation no Servlet configurations are needed.
- As we use annotation on filter only filter-mapping is required for the sequence of the chain.
- If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
- The welcome-file tag specifies the startup for our application, which in this case and our application is the welcome file login.html. Reorganize the welcome-file-list to what is shown above.
Creating Web-server Deployment descriptor.
-
The context-root (in example /FilterAnnotation) for the application
will in most cases be specified by a server vendor deployment descriptor.
For those who participate in the review: create a deployment descriptor in Netbeans.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd"> <sun-web-app error-url=""> <context-root>/FilterAnnotation</context-root> <class-loader delegate="true"/> <jsp-config> <property name="keepgenerated" value="true"> <description> Keep a copy of the generated servlet class' java code. </description> </property> </jsp-config> </sun-web-app>
Run the application.
- For those who participate in the review: right click the Web-project and select Run.
- This results in that the application will be deployed to the server and started.
Login
Please enter your username and passwordEnter a text, select some options and press the submit button.