Solved

How to retrieve an error message in a JSP

Posted on 2003-11-19
5
379 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Allow Firefox to use Java 6 plugin? 15 43
FileMaker 13 Port 80 Problem on server 2012 12 192
JMS Tibco queue/topic : how to get messages count of queue? 7 287
java JDK Download 7 76
Configure Web Service (server application) I. Configure security for Web Services methods First, we need to protect Session bean which implements the service: 1. Open EJB deployment descriptor (ejb-jar.xml) in the EJB project that contains you…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

19 Experts available now in Live!

Get 1:1 Help Now