Java Web Foreground exception handling

Source: Internet
Author: User
Tags error status code exception handling xmlns java web

When doing a Java Web program, if the error, often printed on the page on the wrong stack memory information, in the development phase of the debugging program is very helpful, but in the operating environment, such a treatment is not friendly, not developers see will be dumbfounded.

Here is a simple way to deal with the error page.

First, create two common HTML error message pages:

404.html

<body>
   所访问的资源不存在:对不起,所请求的资源不存在 ! <br>
</body>

500.html

<body>
         服务器内部错误:对不起,服务器忙! <br>
     </body>

Second, configure Web.xml

<?xml version= "1.0" encoding= "UTF-8"
<web-app version= "2.4"
xmlns= "http://java.sun.com/" Xml/ns/j2ee "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemalocation=" http://          JAVA.SUN.COM/XML/NS/J2EE
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
<servlet>
<description>this is the description's my Java EE component</description>
<display-name>thi S is the display name of my Java EE component</display-name>
<servlet-name>errservlet</servlet-n      Ame>
<servlet-class>lavasoft.errtest.errservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>errservlet</servlet-name>
<url-patter          N>/servlet/errservlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welCome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-c        Ode>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error -page>
</web-app>

Third, create a test servlet, used to throw 500 wrong, hehe.

package lavasoft.errtest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ErrServlet extends HttpServlet {
   public void doGet(HttpServletRequest request,  HttpServletResponse response)
       throws ServletException, IOException {
     response.setContentType("text/html");
     throw new RuntimeException("------");
   }
}

Four, test

1, when access to non-existent resources, the server will return 404 error status, this will automatically turn to 404 of the corresponding error page 404.html, sent it to the client.

2, when the server processing error, will return 500 error status code, so automatically to 500 corresponding error page 500.html, sent it to the client.

In this way, without much effort, the unusual unfriendly problem was solved!

Of course, this is only the simplest one of the most lazy way to deal with, there is a way to recommend: that is, in a good hint of the page does not directly display error stack information, only when the request to see the error details only click to display, the effect is achieved through JS.

This article supporting source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.