5 Replies Latest reply: Mar 28, 2011 2:26 AM by EJP RSS

    httpservletresponse sendRedirect giving 404 error

    799435
      Hi,

      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. :)
        • 1. Re: httpservletresponse sendRedirect giving 404 error
          EJP
          You can achieve all that directly via web.xml, you don't need to code it.
          • 2. Re: httpservletresponse sendRedirect giving 404 error
            799435
            using -
            public class Class1 extends HttpServlet
            {   
              private ServletConfig config;
            
              public void init(ServletConfig config)throws ServletException
              {
               this.config=config;
              }
            
               public void service (HttpServletRequest request, HttpServletResponse response)
                  throws IOException
               {
            
                 response.setContentType("text/html");     
                 ServletOutputStream l_out = response.getOutputStream();     
            
                 response.sendError(503); 
               }
            }
            
            web.xml
            
            <?xml version="1.0"?>
            <!DOCTYPE web-app
                PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                "http://java.sun.com/dtd/web-app_2_3.dtd">
            <web-app>
            
                 <servlet>
                  <servlet-name>Class1</servlet-name>
                  <servlet-class>project2.Class1</servlet-class>
               </servlet>   
            
               <servlet-mapping>
                  <servlet-name>Class1</servlet-name>
                  <url-pattern>/*</url-pattern>
               </servlet-mapping>
            
               <error-page>
                <error-code>503</error-code>
                <location>/jsp/error/custom.html</location>
            </error-page>
            
            </web-app>
            already tried. Didnt work - i was getting the error
            for the error-code: "503" causes an infinite loop condition as it throws the same code for which it has been mapped in the server logs.

            Edited by: Tom on Mar 28, 2011 12:48 PM
            • 3. Re: httpservletresponse sendRedirect giving 404 error
              EJP
              So you need to make sure the page mapped to 404 actually exists.
              • 4. Re: httpservletresponse sendRedirect giving 404 error
                799435
                my mistake.. edited the post.
                server log mentioned error 503 .
                • 5. Re: httpservletresponse sendRedirect giving 404 error
                  EJP
                  So you need to adjust your servlet mapping so that Class1 doesn't map to any error pages.