[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

response.sendRedirect() not working !

Posted on 2004-07-30
7
Medium Priority
?
482 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 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
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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 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