There are two ways to handle exceptions in JSP: by errorPage and isErrorPage attributes of page directive and by <error-page> element in web.xml file. 

Let's first get to know about the formal way. 

 

Handling Exception using page directive attributes

Two attributes can be used in JSP: errorPage and isErrorPage.

 

errorPage 

 <%@page errorPage="url of the error page"%>

isErrorPage 

 <%@page isErrorPage="true"%>

If the attribute of isErrorPage is true, it is designated as an error page. 

 

Let's discuss it more with some examples. 

In this code, there is no value in the name parameter; yet, we didn't handle the exception, so you will encounter the HTTP status code 500.

<%@ page contentType = "text/html; charset=utf-8" %>
<html>
<head><title>Print out Parameter</title></head>
<body>

Parameter value of name: <%= request.getParameter("name").toUpperCase() %>

</body>
</html>

with errorPage attribute

<%@ page contentType = "text/html; charset=utf-8" %>
<%@ page errorPage = "/error/viewErrorMessage.jsp" %>
<html>
<head><title>Print out Parameter</title></head>
<body>

Parameter value of name: <%= request.getParameter("name").toUpperCase() %>

</body>
</html>

vieErrorMessage.JSP

<%@ page contentType = "text/html; charset=utf-8" %>
<%@ page isErrorPage = "true" %>
<html>
<head><title>Error</title></head>
<body>

Sorry, an error occurred while processing your request.<br>
<p>
Error type: <%= exception.getClass().getName() %> <br>
Error message: <b><%= exception.getMessage() %></b>
</body>
</html>

You can also use try ~ catch like Java.

<%@ page contentType = "text/html; charset=utf-8" %>
<html>
<head><title>Print out Parameter</title></head>
<body>

name parameter value: 
<% try { %>
<%= request.getParameter("name").toUpperCase() %>
<% } catch(Exception ex) { %>
Wrong name parameter.
<% } %>
</body>
</html>

 

The error data should exceed 512 bytes. If the length of the error page is less than 512 bytes, Internet Explorer does not print the error page that this page prints It outputs its own 'HTTP error message' screen. To print the contents of an error page correctly in Internet Explorer, you need to include the same in responding to this comment. 

 

errorPage 02

<%@ page contentType = "text/html; charset=utf-8" %>
<%@ page buffer="1kb" %>
<%@ page errorPage = "/error/viewErrorMessage.jsp" %>
<html>
<head><title>Buffer flush Exception</title></head>
<body>

<%  for (int i = 0 ; i < 300 ; i++) { out.println(i); }  %>

<%= 1 / 0 %>

</body>
</html>

So far, we have discussed the formal way of handling exceptions, but unless you have just several pages, you will need to use the latter way to handle exceptions using error-page element en web.xml file.

Instead of the errorPage directive, the error page can be specified in the web.xml file of <error-page> tags.

The web.xml file has to be in WEB-INF folder. Anything you need, to handle the exceptions, you have to put in the web.xml file. Here is the web.xml file.

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	id="WebApp_ID" version="4.0">
	<display-name>jspproject</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

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

	<error-page>
		<error-code>500</error-code>
		<location>/error/error500.jsp</location>
	</error-page>

	<error-page>
		<exception-type>java.lang.NullPointerException</exception-type>
		<location>/error/errorNullPointer.jsp</location>
	</error-page>

</web-app>

 

'Java > JSP' 카테고리의 다른 글

JSP) JavaBean  (0) 2022.08.31
JSP) Action tags - include  (0) 2022.08.31
JSP) Action tags - forward  (0) 2022.08.30
JSP) Scopes - Page / Request Scopes  (0) 2022.08.29
JSP) Session  (0) 2022.08.29

+ Recent posts