Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what if jspbean creates an exception

 
ajay vasudevan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear all,

/*i accidentally pressed the ass new topic when i pressed the tab key.

sorry for misguiding u people*/

consider the code,

<%@ page errorPage='myerror.jsp'%>
<html>
<jsp:useBean id='myClass' class='CLass1'scope='request'>
<%
int i =0;/*Note here*/
%>
<jsp:setProperty name='myClass' property='i' value=<%=i%>/>
<jsp:getProperty name='myClass' property='i'/>
<html>

/*my bean class*/
class Class1
{
private int i ;
public void setI(int i)
{
this.i = 100/i; /*Arithematic exception when i = 0*/
}
public int getI()
{
return i;
}
}

i want the exception to be seen on the myerror.jsp page

how can i do it ?


please reply,

ajay vasudevan
scjp 1.4
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must create corresponding myerror.jsp
And define directive there
<%@ page isErrorPage="true" %>

Then you can use on this page "exception" implicit variable to get the details of the exception.
But remember: these directives override the error-page settings from the deployment descriptor
 
ajay vasudevan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Contrary to Mr Sergey has pointed out,

you will NOT get the exception object in the errorpage myerror.jsp as the exception becomes a null on the page.
(as i think the exception is set in the jsp:setProperty script)

but if the code to setting the bean is written within scriplets
it does work.

but i don't like using the scriplet in this case.

please can anyone guide me to a solution.


regards,

ajay vasudevan.
scjp 1.4
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use pageContext to reach JSP implicit variables in JSTL expressions

To clarify, here is the code i tested (yes, i tested it!)

TestClass1.java
-----------------------------------------
package com.test;
public class TestClass1 {
private int i;
public void setI(int i) {
this.i = 100 / i; /*Arithematic exception when i = 0*/
}
public int getI() {
return i;
}
}


test.jsp
-----------------------------------------------
<%@ page errorPage='myerror.jsp'%>
<html>
<jsp:useBean id='myClass' class='com.test.TestClass1' scope='request'/>
<%
int i =0;/*Note here*/
%>
<jsp:setProperty name='myClass' property='i' value='<%=i%>'/>
<jsp:getProperty name='myClass' property='i'/>
<html>

myerror.jsp
-----------------------------------------------
<%@ page isErrorPage="true" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
Hi! <c ut value="${pageContext.exception}"/>



Hope, it helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic