18 May 2016

Servlet spec - web.xml - Fix lỗi Web-app

Servlet spec - web.xml

Status

Released on (mm/dd/yyyy) : 02/06/2012

Description

JEE web specification provides a way to configure, declaratively in the web deployment descriptor ("web.xml" file), the web app. behavior when an exception occur in a web component.
Behavior can be configured to react on elements below to display a resource:
  • Java exception
  • HTTP response code

Possible configurations

Configuration below redirect user to page "/errorManagement.jsp" when an error occur.
Configuration to react on Java exception
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	
	...
	
	<!-- Define error page to react on Java exception -->
	<error-page>
		<exception-type>java.lang.Throwable</exception-type>
		<location>/errorManagement.jsp</location>
	</error-page>
	
	...	
	
</web-app>
Configuration to react on HTTP response code
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	
	...
	
	<!-- Define error page to react on HTTP response code -->
	<error-page>
		<error-code>500</error-code>
		<location>/errorManagement.jsp</location>
	</error-page>
	
	...	
	
</web-app>

Content of the error management page
<?xml version="1.0" encoding="ISO-8859-1" ?>

<%@ page language="java" 
	contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1" 
	isErrorPage="true"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Page to manage error</title>
</head>

<body>
	<%-- Log error on server side --%>
	<%
		//When the page attribute "isErrorPage" is set to "true" the exception object is available
		System.err.println("Error : " + exception.getMessage());
	%>

	<%-- Display generic error to client --%>
	<b>An error occur !</b>
</body>
</html>

1. Servlet 3.1 deployment descriptor

Java EE 7 XML schema, namespace is http://xmlns.jcp.org/xml/ns/javaee/
web.xml
Markup
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
		 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>

2. Servlet 3.0 deployment descriptor

Java EE 6 XML schema, namespace is http://java.sun.com/xml/ns/javaee
web.xml
Markup
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	      version="3.0">
</web-app>

3. Servlet 2.5 deployment descriptor

Java EE 5 XML schema, namespace is http://java.sun.com/xml/ns/javaee
web.xml
Markup
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	      version="2.5">
</web-app>

4. Servlet 2.4 deployment descriptor

J2EE 1.4 XML schema, namespace is http://java.sun.com/xml/ns/j2ee
web.xml
Markup
<web-app 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"
	      version="2.4">

  <display-name>Servlet 2.4 Web Application</display-name>
</web-app>

5. Servlet 2.3 deployment descriptor

J2EE 1.3 DTDs schema. This web.xml file is too old, highly recommend you to upgrade it.
web.xml
Markup
<!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>
  <display-name>Servlet 2.3 Web Application</display-name>
</web-app>

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang