Solved

How to retrieve an error message in a JSP

Posted on 2003-11-19
5
382 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
ID: 9785451
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
ID: 9785522
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
ID: 9795322
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Programatically Import a WAR file to my existing Tomcat 5 87
spring AOP 6 85
some methods private some are not 8 210
Using Tomcat as raspberry pi java app server 13 164
-Xmx and -Xms are the two JVM options often used to tune JVM heap size.   Here are some common mistakes made when using them:   Assume BigApp is a java class file for the below examples. 1.         Missing m, M, g or G at the end …
This article is about some of the basic and important steps to be used to improve the performance in web-sphere commerce application development. 1) Always leverage the Dyna-caching facility provided by the product 2) Remove the unwanted code …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

808 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