-- Learn not just technology, but dreams!
Home HTML JAVASCRIPT CSS VUE REACT PYTHON3 JAVA C C++ C# AI GO SQL LINUX VS CODE BOOTSTRAP GIT Local Bookmarks
JSP Tutorial
JSP Tutorial
JSP Introduction
JSP Development Environment Setup
Eclipse JSP/Servlet
JSP Structure
JSP Life Cycle
JSP Syntax
JSP Directives
JSP Action Elements
JSP Implicit Objects
JSP Client Request
JSP Server Response
JSP HTTP Status Codes
JSP Form Processing
JSP Filters
JSP Cookie Handling
JSP Session
JSP File Upload
JSP Date Processing
JSP Page Redirection
JSP Hit Counter
JSP Auto Refresh
JSP Sending Email
JSP Advanced Tutorial
JSP Standard Tag Library (JSTL)
JSP Database Connection
JSP XML Data Processing
JSP JavaBean
JSP Custom Tags
JSP Expression Language
JSP Exception Handling
JSP Debugging
JSP Internationalization
JSP Expression Language
JSP Debugging
Deep Dive
Development Tools
Software
Programming Languages
Scripting Languages
Computer Science
Web Service
Programming
Web Services
Web Design & Development
Scripts
JSP Exception Handling
When writing JSP programs, programmers may miss some bugs, which can appear anywhere in the program. There are generally several types of exceptions in JSP code:
Checked Exceptions: A checked exception is a typical user error or an error that a programmer cannot foresee. For example, if a file is about to be opened but cannot be found, an exception is thrown. These exceptions cannot be simply ignored at compile time.
Runtime Exceptions: A runtime exception may have been avoided by the programmer, and this type of exception will be ignored at compile time.
Errors: Errors are not exceptions, but the problem is that they are beyond the control of the user or programmer. Errors are usually ignored in code, and there is little you can do about them. For example, a stack overflow error. These errors are ignored at compile time.
This section will provide a few simple and elegant ways to handle runtime exceptions and errors.
Using the Exception Object
The exception object is an instance of a subclass of Throwable and is only available on error pages. The following table lists some important methods in the Throwable class:
Number Method & Description
1 public String getMessage()
Returns the message of the exception. This message is initialized in the Throwable constructor.
2 public Throwable getCause()
Returns the cause of the exception, as a Throwable object.
3 public String toString()
Returns the class name.
4 public void printStackTrace()
Outputs the exception stack trace to System.err.
5 public StackTraceElement [] getStackTrace()
Returns the exception stack trace as an array of stack trace elements.
6 public Throwable fillInStackTrace()
Fills the Throwable object with the current stack trace.
JSP provides the option to specify an error page for each JSP page. Whenever an exception is thrown from the page, the JSP container will automatically invoke the error page.
The following example specifies an error page for main.jsp. Use the directive to specify an error page.
Error Handling Example
Now, write the ShowError.jsp file as follows:
Show Error Page
Opps...
Sorry, an error occurred.
Here is the exception stack trace:
Note that the ShowError.jsp file uses the directive, which tells the JSP compiler to generate an exception instance variable.
Now try to access the main.jsp page, it will produce the following result:
java.lang.RuntimeException: Error condition!!!
......
Opps...
Sorry, an error occurred.
Here is the exception stack trace:
...
Using JSTL Tags in the Error Page
You can use JSTL tags to write the error page ShowError.jsp. The code in this example is logically almost the same as the previous example, but the code in this example has better structure and can provide more information:
Show Error Page
Opps...
| Error: |
${pageContext.exception} |
| URI: |
${pageContext.errorData.requestURI} |
| Status code: |
${pageContext.errorData.statusCode} |
| Stack trace: |
${trace}
|
The result is as follows:
...
Using try…catch Blocks
If you want to handle exceptions within a single page and handle different exceptions differently, then you need to use try…catch blocks.
The following example shows how to use try…catch blocks. Put this code in main.jsp:
Try...Catch Example
Try to access main.jsp, it will produce the following result:
An exception occurred: / by zero
JSP Expression Language
JSP Debugging
ByteDance Coding Plan
Supports mainstream large models like Doubao, GLM, DeepSeek, Kimi, MiniMax, officially supplied, stable and reliable.
Configuration Guide
¥9.9
/ month
Subscribe Now
iFlytek Star Coding Plan
Includes free model call quota, DeepSeek, GLM, Kimi, MiniMax, one-stop experience and deployment platform.
Configuration Guide
¥3.9
/ month
Subscribe Now
Click here to share notes
Category Navigation
Python / Data Science
AI / Intelligent Development
Front-end Development
Back-end Development
Database
Mobile Development
DevOps / Engineering
Programming Languages
Computer Fundamentals
XML / Web Service
.NET
Website Construction
Advertisement
JSP Tutorial
JSP Tutorial
JSP Introduction
JSP Development Environment Setup
Eclipse JSP/Servlet
JSP Structure
JSP Life Cycle
JSP Syntax
JSP Directives
JSP Action Elements
JSP Implicit Objects
JSP Client Request
JSP Server Response
JSP HTTP Status Codes
JSP Form Processing
JSP Filters
JSP Cookie Handling
JSP Session
JSP File Upload
JSP Date Processing
JSP Page Redirection
JSP Hit Counter
JSP Auto Refresh
JSP Sending Email
JSP Advanced Tutorial
JSP Standard Tag Library (JSTL)
JSP Database Connection
JSP XML Data Processing
JSP JavaBean
JSP Custom Tags
JSP Expression Language
JSP Exception Handling
JSP Debugging
JSP Internationalization
Online Examples
·HTML Examples
·CSS Examples
·JavaScript Examples
·Ajax Examples
·jQuery Examples
·XML Examples
·Java Examples
Character Sets & Tools
· HTML Character Set Settings
· HTML ASCII Character Set
· JS Obfuscation/Encryption
· PNG/JPEG Image Compression
· HTML Color Picker
· JSON Formatting Tool
· Random Number Generator
Latest Updates
· AI Beginner Tutorial
· Large Model Multimodal (M...
· Codex Advanced Configuration
· AI Agent Terminology
· ZCode Beginner Tutorial
· Loop Engineerin...
· Claude Code Us...
Site Information
· Feedback
· Disclaimer
· About Us
· Article Archive
Follow WeChat
My Collection
Mark Article
Browsing History
Clear All
No records yet