| Author |
How to forward a page from catch block
|
zimbu bolleddu
Ranch Hand
Joined: Jan 14, 2008
Posts: 35
|
|
Hi all, Here is my requirement: <% try{ String str = (String) session.getAttribute("hello"); if(str==null || str!="hello" || str.length() == 0 ) { %> <jsp:forward page="../failure.jsp"/> <% } } catch(NullPointerException e){ %> <jsp:forward page="../failure.jsp"/> <% } %> The hello session variable have set to "hello" initially. I have placed this code in one jsp file. Im getting error at catch block, I dont know whether this is correct way or not, Please help me out. My requirement is on NullPointerException forward to failure.jsp file. Thanks in advance.
|
 |
K Kiran Kumar
Ranch Hand
Joined: Jan 04, 2006
Posts: 109
|
|
Hi, What is the error you were getting in try/catch block?? One more point here is you were using (str!="hello"). You have to use str.equals("hello") but not '!=' because == and != compares the references but not the strings. Regards, Kiran.
|
 |
zimbu bolleddu
Ranch Hand
Joined: Jan 14, 2008
Posts: 35
|
|
thanks for your suggestions ,but that is for equal can I use : !(str.equals("hello")) This is my error : HTTP Status 500 - -------------------------------------------------------------------------------- type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: Exception in JSP: /holydays.jsp:16 13: 14: catch(NullPointerException e){ 15: %> 16: <jsp:forward page="../failure.jsp"/> 17: <% 18: } 19: %> Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368) root cause
|
 |
K Kiran Kumar
Ranch Hand
Joined: Jan 04, 2006
Posts: 109
|
|
Hi Zimbu, I am not sure why you are getting that error. May be there is some mismatch in braces({}) in your entire JSP page (since you have only given a part of it). You can debug further by going to work directory. tomcathome\work\Catalina\localhost\<yourprojec>\<yourjsp> Regards, Kiran.
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
You're not reporting any errors from your catch block. At a bare minimum, you should add: e.printStackTrace() to the top of the block so that you can see, in the logs, what is going wrong. Without doing that, it's going to be very hard find the problem. The accepted best practice is not to catch things like this. Rather, let the container handle these exceptions and forward context to your specified error handling page. You specify the error page in web.xml. [ May 13, 2008: Message edited by: Ben Souther ]
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Praveen Kumar
Ranch Hand
Joined: Nov 06, 2006
Posts: 133
|
|
It seems this basic way of handling the exceptions in JSP. Any uncaught exceptions should re-direct to another jsp page we will show the stack. You can google like : exceptions in jsp http://exampledepot.com/egs/javax.servlet.jsp/error.html
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: How to forward a page from catch block
|
|
|