user460920
user460920

Reputation: 887

jsp errorpage--exception?

 <%@ page isErrorPage = "true"%>
    <body>
    <h2>Your application has generated an error</h2>
    <h3>Please check for the error given below</h3>
    <b>Exception:</b><br> 
    <font color="red"><%= exception.toString() %></font>
    </body>

I want to know in the JSP Expression what does exception.toString() - exception object is used for ??

Can we alternatively make a use of <%= exception.getMessage() %>??

Thanks..

Upvotes: 2

Views: 5359

Answers (2)

reevesy
reevesy

Reputation: 3472

I think you are asking whats contained in the exception variable.

exception is a JSP implicit variable

The exception variable contains any Exception thrown on the previous JSP page with an errorPage directive that forwards to a page with an isErrorPage directive.

e.g.

If you had a JSP (index.jsp) which throws an exception (I have deliberately thrown a NumberFormatException by parsing a String, obviously you wouldn't write a page that does this, its just an example)

<%@ page errorPage="error.jsp" %>
<% Integer.parseInt("foo"); //throws an exception %>

This will forward to error.jsp,

If error.jsp was

<%@ page isErrorPage = "true"%>
<body>
<h2>Your application has generated an error</h2>
<h3>Please check for the error given below</h3>
<b>Exception:</b><br> 
<font color="red"><%= exception.toString() %></font>
</body>

Because it has the

<%@ page isErrorPage = "true"%>

page directive, the implicit variable exception will contain the Exception thrown in the previous jsp

So when you request index.jsp, the Exception will be thrown, and forwarded to error.jsp which will output html like this

<body>
<h2>Your application has generated an error</h2>
<h3>Please check for the error given below</h3>
<b>Exception:</b><br> 
<font color="red">java.lang.NumberFormatException: For input string: "foo"</font>
</body>

As @JB Nizet mentions exception is an instanceof Throwable calling exception.getMessage() For input string: "foo" instead of java.lang.NumberFormatException: For input string: "foo"

Upvotes: 5

JB Nizet
JB Nizet

Reputation: 691635

toString() and getMessage() are two methods of Throwable, and you may of course call both. They don't do the same thing, though. To know what they do, read their documentation.

Upvotes: 0

Related Questions