Implementation Mechanism and Application of abnormal turning of JSP errorPage commands to error pages

Source: Internet
Author: User
Tags websphere application server

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

  1. <% @ Page errorpage = "errorpage. jsp" %>
  2. <%
  3. Throw new Exception ("exception from jsp ");
  4. %>

<% @ Page errorPage = "errorPage. jsp "%> </p> <% <br/> throw new Exception (" exception from jsp "); <br/> %>

ErrorPage. jsp

  1. <% @ Page isErrorPage = "true" %>
  2. <%
  3. Out. println (exception );
  4. // Display understandable information based on the Exception type and description
  5. %>

<% @ 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) ()

  1. Request. setAttribute ("javax. servlet. jsp. jspException", t );
  2. Request. setAttribute ("javax. servlet. error. request_uri ",
  3. (HttpServletRequest) request). getRequestURI ());
  4. Forward (errorPageURL );
  5. Object newException = request. getAttribute ("javax. servlet. error. exception ");
  6. // T = null means the attribute was not set.
  7. If (newException! = Null) & (newException = t )){
  8. Request. removeAttribute ("javax. servlet. error. exception ");
  9. }
  10. Request. removeAttribute ("javax. servlet. error. exception ");
  11. 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)

  1. Throwable error = (Throwable) request. getAttribute (SERVLET_EXCEPTION );
  2. If (error = null ){
  3. Error = (Throwable) request. getAttribute (JSP_EXCEPTION );
  4. (Error! = Null ){
  5. 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

  1. Throwable t = new Exception ("exception from Struts Action .");
  2. Request. setAttribute ("javax. servlet. error. jspException", t );
  3. // Or configure a global error page in the struts-config.xml
  4. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.