简体   繁体   中英

Overwriting the errorPage property in JSP

I've defined <%@ page errorPage="/error.jsp" %> in the header that all JSP files include, to catch any unhandled exceptions and redirect to that error page instead of printing them. This works fine with one caveat - if error.jsp itself throws an exception, it will continuously redirect to itself in an infinite loop. I want to erase the errorPage value for just error.jsp so that it'll just print the exception as normal. I tried just redefining the errorPage property to be blank but I get the following error:

Page directive: illegal to have multiple occurrences of errorPage with different values (old: /error.jsp, new: )

Is there any way for me to overwrite that property? Or any other suggestions on how to prevent this issue?

It is indeed illegal to have multiple page declarations with the same attribute. Your choices are:

  1. Not include your header into your error page.
  2. Ensure that your error page doesn't throw any exception on its own. It should really be rather simple and straightforward - error page is no place for business logic. If you want to do something complicated there, consider redirecting to another page instead.

Why don't you just have a different include header for the error page which do not include it himself?!!

So, instead of having:

header.jsp
==========
a
b
c
errorPage=error.jsp

You could have:

commonHeader.jsp
===========
a
b
c

Without the errorPage directive

And modify the header to include the new one.

header.jsp
===========
include=commonHeader.jsp
errorPage=error.jsp

That way you don't need to change anything in the rest of your jsp's

You just need to change your errorPage from:

 include="header.jsp"

to

 include="commonHeader.jsp"

And the errorPage won't have an error page anymore ....

最后,我只是通过在页面周围用<c:catch>标记来避免该重定向,并打印一条准系统消息(例外情况)以确保它不会中断,来解决此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM