Case Analysis of Exception Handling in JSP Learning

  • 2021-07-26 08:38:37
  • OfStack

In this paper, an example is given to describe the method of JSP exception handling. Share it for your reference. The details are as follows:

Handling exceptions that an JSP program may generate can be done from several angles: it can be specific to code, specific files, or the type of error.

For specific code, you can use the < c:catch > Label.

For specific files, this can be done using the isErrorPage and errorPage attributes in the page directive of JSP.

For specific types of errors, you can use the web. xml configuration file.

1 Use < c:catch > Perform exception handling

< c:catch > Tags can be specific to 1 or more lines of code. < c:catch > The basic format of the label is as follows:

<c:catch [var=" Variables that store exception information "]>
Others JSP Code, including various labels
</c:catch>

The usage is very similar to the try statement in Java code, and the code that may generate an exception is placed in the < c:catch > Between the start sign and the end sign. This way, when the code generates an exception, the JSP container handles the exception.

The following is an example of handling interface exceptions:


<c:catch>
  <c:out value="${user.address}"/>
</c:catch>

If you want to save the exception information for other code after the exception is generated, you can specify a variable using the var attribute.

The following code stores exception information in the exceptionInfo variable when the exception is generated:


<c:catch var="exceptionInfo">
  <c:out value="${user.address}"/>
</c:catch>

You can output exception information later if necessary:


<c:if test="${exceptionInfo != null}">
   An exception is generated at runtime, and the exception information is: ${exceptionInfo.message}
</c:if>

2 Specify exception handling files for pages

If you don't want to handle exceptions for every 1 piece of code, you can handle exceptions by page, and you can specify that when errors occur in the current page running process, a specific page will handle exceptions.

Using this exception handling method, we need to write a special exception handling file, and then set it in each file that needs exception handling.

Writing exception handling files

The isErrorPage attribute of the page directive is required in the exception handling file in the following format:

<%@ page isErrorPage="true"%>

If you set this up on the page, the page has a special feature to access the exception object exception. exception is an internal object of JSP. When an exception occurs in the running process of the page, an exception object exception will be thrown, which contains exception information.

The following is an exception handling file:

File name: errorPage. jsp


<%@ page contentType="text/html;charset=gb2312"%>
<%@ page isErrorPage="true"%>

The page generates an exception with the following exception information:

${exception.message}

Specify exception handling files for pages

If you want to make the page generate exceptions, by a special exception handling file to handle exceptions, you need to use errorPage of page instruction to specify a special exception handling interface in this page, and the format is as follows:

<%@ page errorPage=" Exception handling file "%>

Assuming you want to set the errorPage. jsp page you wrote earlier as the exception handling file for the current page, you can use the following code:
<%@ page errorPage="errorPage.jsp"%>

3 Configuration via web. xml

If you do not want to set up an exception handling file for each page, you can specify an exception handling file for the same type of exception. There are also 1 exception processing is no way to complete through page settings, for example, the user input a non-existent file in the website, at this time, the user should be told that the file does not exist, but this exception is no way to solve through page settings.

To specify one exception handling file for each type of exception, you can configure it through web. xml. Configuration in web. xml can be configured according to the error type, such as NullPointException, IOException, etc., or according to the error code.

Common exception codes are as follows:

403 Error: The file is forbidden to access;
404 Error: File not found;
500 Error: An unknown error occurred while the file was running.
Configured according to the exception type, you can use the following code:


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

Configured according to the exception code, you can use the following code:


<error-page>
  <error-code>401</error-code>
  <location>/401.jsp</location>
</error-page>

I hope this article is helpful to everyone's JSP programming.


Related articles: