?
Solved

response.sendRedirect() not working !

Posted on 2004-07-30
7
Medium Priority
?
470 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Accepted Solution

by:
TomBruser earned 600 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 200 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

Independent Software Vendors: 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

Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

801 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