Summary of JSP website development: 11 and 11

Source: Internet
Author: User

Summary of JSP website development: 11 and 11

Following the previous summary of the filter connection, this article describes in detail how to use the dispatcher attribute of filter rules. In servlet2.5, there are four types of dispatcher attributes, the previous article has introduced the use of the error attribute. This article will detail the functions of the remaining three attributes.

1. the dispatcher attribute in servlet2.5:


An Asynchronous Operation attribute is added to servlet3.0. Because the blogger currently uses servlet2.5, this attribute is not summarized for the moment.

2. Forwarding and redirection: (recommended blog:

Forward: request. getRequestDispatcher ("a. jsp"). forward (request, response) or request. getRequestDispatcher ("a. jsp"). include (request, response)

Redirection: response. sendRedirect ("a. jsp ")

Difference: a. Forwarding is completed on the server; redirection is completed on the client;

B. Fast forwarding speed and slow redirection speed;

C. The same request is forwarded, And the redirection is two different requests;

D. The forwarded code is not executed. The redirected code is executed;

E. the forwarding address bar has not changed; the redirection address bar has changed;

F. The forwarding must be completed under the same server; the redirection can be completed under different servers.

3. Add filter rules:

Add a filter rule in web. xml for the filter we created earlier:

<filter-mapping>  <filter-name>firstFilter</filter-name>  <url-pattern>/main.jsp</url-pattern>  <dispatcher>REQUEST</dispatcher> </filter-mapping>

4. Add redirection:

Add a redirection in our firstFilter class:

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. HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; // redirect res. sendRedirect (req. getContextPath () + "/mian. jsp "); // forward using the forward method // req. getRequestDispatcher ("/main. jsp "). forward (request, response); // include method forwarding // req. getRequestDispatcher ("/main. jsp "). include (request, response); System. out. println ("end ----- first");} public void init (FilterConfig arg0) throws ServletException {System. out. println ("Init ----- first ");}}

5. Deploy the project and test:

Enter our project name in the address bar of the browser: (for example, http: // localhost: 8080/HelloWord/index. jsp)


6. Add forwarding:

1. forward by forward:

A: implemented through the Filter class. (For code, see Section 4)

B: The jsp page is used.

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; System. out. println ("index. jsp loaded "); %> <jsp: forward page ="/main. jsp "> </jsp: forward> <! -- Forward-based forwarding --> <! -- <Jsp: include page = "/main. jsp"> </jsp: include> include-based forwarding --> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

Ii. include method:

A: implemented through the Filter class. (For code, see Section 4)

B: The jsp page is used. (For the code, see section 6. 1. B)

7. Add filter rules:

Add Rules to filter forwarding events in web. xml. You only need to modify the <dispatcher> attribute.

Now, we will share with you the summary of the dispatcher attribute. If you have any questions, please leave a message to discuss it.




Related Article

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: 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.