0
votes

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).

2
JSPs are basically glorified servlets. If you look in the work folder under Tomcat and drill down, you will find the generated source code for the JSP page in servlet format. In the servlet, you just need try/catch blocks to handle catching the exceptions. - mikemil

2 Answers

1
votes

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>
0
votes

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.

enter image description here