Solved

response.sendRedirect() not working !

Posted on 2004-07-30
7
458 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Enabling the Skype for Business Meeting Scheduler in Hybrid OWA
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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