'How to handle HTTP Errors in Struts2
I am working on a Struts2 project which encounter HTTP errors like 404: page not found or 500: Internal server Error. I just want to handle all HTTP errors in Struts 2 programmatically without configuration.
Perhaps I would detect HTTP error code in filter or interceptor and then forward request to another page which especially design to display proper messages. How can I design such a program?
Solution 1:[1]
Take a look at the Struts2 Exception Handling Wiki.
You can define global results for specific Exceptions.
<global-results>
<result name="securityerror">/securityerror.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
Solution 2:[2]
you can handle the such HTTP errors in the web.xml , not the struts framework like the following :
<web-app>
...
<error-page>
<error-code>404</error-code>
<location>/error404page.jsp</location>
</error-page>
</web-app>
When system hit the 404 error, it will forward to your custom 404 error page “/error404pag.jsp“.
Hope that Helps .
Solution 3:[3]
These are not errors, just response codes. I don't know how you manage the connection, but the plain http connection works this way:
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int code = httpConnection.getResponseCode();
In this class are missing some codes.
Since Java 6 javax.servlet.http.HttpServletResponse has the response codes. Also in Response.Status you have the response codes in a very readable way.
In your application - up tou the response code you can redirect where you want. How will you get the response code in an interceptor - I don't know, I'm not on my work computer to test some code. But there is a class that could help you - org.apache.struts2.dispatcher.HttpHeaderResult
Hope this helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Johannes |
| Solution 2 | |
| Solution 3 | Thrash Bean |
