Is there a way, in Tomcat, to redirect all invalid requests to a single
URL?

In web.xml add an entry like this:

<error-page>
                <exception-type>java.lang.Exception</exception-type>
                <location>/servlet/ErrorDisplay</location>
</error-page>

Write a servlet like this:

public class ErrorDisplay extends HttpServlet { // Exceptions & 404 page not
founds

        public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {

                String code = null, message=null, type=null;

                //Integer codeInt = 
req.getAttribute("javax.servlet.error.status_code");
                Throwable throwable =
(Throwable)req.getAttribute("javax.servlet.error.exception"); // servlet
origin
                if(throwable==null) {throwable =
(Throwable)req.getAttribute("javax.servlet.jsp.jspException");} // jsp
origin

                String uri = 
(String)req.getAttribute("javax.servlet.error.request_uri");

                StringBuffer report = new StringBuffer();
                if(throwable != null) {
                        report.append(stringStackTrace(throwable));

if(throwable.getClass().getName().equals("javax.servlet.ServletException"))
{
                                report.append("\nRoot Cause\n");

report.append(stringStackTrace(((ServletException)throwable).getRootCause())
);
                        }

                } else {
                        report.append("No 'javax.servlet.error.exception' parameter");
                }
                if(uri!=null) {
                        report.append("\nRequest URI: "+uri);
                } else {
                        report.append("No 'javax.servlet.error.request_uri' 
parameter");
                }

                try{
                        Util.sendMail("[EMAIL PROTECTED]","Exception report at
"+Log.now(),report.toString());
                        errorWriter.println(report.toString());
                        eventWriter.println("Exception report sent");
                }
                catch(IOException e) {UKRoutes.errorWriter.println("Exception report 
not
sent: "+e);}

                Util.dispatch("ServletError.jsp",req,res);
        }

        String stringStackTrace(Throwable throwable) {
                StringBuffer buffer = new StringBuffer();
                if(throwable != null) {
                        buffer.append(throwable.toString()+"\n");
                        StackTraceElement[] traceArray = throwable.getStackTrace();
                        for(int i=0;i<traceArray.length;i++) {
                                buffer.append(traceArray[i].toString()+"\n");
                        }
                }
                return buffer.toString();
        }
}

In that example I use a jsp but it doesn't do much so I'll leave it out.

Then away you go.

If you want this to handle other exceptions then you need to catch them and
rethrow them as a ServletException like this:

catch(SQLException e) {throw new ServletException(e.fillInStackTrace());}

Have Fun.

Now can someone answer my question please ??

--
Mike Whittaker
[EMAIL PROTECTED] <mailto:Mike@;HotPud.com>
~~~~~~~~~~~
Rock Climbing Database at http://www.hotpud.com

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to