1 / 14

JSP

JSP. Filters. 8-Jan-20. JSP - filters. A filter is an object that can transform a request or modify a response. Filters are not servlets ; they don't actually create a response. They are preprocessors of the request before it reaches a

eworkman
Télécharger la présentation

JSP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JSP Filters 8-Jan-20

  2. JSP - filters A filter is an object that can transform a request or modify a response. Filters are not servlets; they don't actually create a response. They are preprocessors of the request before it reaches a servlet, and/or postprocessors of the response leaving a servlet. Servlet and JSP Filters are Java classes that can be used in Servlet and JSP Programming for the following purposes: • To intercept requests from a client before they access a resource at back end. • To manipulate responses from server before they are sent back to the client.

  3. JSP - filters • There are are various types of filters suggested by the specifications: • Authentication Filters. • Data compression Filters • Encryption Filters . • Filters that trigger resource access events. • Image Conversion Filters . • Logging and Auditing Filters. • MIME-TYPE Chain Filters. • Tokenizing Filters . • XSL/T Filters That Transform XML Content.

  4. Filters can perform many different types of functions : • Authentication-Blocking requests based on user identity. • Logging and auditing-Tracking users of a web application. • Image conversion-Scaling maps, and so on. • Data compression-Making downloads smaller. • Localization-Targeting the request and response to a particular locale. • XSL/T transformations of XML content-Targeting web application • responses to more that one type of client. • These are just a few of the applications of filters. There are many more, such as encryption, tokenizing, triggering resource access events, mime-type chaining, and caching. • Filters are deployed in the deployment descriptor file web.xml and then map to either servlet or JSP names or URL patterns in your application's deployment descriptor. The deployment descriptor file web.xml can be found in <Tomcat-installation-directory>\conf directory. • When the web container starts up your web application, it creates an instance of each filter that you have declared in the deployment descriptor. The filters execute in the order that they are declared in the deployment descriptor.

  5. Servlet Filter Methods: A filter is simply a Java class that implements the javax.servlet.Filter interface. The javax.servlet.Filter interface defines three methods:

  6. Programming Filters The filter API is defined by the Filter, FilterChain, and FilterConfig interfaces in the javax.servlet package. You define a filter by implementing the Filter interface. A filter chain, passed to a filter by the container, provides a mechanism for invoking a series of filters. A filter config contains initialization data. The most important method in the Filter interface is the doFilter method, which is the heart of the filter. This method usually performs some of the following actions: • Examines the request headers • Customizes the request object if it wishes to modify request headers or data or block the request entirely • Customizes the response object if it wishes to modify response headers or data

  7. Programming Filters…contd • Invokes the next entity in the filter chain. If the current filter is the last filter in the chain that ends with the target servlet, the next entity is the resource at the end of the chain; otherwise, it is the next filter that was configured in the WAR. It invokes the next entity by calling the doFilter method on the chain object (passing in the request and response it was called with, or the wrapped versions it may have created). Alternatively, it can choose to block the request by not making the call to invoke the next entity. In the latter case, the filter is responsible for filling out the response. • Examines response headers after it has invoked the next filter in the chain • Throws an exception to indicate an error in processing In addition to doFilter, you must implement the init and destroy methods. The init method is called by the container when the filter is instantiated.

  8. JSP Filter Example1: Following is the JSP Filter Example that would print the clients IP address and current date time each time it would access any JSP file. This example would give you basic understanding of JSP Filter, but you can write more sophisticated filter applications using the same concept:

  9. Example…..contd..: Compile LogFilter.java in usual way and put your LogFilter.class class file in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes.

  10. JSP Filter Mapping in Web.xml: Filters are defined and then mapped to a URL or JSP file name, in much the same way as Servlet is defined and then mapped to a URL pattern in web.xml file. Create the following entry for filter tag in the deployment descriptor file web.xml

  11. The above filter would apply to all the servlets and JSP because we specified /* in our configuration. You can specify a particular servlet or JSP path if you want to apply filter on few servlets or JSP’s only. Now try to call any servlet or JSP in usual way and you would see generated log in your web server log. You can use Log4J logger to log above log in a separate file.

  12. Using Multiple Filters: Your web application may define several different filters with a specific purpose. Consider, you define two filters AuthenFilter and LogFilter. Rest of the process would remain as explained above except you need to create a different mapping as mentioned below:

  13. Filters Application Order: The order of filter-mapping elements in web.xml determines the order in which the web container applies the filter to the servlet. To reverse the order of the filter, you just need to reverse the filter-mapping elements in the web.xml file. For example, above example would apply LogFilter first and then it would apply AuthenFilter to any servlet but the following example would reverse the order:

  14. THANK YOU…

More Related