• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using ${exception.message}

 
SanjayKrishna Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while giving mock exam I got below question and I am unable to understand why
${exception.message} is not valid answer?

Question:- A JSP page myerror.jsp has been invoked as a result of an exception from another JSP page.
How can you print the message contained in the exception instead of displaying the whole stack trace in myerror.jsp?

Why ${exception.message} is not correct.

Thanks!!!
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjay there is no object named exception in EL. That implicit object is only available in scripting tags (in error pages of course). In EL, to get the exception object in an error page, you'll have to go through the pageContext object i.e. ${pageContext.exception.message}
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Sanjay there is no object named exception in EL. That implicit object is only available in scripting tags (in error pages of course). In EL, to get the exception object in an error page, you'll have to go through the pageContext object i.e. ${pageContext.exception.message}


${pageContext.exception.message} shows exception message if the JSP is marked with <%@ page isErrorPage="true" %>.
If not, the implicit exception is not available to pageContext.
Am I correct?
 
Chinmaya Chowdary
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai.
${pageContext.exception.message} shows exception message if the JSP is marked with <%@ page isErrorPage="true" %>.
If not, the implicit exception is not available to pageContext.
Am I correct?


No need to declare page directive
<%@ page isErrorPage="true" %>
in jsp, if we use EL. Here 'pageContext' object always available for jsp. Through which we can get 'exception' object and 'message'.
 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if the JSP is marked with <%@ page isErrorPage="true" %>.
If not, the implicit exception is not available to pageContext.
Am I correct?



its true only for jsp implicit object exception ,

<%= exception.getMessage() %>

you can use the implicit object exception ,
only if you have explicity defined page directive : <%@ page isErrorPage="true" %>


 
SanjayKrishna Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit, Sai, Chinmaya and Priya.

I understood my mistake.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic