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.
Body:
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> ol>
3) Deployment tests.
Just type in a non-existent URL. You can see that the 404.html is displayed correctly.
Note:
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>” ); ol>
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> ol>
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> ol>
3) Deployment tests.
After deployment, access to “http://localhost:8080/myJavaWeb/servlet/Hello”. You can see “nullPointerException. HTML page.
(end)
Read More:
- web.xml Web app error in file
- Error in Maven packaging web project: webxml attribute is required (or pre existing WEB-INF)/ web.xml if executing in update)
- Web Crawler: How to get the data in the web page and disguise the header, disguise as a browser to visit many times, avoid a single visit leading to IP blocked
- IOS WebView failed to load the web page. Error domain = kcferrodomaincfnetwork code = 310 “there was a problem communicating with the secure web proxy server (HTTPS). “
- The local web page in WebView failed to load with XMLHttpRequest
- Where’s Maven pom.xml Configure aliyun warehouse in
- Error in web.xml file: error while downloading
- QQ browser open automatically appear two web page solution
- Breaking the web page to prevent copying
- Activity of Android studio_ main.xml Unable to preview (gray page)
- WeChat official account web page authorized, redirect_ Uri parameter error, solution!
- Error in reading XML file IOException parsing XML document from URL
- The web project removal server reports an error, and the web project in eclipse cannot be automatically deployed to Tomcat
- Latex sets page margin, page size, page margin and geometry macro package
- There is more than one Web fragment with the same name: “spring_web”.
- About the steps to create Maven’s (WEB) dependency using in idea
- Android’s viewpager slides to determine whether the current stop page is the last page
- The requested page cannot be accessed because the related configuration data for the page is invalid
- Anaconda opens Navigator to report an error and a web page appears Navigator Error An unexpected error occurred on Navigator start-up Report