Solved

response.sendRedirect() not working !

Posted on 2004-07-30
7
445 Views
Last Modified: 2008-03-03
Any idea, why the page is not forwarded to Errorpage.jsp when I call the ExceptionBean's method processError ?


This is a part of my JSP
--------------------------  

Vector rcdvec =null;
   
ExceptionBean exceptionBean= new ExceptionBean();

try       {

      ContextBean contextBean = new ContextBean();
      
      HistorySessionHome home =
      (HistorySessionHome)contextBean.getHomeObject("HISTORYSESSION",HistorySessionHome.class);
      
      HistorySession localRCS = home.create();
      
      ResponseBean resp = new ResponseBean(-1,new Vector(),new String());
      
      resp = localRCS.historySearchByCR(req);
      
      if (resp.getStatusCode() != StatusCode.SUCCESS)
            throw new Exception(" Your CR Number did not match any of our records in the Database ! Please try again. ");
      
      rcdvec = resp.getChildRCData();
 
      }
      catch (Exception ex)
      {
      
      
            exceptionBean.processError(ex,session,response);
      }


-----------------

A part of my Exception Bean: (JAVA BEAN)


public void processError(Exception ex,HttpSession session,HttpServletResponse response) throws Exception

{
      

      System.out.println("INside Normal Exception : before response.sendReidrect()");
      
      session.setAttribute("errorstring","I was inside ExceptionBean"+ex.getCause().getMessage());
      response.sendRedirect("/Errorpage.jsp");


}
------------------------------------
0
Comment
Question by:basicblue2k
7 Comments
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
I test it works fine.
But something you may modify is

response.sendRedirect("/contextPath/Errorpage.jsp");
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
OK.

     catch (Exception ex)
     {
     
     
          exceptionBean.processError(ex,session,response);
     }

should be

     catch (Exception ex)
     {
     
     
          exceptionBean.processError(ex,session,response,request);
     }





and





public void processError(Exception ex,HttpSession session,HttpServletResponse response) throws Exception

{
     

     System.out.println("INside Normal Exception : before response.sendReidrect()");
     
     session.setAttribute("errorstring","I was inside ExceptionBean"+ex.getCause().getMessage());
     response.sendRedirect("/Errorpage.jsp");


}

should be

public void processError(Exception ex,HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception

{
     

     System.out.println("INside Normal Exception : before response.sendReidrect()");
     
     session.setAttribute("errorstring","I was inside ExceptionBean"+ex.getCause().getMessage());
     response.sendRedirect(request.getContextPath()+"/Errorpage.jsp");


}
0
 
LVL 11

Assisted Solution

by:sompol_kiatkamolchai
sompol_kiatkamolchai earned 50 total points
Comment Utility
My first comment is not quite good to hard code context path in source since it can be change when we deploy on another server with different context path.

My second comment solve this problem by getting it from request.getContextPath() which seems more flexible.

Hope this help.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Accepted Solution

by:
TomBruser earned 150 total points
Comment Utility
The code works fine for me as well, but maybe you'll have better luck trying another approach if this applies to your application:

Use the page directive in your JSP to define an error page:

<%@ page errorPage="Errorpage.jsp" %>

In your Errorpage.jsp, use the page directive to identify it as an error page:

<%@ page isErrorPage="true" %>

From Errorpage.jsp, you will be able to access an object called: exception

This is the Exception that was thrown from the page generating the original error.
0
 
LVL 1

Assisted Solution

by:phupha
phupha earned 50 total points
Comment Utility

You should try to move this code section to the top of your page.
I guess that it will work normally.


And this is a reason !!!!!
===============

response.sendRedirect();
this method will send HTTP REDIRECT or HTTP/1.0 302 Moved Temporarily to client

after your JSP response buffer is full it will send data with [HTTP/1.0 200 OK] header to client (Tomcat doesn't wait until finished full JSP Page - to improve performance) In this case response.sendRedirect() won't work anymore.




0
 

Author Comment

by:basicblue2k
Comment Utility
Thanks for all your responses guys !!!

sompol_kiatkamolchai 's response was correct, but just that I throw exceptions from begining and end of the pages as well, I cant afford to use response.sendRedirect() as said by PHUPHA. I feel TOMBRUSER's answer was more generic.

Sorry to sompol, even though he found solution for my code, I feel Tom's response was more  general to be used in any scenario.

0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
Do not be worried basicblue2k. Tom's answer is better really.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
A procedure for exporting installed hotfix details of remote computers using powershell
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now