?
Solved

How to retrieve an error message in a JSP

Posted on 2003-11-19
5
Medium Priority
?
394 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 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