Jsp automatically jumps when an error page is created, and jsp automatically jumps

Source: Internet
Author: User

Jsp automatically jumps when an error page is created, and jsp automatically jumps

In common web sites, a function is often found: When a page error occurs, an error message is automatically displayed on a page.

To complete the operation on the error page, you must meet two conditions:

1. Specify the page to jump when an error occurs, which is specified through the errorPage attribute;

2. The error handling page must be clearly identified and specified through the isErrorPage attribute.

The following is the error page: errorPage. jsp

The Code is as follows:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Page isErrorPage = "true" %>

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> My JSP 'errorpage. jsp 'starting page </title>
</Head>
<Body>
<H1> A program error occurs. Contact the administrator! </H1>
</Body>
</Html>

Of course, you can also specify global error handling in the entire virtual directory. To achieve this effect, you must modify the web. xml file and add an error page to it.

Global error handling can handle two types of errors: HTTP code errors, such as 404 or 500, and NullPointerException.

Modify the web. xml file and add error handling code as follows:

<Error-page>
<Error-code> 500 </error-code>
<Location>/error/errorPage. jsp </location>
</Error-page>
<Error-page>
<Error-code> 404 </error-code>
<Location>/error/errorPage. jsp </location>
</Error-page>
<Error-page>
<Exception-type> java. lang. NullPointerException </exception-type>
<Location>/error/errorPage. jsp </location>
</Error-page>

 

Example:

Enter "http: // localhost: 8080/jsp/1tiaozhuan_a.jsp" in the invalid JSP page. The following message is displayed:

 

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.