Tom
Tom

Reputation: 145

response sendRedirect giving 404 error

I am trying to redirect to an error page upon some condition in my servlet code. But until now nothing is working out.

So I am using weblogic 10.x as my app server. am deploying apps directly into managed servers using the console.

So basically i jar them up as .war files and deploy them as webapps.

   public void doGet (HttpServletRequest request, HttpServletResponse response)
      throws IOException , ServletException
   {
      try
       {
                  throw new Exception("503_Exception") ;                 
        }
        catch(Exception e)
        {
                  response.sendRedirect(response.encodeRedirectURL(HandleError.handle(e, request)));
       }
   }   


public class HandleError{
    public static String handle(Throwable t, javax.servlet.http.HttpServletRequest request)
    { 
         String sErrorMsg = t.getMessage();

         if (sErrorMsg.equals("503_Exception")) {
            request.setAttribute("msg", "INVALID SESSION");
            return "/jsp/error/custom.html";
         }
         return "/default_error.html";
    }
}

war file structure

->jsp->error->custom.html
->web-inf
->web-inf->classes->project2->Class1.class

http://machineNAME:3030/Application3-Project2-context-root ->redirects to ->http://machineNAME:3030/jsp/error/custom.html -->> where the actual context root is missing ..

Error 404--Not Found From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1: 10.4.5 404 Not Found

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent.

If the server does not wish to make this information available to the client, the status code 403 (Forbidden) can be used instead. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address.

But if i give -

response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + HandleError.handle(e, request)));

i get Error 310 (net::ERR_TOO_MANY_REDIRECTS): in chrome and in FF error says too many re-directions .

Could someone help me out ? thanks in advance. :)

Upvotes: 0

Views: 3782

Answers (1)

Bozho
Bozho

Reputation: 597422

Appending request.getServletContext().getContextPath() in the beginning is a fine way to do it. But you are obviously entering an endless redirection loop. Do not forget to log your exceptions. Thus you will be able to see what the problem is.

Upvotes: 2

Related Questions