Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to retrieve an error message in a JSP

Posted on 2003-11-19
5
Medium Priority
?
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This exercise is about for the following scenario: Dmgr and One node with 2 application server. Each application server contains it owns application. Application server name as follows server1 contains app1 server2 contains app1 Prereq…
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.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

609 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