This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes JSP and the fly likes How to forward a page from catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to forward a page from catch block" Watch "How to forward a page from catch block" New topic

How to forward a page from catch block

zimbu bolleddu
Ranch Hand

Joined: Jan 14, 2008
Posts: 35
Hi all,

Here is my requirement:


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

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.

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


description The server encountered an internal error () that prevented it from fulfilling this request.


org.apache.jasper.JasperException: Exception in JSP: /holydays.jsp:16

14: catch(NullPointerException e){
15: %>
16: <jsp:forward page="../failure.jsp"/>
17: <%
18: }
19: %>


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>
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

You're not reporting any errors from your catch block.

At a bare minimum, you should add:
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
I agree. Here's the link:
subject: How to forward a page from catch block
It's not a secret anymore!