stay web.xml Configure error page in

In Java Web projects, sometimes we need to give a user prompt when an error occurs, or display site maintainer information, or replace the error with another expression to ease the mood. This can be implemented using the error-page tag of Web.xml. As there are many related materials, this article is only a simple guide.  

It is important to note that Error-Page is supported as long as it is a Java Web project, regardless of the introduction of framework plug-ins, etc.  


One, according to the error number to specify the jump.  

1) Create an error handling page.  

Create “/ Web-inf /404.html” (or “/404.html”). Open “404.html” and add the contact information of the maintainer.  

2) Add the error-Page tag.  

Add “error-Page” child node to “Web-app”, and the content is as follows:

Xml code

    < error-page> & lt; error-code> 404< /error-code> & lt; location> /WEB-INF/404.html< /location> & lt; /error-page>

    3) Deployment tests.  

    Just type in a non-existent URL. You can see that the 404.html is displayed correctly.  



    Put 404.html under WebRoot/ or WebRoot/ Web-inf /.  

    Second, specify a jump according to the exception type.  

    1) Create an exception handling page (take null pointer exception as an example).  

    A) Create the servlet “Hello” under the default package “SRC/” directory.  

    B) Modify doGet(,) to call this.dopost (,) directly.  

    C) Insert 2 lines of code between comments in doPost(,). As shown below.

    Java code

      out.println(“, using the POST method”); // Test Snippet Object obj = null; Out.println (obj. ToString ());// & lt; – the test out. Println (” & lt; /BODY>” );

      Make a NullPointerException by hand.  

      D) to create “nullPointerException. HTML”, and write “nullPointerException” in the content.  

      2) Add the error-Page tag.  

      A) Add the servlet configuration as follows:

      Xml code

        < servlet> & lt; servlet-name> Hello< /servlet-name> & lt; servlet-class> Hello< /servlet-class> & lt; /servlet> & lt; servlet-mapping> & lt; servlet-name> Hello< /servlet-name> & lt; url-pattern> /servlet/Hello< /url-pattern> & lt; /servlet-mapping>

        B) Add error-Page:

        Xml code

          < error-page> & lt; exception-type> java.lang.NullPointerException< /exception-type> & lt; location> /WEB-INF/nullPointerException.html< /location> & lt; /error-page>


          3) Deployment tests.  

          After deployment, access to “http://localhost:8080/myJavaWeb/servlet/Hello”. You can see “nullPointerException. HTML page.  



Read More: