Exercise-Servlet Filter Tutorial
8 pages
English

Exercise-Servlet Filter Tutorial

-

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
8 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

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 _______________________________________________________________ © Copyright 2003, OnsiteSeminar LLC J2EE Development using WebSphere Studio Application Developer v5.0 All rights reserved, no unauthorized duplication Student Exercise Guide E-1 Revision date: June 15, 2003 J2EE Development using WebSphere Studio Application Developer Student ...

Sujets

Informations

Publié par
Nombre de lectures 68
Langue English

Exrait

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
_______________________________________________________________© Copyright 2003, OnsiteSeminar LLC J2EE Development using WebSphere Studio Application Deve loper v5.0 All rights reserved, no unauthorized duplication Student Exercise Guide E1 Revision date: June 15, 2003
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:
_______________________________________________________________© Copyright 2003, OnsiteSeminar LLC J2EE Development using WebSphere Studio Application Deve loper v5.0 All rights reserved, no unauthorized duplication Student Exercise Guide E2 Revision date: June 15, 2003
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:
import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; import javax.servlet.ServletException;
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:
FilterConfig filterConfig; __ 5)method. It wasIn the source code for our GenericFilter class, locate the doFilter created by the filter wizard. The method should appear as:  public void doFilter(final ServletRequest request, final ServletResponse response, FilterChain chain)  throws IOException, ServletException  {  chain.doFilter(request,response);  } Above, we use the passed into our Filter to call the next filter in the Filter Chain FilterChain when the Container invokes the method of this Filter. If there is no more filters doFilter available, the container will make sure that the target of the original request is called. __6) To of this class, we add a utility method that will be used to get thesimplify extension current FilterConfig. Add the following method to your source:  public void setFilterConfig(final FilterConfig filterConfig)  {  this.filterConfig=filterConfig;  } _______________________________________________________________© Copyright 2003, OnsiteSeminar LLC J2EE Development using WebSphere Studio Application Deve loper v5.0 All rights reserved, no unauthorized duplication Student Exercise Guide E3 Revision date: June 15, 2003
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.
__9)Use CtrlS to save your changes. Writing your first Filter Our very first Filter will write out messages to the console whenever it is invoked. We want it to tell us when its entered and exited. The only thing we want the Filter to do at this time is to add the text 'Hello World' as a Request attribute by the keyname of 'hello'. In order to implement this Filter we will extend the GenericFilter created above. __1) Highlight the Java Source folder in theServletFilterproject, right click the mouse and selectNewà Filterthe menu. The Filter wizard panel is displayed and enter from “ ” as the filter name and use as the Superclass. Click HelloWorldFilter GenericFilter Finishbutton. __2) The new filter will be opened in the Java editor. Now, we need to add the following package statements to it: import import javax.servlet.*; import java.io.*; import javax.servlet.FilterChain; __3) Now, we need to override the method in our class doFilter HelloWorldFilter that is inherited from the class. Input the following for this method: GenericFilter  public void doFilter(final ServletRequest request, final ServletResponse response, FilterChain chain)  throws IOException, ServletException  {  System.out.println("Entering Filter"); _______________________________________________________________© Copyright 2003, OnsiteSeminar LLC J2EE Development using WebSphere Studio Application Deve loper v5.0 All rights reserved, no unauthorized duplication Student Exercise Guide E4 15, 2003Revision date: June
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 CtrlS 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
_______________________________________________________________© Copyright 2003, OnsiteSeminar LLC J2EE Development using WebSphere Studio Application Deve loper v5.0 All rights reserved, no unauthorized duplication Student Exercise Guide E5 Revision date: June 15, 2003
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 CtrlS 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 WEBINF 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 webapplication 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
_______________________________________________________________© Copyright 2003, OnsiteSeminar LLC J2EE Development using WebSphere Studio Application Deve loper v5.0 All rights reserved, no unauthorized duplication Student Exercise Guide E6 Revision date: June 15, 2003
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:
<filter><filtername>HelloWorldFilter</filtername><displayname>HelloWorldFilter</displayname><filterclass>filters.HelloWorldFilter</filterclass></filter>. . <filtermapping><filtername>HelloWorldFilter</filtername><urlpattern>/Filter.jsp</urlpattern>
</filtermapping>
4)
Use CtrlS to save your changes.
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:
 These lines reflect the entering and exiting within the HelloWorldFilter servlet filter prior to the Filter.jsp being accessed. _______________________________________________________________© Copyright 2003, OnsiteSeminar LLC J2EE Development using WebSphere Studio Application Deve loper v5.0 All rights reserved, no unauthorized duplication Student Exercise Guide E7 Revision date: June 15, 2003
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.
_______________________________________________________________© Copyright 2003, OnsiteSeminar LLC J2EE Development using WebSphere Studio Application Deve loper v5.0 All rights reserved, no unauthorized duplication Student Exercise Guide E8 Revision date: June 15, 2003
  • Accueil Accueil
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • BD BD
  • Documents Documents