How to catch and throw errors in JSP

I asked a question similar lines yesterday. In this question, I was offered to have a global filter (which I already had).

So I have a JSP as shown below

....code...code ..tags...html...code Object [] res = iBatisDAO.getReport_pging(null,null,0,null); //call to DB ...more code... ...tags...end 

In the code above, I intentionally pass null because I want it to fail, and when it doesn't work, I want it to go to our central error page. I have the following in my web.xml

  <error-page> <exception-type>com.ibatis.common.jdbc.exception.NestedSQLException</exception-type> <location>/errorpages/Error.jsp</location> </error-page> <error-page> <exception-type>org.springframework.dao.DataAccessException</exception-type> <location>/errorpages/Error.jsp</location> </error-page> <error-page> <exception-type>javax.servlet.ServletException</exception-type> <location>/errorpages/Error.jsp</location> </error-page> <error-page> <exception-type>java.sql.SQLException</exception-type> <location>/errorpages/Error.jsp</location> </error-page> <error-page> <exception-type>org.springframework.jdbc.UncategorizedSQLException</exception-type> <location>/errorpages/Error.jsp</location> </error-page> 

"control" comes to the above JSP through the global filter that I have. it has chain.doFilter() wrapped in a try/catch . When exception occurs, it is redirected to Error.jsp.

If an error occurs ... it does not get on the centralized error page and does not get into the filter. I think the filter did not catch it, because when the filter "calls" jsp ... there is no error yet.

I know that a DB call is a BAD inside the JSP, but I am dealing with a lot of legacy code.

What can I do to get errors on the centralized error page in this scenario? In addition, the JSP does not import the imported error page. I would not want to import an error page for all JSPs. I want to have a more general solution.

+4
source share
2 answers

JSP exceptions cannot be handled nicely because it is too late to change the answer. JSP, as a viewing technology, is responsible for the entire answer on its own. It sends response headers and response content. When response headers are sent, then this is the point with no return . Even a filter does not help here.

Whenever an exception occurs halfway through the JSP, the response abruptly aborts, and the client encounters a blank or semi-processed page, and the exception may be the highest only to enter the server log. Perhaps, according to IllegalStateException: response already committed when trying to redirect / forward / display the error page while this is not possible, because the answer has already been made.

In short: do not write raw Java code in JSP files. Put them in Java classes such as (in) direct in Servlet. It is processed before , which is displayed by JSP. Thus, there is plenty of room for changing the destination of the response.

If you insist on using JSP for business logic (which I do not recommend), then the alternative is to place all business logic in the top JSP file before any template text (HTML, etc.) Should be sent in response. If you're lucky, servletcontainer will be able to change the response to the error page whenever an exception is thrown.

+3
source

You have taken the wrong approach. You should not have processing logic code (business logic) in your JSP files. JSP is a viewing technology. Use servlets or the action / component framework (Struts2 / Spring MVC / JSF / etc.) to process business logic.

Regarding the filter approach, this is a good solution, but the filter should map to /* (using <filter-mapping> ):

 <filter> <filter-name>exceptionFilter</filter-name> <filter-class>com.yourpackage.YourFilter</filter-class> </filter> <filter-mapping> <filter-name>exceptionFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 
0
source

Source: https://habr.com/ru/post/1303671/


All Articles