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: