The basic summary of JSP Web site development "Nine"

Source: Internet
Author: User

This article belongs to the additional chapter, in the previous summary to everyone mentioned a login status verification effect, was completed by the session object, today I checked, JSP for us to encapsulate a filter for filter class filters, Through it we can very easily complete the previous function, and can be used in many places, very practical. Needless to say, the following is a simple introduction to the JSP provides the filter class.

1, Filter Introduction:

What is a filter? The filter filters the filter source according to the filter rules and then gets the filtered result. What is the Web filter? First, the filter source of the Web filter is the user's various requests, the filtering rule is the developer realizes to make the good, the filter result is the response. How to understand it? Everyone should have this experience, when we want to download a resource, if not logged in, this is the system will pop up the login window to force us to log in, and when we have logged in, we click on the download, we do not appear to force us to sign in the page. This is the Web interceptor knowledge that we are going to learn together today.

2, the working principle of the filter:

  

3, the life cycle of the filter:

  

Filter initialization, we add our project to Tomcat, and when Tomcat is started, the system immediately invokes the Init () method of the filter, that is, once our project is deployed, the filters we add to the project start working immediately. When we close Tomcat, our filter calls the Destroy () method to end our filtering monitoring.

4, explore the working principle of filter:

A. Create a Filter object class:

 Public classFirstfilterImplementsFilter { Public voiddestroy () {System.out.println ("Destory-----First"); }     Public voidDoFilter (servletrequest request, servletresponse response, Filterchain Arg)throwsIOException, servletexception {System.out.println ("Start-----First"); Arg.dofilter (request, response);//without this method, the page will always be in the loaded state. SYSTEM.OUT.PRINTLN ("End-----First"); }     Public voidInit (Filterconfig arg0)throwsservletexception {System.out.println ("Init-----First"); }}

The class needs to implement the Javax.servlet.Filter interface:

  

B. Web. xml File Declaration:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http/ Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <!--Filter Object declaration--<filter> <filter-name> firstfilter</filter-name><!--Filter Name--<filter-class>cn.imcook.filter.firstfilter</filter-class><!--Specify the address of our new Filter Object--</filter> <!--filter Rule declaration--<filter-mapping> <filter-name> firstfilter</filter-name><!--Specify the filter object for the rule-<url-pattern>/*</url-pattern><!--*: On behalf of all requests-<dispatcher>REQUEST</dispatcher><!--There are four values to choose from, Default is request-</filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file > </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </ Login-config></web-app>

This line of code, very inconvenient, myeclipse for us to provide a windowing operation, see the following steps:

Open our web. xml file, click Design, and go to the window operator interface:

  

Click Filter on the left to add a filter object first:

  

Then click Filter to add the filter's rule declaration (filter Mapping):

  

5. Start Tomcat, enter our project name (for example: http://localhost:8080/HelloWord/index.jsp) in the Address Explorer address bar, and observe the output of the MyEclipse console.

·

This is our exploration of the filter life cycle.

6. Filter redirection:

In this case, we just need to make a slight modification to the filter class we created, and then how do we change it? I just need to add a redirect to our Dofilter () method.

 Public classLoginfilterImplementsFilter { Public voiddestroy () {System.out.println ("Destroy"); }     Public voidDoFilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2)throwsIOException, servletexception {System.out.println ("Start-----doFilter"); //Arg2.dofilter (arg0, arg1); // redirectHttpServletRequest Requst =(httpservletrequest) arg0; HttpServletResponse Response=(HttpServletResponse) arg1; Response.sendredirect (Requst.getcontextpath ()+ "/login.jsp"); System.out.println ("End------DoFilter"); }     Public voidInit (Filterconfig arg0)throwsservletexception {System.out.println ("Init"); }}

For the JSP in the knowledge of the filter, first for everyone to summarize here, if you are interested in filter knowledge, please continue to follow the blog. Next: Execution Order of filters

The basic summary of JSP Web site development "Nine"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.