Summary of JSP website development 10 and jsp website development 10

Source: Internet
Author: User

Summary of JSP website development 10 and jsp website development 10

After the introduction in the previous article, I believe that you must have a concept for the filters provided by JSP. In this article, we will take a look at how to create multiple filters, if there are two or more filters with the same filter rules, what is the execution sequence of these filters? The answer is to execute the statement in the order declared in web. xml, that is, first execute the declaration, and then execute the declaration. The text description may not be clear. Let's verify it with a program.

1. Create a Filter class:

Because we need to complete the sequential judgment of multiple filters during execution, we need to create at least two Filter classes.

A. firstFilter. java:

Public class firstFilter implements Filter {public void destroy () {System. out. println ("Destory ----- first");} public void doFilter (ServletRequest request, ServletResponse response, FilterChain arg) throws IOException, ServletException {System. out. println ("start ----- first"); arg. doFilter (request, response); // The page will remain loaded without this method. System. out. println ("end ----- first");} public void init (FilterConfig arg0) throws ServletException {System. out. println ("Init ----- first ");}}

B. secondFilter. java:

public class secondFilter implements Filter {    public void destroy() {        System.out.println("Destroy----second");    }    public void doFilter(ServletRequest request, ServletResponse response,            FilterChain chain) throws IOException, ServletException {        System.out.println("start----second");        chain.doFilter(request, response);        System.out.println("end----second");    }    public void init(FilterConfig filterConfig) throws ServletException {        System.out.println("Init----second");    }}

2. web. xml declaration:

To achieve the above effect, we need to ensure that the requests matching the two filters are consistent in the declared filter rules.

<? 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 firstFilter declaration --> <filter-name> firstFilter </filter-name> <! -- Filter Name --> <filter-class> cn. imcook. filter. firstFilter </filter-class> <! -- Specify the address of the newly created filter object --> </filter> <! -- Filter object secondFilter declaration --> <filter-name> secondFilter </filter-name> <filter-class> cn. imcook. filter. secondFilter </filter-class> </filter> <! -- Rule Declaration of the filter firstFilter --> <filter-mapping> <filter-name> firstFilter </filter-name> <! -- Specify the filter object for the rule --> <url-pattern>/index. jsp </url-pattern> <dispatcher> REQUEST </dispatcher> <! -- There are four optional values. The default value is REQUEST --> </filter-mapping> <! -- Rule Declaration of the filter secondFilter --> <filter-mapping> <filter-name> secondFilter </filter-name> <url-pattern>/index. jsp </url-pattern> <dispatcher> REQUEST </dispatcher> </filter-mapping> <! -- Error handling --> <error-page> <error-code> 404 </error-code> <location>/error404.jsp </location> </error-page> <error- page> <error-code> 500 </error-code> <location>/error500.jsp </location> </error-page> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> <login-config> <auth-method> BASIC </auth-method> </login-config> </ web-app>

3. Start the project test:

Enter http: // localhost: 8080/HelloWord/index. jsp in the address bar of the browser to observe the output of the myeclipse console:

  

At this point, I think you should have understood the principle of the execution sequence of multiple filters.

4. 404 and 500 Error Filtering:

In the web. xml above, we must have seen how to implement two filter listening statements about 404 and 500 errors? We only need to declare the two errors in our web. xml, and the system will start listening. Once an error occurs, the system will jump to the configured error reminder page.

A. error404.jsp:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

B. error500.jsp:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

C. Test. jsp used to Test error 500:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

5. effect:

A. When we enter a page in the address bar that does not exist:

  

B. When we enter http: // localhost: 8080/HelloWord/Test. jsp in the address bar:

  

The construction of the Filter class provided by JSP is summarized here. For specific use of these functions, you still need to make a good effort. If you have any questions, please leave a message for discussion.

 

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.