404 error page page not shown in some conditions - java

When my path is
hostname/some_file.jsp
ErrorPage404.jsp is displayed
But when my path is any one of following
hostname/some_file.html
hostname/some_file_path
hostname/unknown_path/some_file_path
hostname/unknown_path/some_file.html
ErrorPage404.jsp is not displayed
I have mapped ErrorPage404.jsp in web.xml in following manner
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/Entry/ErrorPage404.jsp</location>
</error-page>
Thanks in advance.

Related

Java EE - more generic error code mapping

I'm currently mapping error codes in web.xml and so far it looks like this:
<error-page>
<error-code>404</error-code>
<location>/errorHandler?code=404</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errorHandler?code=500</location>
</error-page>
Do I have to map every single error code manually or is there more automatic way? I'd like to have something like this:
<error-page>
<error-code>*</error-code>
<location>/errorHandler?code=*</location>
</error-page>
That's possible, according to the documentation of oracle. It depends on whether you are using servlet 2.5 (not supported) or 3.0 (supported).
I would recommend you to use plain html for the error codes.
The reason behind this, is these pages will be shown when something is going very wrong. Full list of status codes
Generic solution example: moreinfo with onehippo
From security perspective it is not recommended to give information what went wrong in the application: https://blog.whitehatsec.com/error-handling-in-java-web-xml/

Handling runtime exceptions in UI

Suppose my web application throws a runtime exception due to some reason.
When that happens, I want to capture it and show a proper message in the UI rather than a 501/505 error.
What is the best way to do that ?
something lik ethis in your web.xml
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/app/error/commonErrorPage</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/app/error/404ErrorPage</location>
</error-page>

Replace Error Handling from JSP to Servlet

I have this code in my JSP script
<%# page errorPage ="error.jsp"%>
<%!
...My JSP Code... //No matter for this question
%>
My error.jsp script is:
<%# page isErrorPage = "true"%>
<% if (exception != null) { %>
The cause of the exception error has been:
<% exception.printStackTrace(new java.io.PrintWriter(out)); %>
<% } %>
like:
http://www.tutorialspoint.com/jsp/jsp_exception_handling.htm
But I want to replace all before code to servlet...
As you can see...
I need to translate <%# page errorPage ="error.jsp"%> to Servlet code...
and error.jsp entirely...for example How translate this:<%# page isErrorPage = "true"%>?
Sorry, but I don't know how to do it...
PD:
I was looking for my question, but sites tell that I need to change web.xml file, I don't want to do it, I think that (My JSP script works fine, and the converted by Tomcat servlet works fine too without change that file).
hey buddy #Luigi Giuseppe. I thing this one will be going to satisfying for you doubt.
Two ways to define Error page in Java web application written using Servlet and JSP like your question related.
1. First way is page wise error page which is defined on each jsp page and if there is any unhanded exception thrown from that page, corresponding error page will be displayed.
2. Second approach is an application wide general or default error page which is shown if any Exception is thrown from any Servlet or JSP and there is no page specific error page defined.
HTTP standard error codes:
1. Information This one is a new return code which is not 100%
supported and normally and only provides information to the client
about the request.
2. Success response, the request has been correctly executed ->
expected answer from server (http code 200).
3. Redirection response. The resource has moved and is not any more at
this URL.
4. Error on client side. Probably most known of all is error 404 : Not
Found.
Defining error.jsp page like:
//error.jsp
<%# page isErrorPage="true"%>
//login.jsp
<%# page errorPage="error.jsp"%>
And, Error page in Java Web Application JSP Servlet page is like:
Default Error page based on Exception:
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.htm</location>
</error-page>
Default Error page based on HTTP Error code:
<error-page>
<error-code>500</error-code>
<location>/internal-server-error.htm</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/page-not-found-error.htm</location>
</error-page>
Create your custom error pages:
1. 400.html -> telling the user did something wrong
2. 500.html -> telling the server did something wrong
400.html:
<error-page>
<error-code>400</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>401</error-code>
<location>/404.html</location>
</error-page>
500.html:
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
<error-page>
<error-code>501</error-code>
<location>/500.html</location>
</error-page>
hey buddy #Luigi Giuseppe. Yes, #mikemil's telling right buddy, You should just use the try/catch and otherwise use the method to display the exception error like as following code like this:
package com.jmail.servlet.exception;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/MyExceptionServlet")
public class MyExceptionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
throw new ServletException("GET method is not supported.");
}
}
Now, you just getting error page like this:
GET method is not supported.

how to get custom exception message in ajaxError of jquery

I know we can get exception using xhr.responseText, but in my APP, the responseTextdisplay as exception description amended by Tomcat. please refer to below image, is there anyone can tell me how can I control the content of responseText or how to handle custom message(may be in a better way).
I need a title and content in the response as I can display them in dialog of Jquery UI. BTW, I throw a general exception just like throw new Exception("some error happens");
You can define custom error message in your web.xml like this -
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/Error.jsp</location>
</error-page>
You can have any text in your error.jsp page, that text will be sent to client on respective error
more info

How to get original page url/uri after request dispatcher forward

I have a Error404Servlet which is configured as error-page for 404 in web.xml:
<servlet>
<servlet-name>Error404</servlet-name>
<servlet-class>com.foo.bar.Error404Servlet</servlet-class>
</servlet>
<error-page>
<error-code>404</error-code>
<location>/error404</location>
</error-page>
In this servlet i have to log the original url that caused 404, but request.getRequestURI() always returns "/error404"
How can i get the original url? The unly ugly method i know is to create filter that puts the original url to request attribute.
From the request you can retrieve attributes set by the container in the event of an error:
request.getAttribute("javax.servlet.error.request_uri");
other attributes that can provide usefull information;
javax.servlet.error.status_code
javax.servlet.error.exception_type
javax.servlet.error.message
javax.servlet.error.exception
See also this servlet 2.3 features article.

Categories