Jsp page Exception Handling

Source: Internet
Author: User

In this article, I will explain to you how a jsp (preferred for SUN Enterprise Applications) page exception (Exceptions) throws (Throw) and captures these Exceptions, in this way, you can obtain more favorable information in the jsp (preferred for SUN Enterprise Applications) design.
First, what are Exceptions? As we all know, Exceptions is an exception that may occur anywhere in the program. For example, if you try to connect to a database but the database is closed, an exception is generated.
How to capture (throw) an exception? We can use the following expression:
<%
Try {
// Code which can throw can exception
} Catch (Exception e ){
// Exception handler code here
}
%>
Of course, another useful method is to specify a special exception processing page, which will be used when exceptions occur. This is what I will discuss below.
Create three pages: 1.Form.html (simple age input basket) code:

<Html> 2: formhandler.jsp(the first choice for sunenterprise-level applications (metadata (age sent by form.html) code is as follows:





<% @ Page errorPage = "ExceptionHandler. jsp (preferred for SUN Enterprise Applications) "%> 

Note: (1) <% @ page errorPage = "ExceptionHandler. jsp (preferred for SUN Enterprise Applications) "%> specifies an exception handling page, which must be the first line in jsp (preferred for SUN Enterprise applications. (2)

<% Int age; age = Integer. parseInt (request. getParameter ("age"); %> is to get age (String class) and convert it to int (class ). <p> Your age is: <% = age %> years. </p>
The output is the age you just entered. Now the exception may occur. If you do not enter a number, such as zsa, can String be converted to an int?
3. The ExceptionHandler. jsp (preferred for SUN Enterprise Applications) (Handling exceptions) code is as follows:
<%@ page isErrorPage="true" import="java.io.*" %>
Note: <% @ page isErrorPage = "true" %> indicates that when jsp (preferred for SUN Enterprise Applications) declares an errorPage, isErrorPage = "true;
<% Out. println ("<! -- "); StringWriter sw = new StringWriter (); PrintWriter pw = new PrintWriter (sw); exception. printStackTrace (pw); out. print (sw); sw. close (); pw. close (); out. println ("-->"); %> used the PrintWriter and StringWrighter classes, so you have to declare: import java. io. * In your jsp (preferred for SUN Enterprise Applications) Program; that is:

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.