If there is an index. jsp page, when an exception occurs on the server side, you must switch to errorpage. jsp and display the error information in errorpage. jsp. We need to add the command errorpage = "errorpage. jsp" and iserrorpage = "true" on these two pages respectively ".
Index. jsp
- <% @ Page errorpage = "errorpage. jsp" %>
- <%
- Throw new Exception ("exception from jsp ");
- %>
<% @ Page errorPage = "errorPage. jsp "%> </p> <% <br/> throw new Exception (" exception from jsp "); <br/> %>
ErrorPage. jsp
- <% @ Page isErrorPage = "true" %>
- <%
- Out. println (exception );
- // Display understandable information based on the Exception type and description
- %>
<% @ Page isErrorPage = "true" %> <br/> <% <br/> out. println (exception); </p> <p> // display understandable information based on the Exception type and description <br/> %>
In errorpage. jsp, The iserrorpage = "true" command must be added to ensure that the built-in variable exception exists. When direct access to errorpage. jsp is normal, exception is null.
Use URL http: // localhost: 8080/test/index. jsp in the browser to access the page.
Java. Lang. Exception: exception from index. jsp.
So what is the implementation mechanism? Analyze the running results in common Tomcat containers
View the index_jsp.java compiled by Tomcat and trace it back to (code snippet)
Some code in org. Apache. Jasper. runtime. pagecontextimpl. dohandlepageexception (throwable t) ()
- Request. setAttribute ("javax. servlet. jsp. jspException", t );
- Request. setAttribute ("javax. servlet. error. request_uri ",
- (HttpServletRequest) request). getRequestURI ());
- Forward (errorPageURL );
- Object newException = request. getAttribute ("javax. servlet. error. exception ");
- // T = null means the attribute was not set.
- If (newException! = Null) & (newException = t )){
- Request. removeAttribute ("javax. servlet. error. exception ");
- }
- Request. removeAttribute ("javax. servlet. error. exception ");
- Request. removeAttribute ("javax. servlet. jsp. jspException ");
Request. setAttribute ("javax. servlet. jsp. jspException ", t); <br/> request. setAttribute ("javax. servlet. error. request_uri ", <br/> (HttpServletRequest) request ). getRequestURI (); <br/> forward (errorPageURL); </p> <p> Object newException = request. getAttribute ("javax. servlet. error. exception "); </p> <p> // t = null means the attribute was not set. <br/> if (newException! = Null) & (newException = t) {<br/> request. removeAttribute ("javax. servlet. error. exception "); <br/>}</p> <p> request. removeAttribute ("javax. servlet. error. exception "); <br/> request. removeAttribute ("javax. servlet. jsp. jspException ");
You can also view the error page_jsp.java compiled by Tomcat (code snippet)
Throwable exception = org. Apache. Jasper. runtime. jspruntimelibrary. getthrowable (request); // in JSP
Part of the Code in org. Apache. Jasper. runtime. jspruntimelibrary. getthrowable (request)
- Throwable error = (Throwable) request. getAttribute (SERVLET_EXCEPTION );
- If (error = null ){
- Error = (Throwable) request. getAttribute (JSP_EXCEPTION );
- (Error! = Null ){
- Request. setAttribute (SERVLET_EXCEPTION, error );
Throwable error = (Throwable) request. getAttribute (SERVLET_EXCEPTION); <br/> if (error = null) {<br/> error = (Throwable) request. getAttribute (JSP_EXCEPTION); <br/> if (error! = Null) {<br/> request. setAttribute (SERVLET_EXCEPTION, error );
In the end, the exception is set to the request using servlet_exception.
The two constant strings are defined in JspRuntimeLibrary.
Private static final String SERVLET_EXCEPTION = "javax. servlet. error. exception ";
Private static final String JSP_EXCEPTION = "javax. servlet. jsp. jspException ";
If an exception occurs in your own code, such as Servlet, Filter, Struts's RequestProcessor, or Struts Action, you can also switch to errorPage. jsp, and can reuse the original errorPage. how can I apply the above analysis results to the code that displays error information in jsp?
The following uses Struts Action as an example to describe how to use it in other places. Why does Struts not use the exception handling model provided by Struts? It is also helpless. The maintenance of the old system is framed with Struts, and the rest can only be changed slowly.
Write in the execute () method of Struts Action
- Throwable t = new Exception ("exception from Struts Action .");
- Request. setAttribute ("javax. servlet. error. jspException", t );
- // Or configure a global error page in the struts-config.xml
- Return new ActionForward ("/errorPage. jsp ");
Throwable t = new Exception ("exception from Struts Action. "); <br/> request. setAttribute ("javax. servlet. error. jspException ", t); </p> <p> // or configure a global error page in the struts-config.xml <br/> return new ActionForward ("/errorPage. jsp ");
For more details, you can customize a Struts exception handling class to do this.
At this time, you also need to add the java code in the last line of errorPage. jsp:
Request. removeAttribute ("javax. servlet. error. exception ");
Remove the exception attribute in the request, or you cannot use errorPage. jsp to display errors.
HTTP Status 500-
Description The server encountered an internal error () that prevented it from fulfilling this request.
In this case, the browser accesses the URL http: // localhost: 8080/test. Do, and the page output
Java. Lang. Exception: exception from struts action.
Finally, I tried to publish this project to WebSphere Application Server (WAS) 5.1 and access http: // localhost: 9080/test. Do. The page output is null. No exception was found!
There is no good way to decompile the JSP code generated by was and find that the implementation class of pagecontext of was 5.1 is org. apache. jasper. runtime. pagecontextimpl, in was_home/lib/webcontainer. jar package. It is also set in this pagecontextimpl class
Request. setattribute ("javax. servlet. jsp. jspexception", t );
However, in was 5.1, errorpage. jsp directly uses the request to retrieve the exception:
Throwable = (throwable) httpservletrequest. getattribute ("javax. servlet. jsp. jspexception ");
Ignore the javax. servlet. Error. Exception attribute value in the request.
Therefore, you only need to note that in Tomcat, you can set
Request. setattribute ("javax. servlet. Error. Exception", t );
Or yes.
Request. setattribute ("javax. servlet. jsp. jspexception", t );
Of course, it is not required in errorpage. jsp under was 5.1.
Request. removeattribute ("javax. servlet. Error. Exception ");
Do you want to use
Request. removeattribute ("javax. servlet. jsp. jspexception ");
Remove the javax. servlet. jsp. jspexception attribute from the request? No.
The main difference between JSP pages under Tomcat and was is that their base classes are different. JSP pages under Tomcat are inherited from Org. apache. jasper. runtime. httpjspbase, And the JSP page under was 5.1 inherits from Com. IBM. WS. webcontainer. JSP. runtime. httpjspbase.
To be compatible with the two platforms, you should use
Request. setattribute ("javax. servlet. jsp. jspexception", t );
Set an exception and add a line in errorpage. jsp
Request. removeattribute ("javax. servlet. Error. Exception ");
OK.