开发者

custom 503 error page thrown from servlet java

This is pretty simple and straightforward. I want to throw a 503 error from the servlet side.

response.sendError(503); 

When this is thrown, I need it to hit a custom error page. Basically a 503 error page itself, but with a few modifications.

Say I have 503.html, and I added

<error-page>
    <error-code>503</error-code>
    <location>/503.html</location>
</error-page>

in web.xml.

I created a war file, with a servlet which throws the 503 error, and web.xml with this content. I kept the 503.html in the parent folder location. (Should I keep it elsewhere ?)

I deployed the app in WLS, but this custom 503.html is not getting hit. I am getting the generic 503 error.

Am I missing something?

My code is below:

webapp1.war

->web-inf

->web-inf->classes->prject4->Class1.class

->web-inf->jsp->error->custom.html

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>
      <serv开发者_开发知识库let-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>/WEB-INF/jsp/error/custom.html</location>
</error-page>

</web-app>

class1.java

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); 
   }
}


Ok, this was a minor error which I didn't figure out in the beginning.

In my web.xml the servlet-mapping was given as /*, which was causing an infinite loop condition as it throws the same code for which it has been mapped. So I had to adjust the servlet mapping so that Class1 doesn't map to any error pages - like say /images/*.

And then everything started working fine. :)


You can also try handling it with custom Error Handler.

public void service (HttpServletRequest request, HttpServletResponse response)
      throws IOException
 {
      try
      {
          //some error generating code
           throw new Exception("503_Exception");
      }
      catch(Exception e)
      {
          response.sendRedirect(HandleError.handle(e, request));
      }
 }

A separate class to handle errors. This can handle different types of errors. You can add functionality to log stacktrace, send out emails if something is wrong etc.

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", Constants.EINVALSESSION);
            return "/503.html";
         }


         return "/default_error.html";
    }
}


If you are using Maven as your project build tool then it will look in the src/main/webapp directory, so for example our config looks like this:

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/error/error404.html</location>
</error-page>

and our error404.html sits in the folder:

${PROJECT_NAME}/src/main/webapp/WEB-INF/jsp/error/

If your not using Maven the path in the location will have a base directory of wherever you put your index.jsp


I guess there's a minimum limit on the number bytes your custom error page has. The lower limit is usually 512 Bytes. See Important note for your Custom error pages. I've seen this behavior in Google-Chrome too when using Tomcat.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜