Solved

response.sendRedirect() not working !

Posted on 2004-07-30
7
451 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
ID: 11676395
I test it works fine.
But something you may modify is

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

Expert Comment

by:sompol_kiatkamolchai
ID: 11676476
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
ID: 11676493
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Accepted Solution

by:
TomBruser earned 150 total points
ID: 11748244
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
ID: 11755804

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
ID: 11791460
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
ID: 11791523
Do not be worried basicblue2k. Tom's answer is better really.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
netstat -a in AIX unix 8 36
JSP Popup page 2 107
bean mapper frameworks 5 59
Getting NoSuchMethodError on accessing a spring mvc project 3 158
Scenario: Your operations manager has discovered an anomaly in your security system. The business will start to suffer within 15 minutes if it is a major IT incident. What should she do? We have 6 recommendations for managing major incidents (https:…
In 2017, ransomware will become so virulent and widespread that if you aren’t a victim yourself, you will know someone who is.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

21 Experts available now in Live!

Get 1:1 Help Now