Solved

How to retrieve an error message in a JSP

Posted on 2003-11-19
5
376 Views
Last Modified: 2013-12-10
Hi

I'm developping a webapp with weblogic 45.

I've got a little issue:
How can i get the error message which comes from an error redirect (404,
500,...)

For example, with Tomcat, i do:
response.sendError(response.SC_NOT_FOUND, "file not found");

I want to get the 2nd parameter in my JSP page with this :
request.getAttribute("javax.servlet.error.message")

On Weblogic, it doesn't work.

Does someone has an idea?

Thanks a lot.

Laurent.
0
Comment
Question by:nonolaurent
  • 2
5 Comments
 
LVL 4

Expert Comment

by:boxy73
Comment Utility
I think you could include a variable in your URL for each case, for example

yourerrorpage.jsp?ERROR=NOT_FOUND

And you could recover it using request.getParameter("ERROR") in your JSP, so you can place then the code you need for each error.

0
 

Author Comment

by:nonolaurent
Comment Utility
i don't think you understood what i want to do...
With web server, you can specify in the web.xml your own error pages (status code and their associated jsp page)
Each page has its own status code. And Tomcat enables to embed a message.
When you do response.sendError, you cannot store anything in the request... That would havee been easy...
0
 
LVL 4

Accepted Solution

by:
boxy73 earned 50 total points
Comment Utility
Yes I did, and I will give you three chances now:

1. Define in the location of the errors the parameter itself for the page. i.e. in your web.xml, define

  <error-page>
    <error-code>404
    </error-code>
    <location>/jspError.jsp?ERROR=404</location>
  </error-page>
  <error-page>
    <error-code>500
    </error-code>
    <location>/jspError.jsp?ERROR=500</location>
  </error-page>

And in that jsp, you just change its behaviour using request.getParameter("ERROR") and doing what you wanna do for each case. You can even use

     <exception-type>
     classes.myOwnExceptionClass
     </exception-type>

for your own defined exception classes.

2. just place the code on your web.xml and define your JSP as:

<%@page isErrorPage="true" %>
With this in JSP beginning, you can place in your page <%= exception.getClass().getName() %> or whatever to recover what exception was launched and make what you need.

3. The same for web.xml but using a servlet redirection instead of JSP. Define a servlet like this to recover errors:

   public class ErrorServlet extends HttpServlet {
        ...
        protected static String[] vars = {
                "javax.servlet.error.status_code",
                "javax.servlet.error.exception_type",
                "javax.servlet.error.message",
                "javax.servlet.error.exception",
                "javax.servlet.error.request_uri"
            };

        // Handle post request
        public void doPost(HttpServletRequest req,
            HttpServletResponse res)
            throws IOException, ServletException {
            ...
            // Print vars
            for (int i = 0; i < vars.length; i++) {
                out.println(
                    "<TR><TD>" + vars[i] + "</TD><TD>" +
                    req.getAttribute(vars[i]) +
                    "</TD></TR>");
            }
            ...
        }
    }


Hope this helps




0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Most of the developers using Tomcat find it easy to configure the datasource in Server.xml and use the JNDI name in the code to get the connection.  So the default connection pool using DBCP (or any other framework) is made available and the life go…
Upgrading Tomcat – There are a couple of methods to upgrade Tomcat is to use The Apache Installer is to download and unzip and run the services.bat remove|install Tomcat6 Because of the App that we are working with, we can only use Tomcat 6.…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now