J2EE Development using WebSphere Studio Application Developer Student Exercise Guide __________________________________________________________________________Exercise: Servlet Filters Exercise Description In this exercise, each student will have the opportunity to work with Servlet Filters. They have been implemented in the Servlet 2.3 specification. A Filter is basically a component that is invoked when a client request a resource that the Filter is mapped to, such as a URL pattern or a Servlet name. Normally, a filter is used to wrap and manipulate Request, Response or header values. In completing the exercise, the student should reference the student notebook, their individual notes and instructor answers to their questions. Exercise Duration Upon successful completion of this exercise, the student should be able to: ·Understand role of Servlet Filters ·Create a new Servlet Filter ·Link a servlet filter to a JSP document ·Modify web.xml file for servlet filters ·Test a servlet filter
J2EE Development using WebSphere Studio Application Developer Student Exercise Guide __________________________________________________________________________Exercise: Servlet Filters Exercise Instructions Build Web Project The first step will be to create a new Web Project that will contain our Servlet Filter example. The filter and the corresponding servlet will be placed in this new project. __1)a Web Perspective, then creates a new Web Application project called Open . ClickNextbutton. Enter as the Enterprise Application ServletFilter ServletFilterEAR project name. ClickFinishbutton. __2) Verify that both the Web and EAR project have been created. Build a Servlet Filter Now that the project has been created, we can begin the task of building our servlet filters. Before we write our first Filter, we’ll create a generic Filter that will hold all of the needed methods. We can then extend this generic Filter throughout this tutorial. __1) Highlight the Java Source folder in the ServletFilter project, right click the mouse and selectNewàFilterfrom the menu. The Filter wizard panel is displayed:
J2EE Development using WebSphere Studio Application Developer Student Exercise Guide __________________________________________________________________________
__
__
__
2)the filter wizard panel, enter In as the filter name andas GenericFilter filters the package name. ClickFinishbutton.
3)new filter will be opened in the Java editor. Note that the following package import The statements have been added to it:
4)that our class Note interface and that eachimplements the javax.servlet.Filter instance of this Filter will have an instance that can hold the FilterConfig given to the class by the Container upon initialization. Now, add the following variable to our filter class:
J2EE Development using WebSphere Studio Application Developer Student Exercise Guide ____________________________________________________________________________7) Now we need to update the method in our filter.Add the following statement to init our method to set the contents of our variable: init filterConfig this.filterConfig = config; This method will be called by the Container upon initializing the Filter. In the method, we make sure to keep the FilterConfig given to us by the Container is loaded into our instance variable. __8) Now we need to update the method in our filter.Add the following statement destroy to our method to set the contents of our variable: init filterConfig this.filterConfig = null;
The method we added to the source above will be called by the Container when the current Filter instance should be removed from service. This is where you perform any cleanup operations.
J2EE Development using WebSphere Studio Application Developer Student Exercise Guide __________________________________________________________________________ request.setAttribute("hello","Hello World!"); chain.doFilter(request,response); System.out.println("Exiting Filter"); } } Above, we first print out some messages, and then add a message to a Request attribute with the keyname of 'hello'. After that, we use the that the container has given us. FilterChain We then invoke the method on the next Servlet Filter in the chain. doFilter __ 4)Use CtrlS to save your changes.Creating a JSP page to test the Filte r In order to use our filter, we need to attach the servlet filter to something. We will create a real simple JSP page and place it in the Web Content folder. Upon execution, the JSP will call our new HelloWorldFilter filter. __ 1)Highlight the Web Content folder, right click the mouse and selectNewàJSP Filefrom the context menu. The JSP wizard will appear: Enter as the JSP file name and clickNextbutton. Filter
J2EE Development using WebSphere Studio Application Developer Student Exercise Guide __________________________________________________________________________
__
2)The Page Designer tool will open, click on theSourcetab and add the following content below the<BODY>tag in our new JSP file:
<HR> <P><%=request.getAttribute("hello")%></P> <P>Check your console output!</P> <HR>
The JSP page will now try to print out a Request attribute with the name "hello" to the page. __ 3)Use CtrlS to save your changes. Configure Filter existence in web.xml The link between the JSP file and the Servlet filters is defined in the web.xml deployment descriptor file. We will initiate the XML editor and open for the web.xml file and modify its contents to reflect this association. __1) First, locate the file in the WEBINF subfolder in the Web Content folder of web.xml our project. Double click on it to open it in the XML editor. Click on theFiltersand the tab following filters should be identified:
This will tell the container that our webapplication has a filter that we will refer to as , which represents the class . HelloWorldFilter filters.HelloWorldFilter
__
2)Finally, we need to link our Servlet filter to our JSP document. On theFilterspanel, locate the URL Mappings area. Presently it will have an entry for . We /HelloWorldFilter need to change this entry to / .Filter.jsp
J2EE Development using WebSphere Studio Application Developer Student Exercise Guide __________________________________________________________________________
__
__
3)Type over the URL Mapping entry and change the name toFilter.jsp. Then click on theSourcetab and locate the following entries:
Test Servlet Filter After completing all of the coding and configuration updates we are ready to begin our testing. Before we begin testing we will need to add our EAR project to the application server. __ 1)First, we need to open a Server perspective. Then, go to the Server perspective, open the Server Configuration panel and highlight the WebSphere v5.0 Test Environm ent server, right click the mouse and selectAddà ServletFilterEARadd our project to the server. to Then, restart the server. __ 2)After restarting the server, highlight the file, right click the mouse and Filter.jsp selectRun on Serveroption. The result will be several lines of output in the Console panel:
J2EE Development using WebSphere Studio Application Developer Student Exercise Guide ____________________________________________________________________________ 3)Finally, the Web Browser will display the following content:
The “Hello World” content was placed in the request object by the servlet filter and thereby verifies that it was executed successfully.