How to handle 404 in application context

I have 2 applications / projects deployed on the tomcat 7 web server. There contextual paths differ from "project1", "project2". When I click the URLs with this path, the corresponding application loads and works fine.

Valid URLs:

http: // localhost: 8080 / project1

http: // localhost: 8080 / project2

Now that I hit any invalid URL with the correct host name and context, for example / project3 , it gives me a 404 error message not found and shows the user a weird screen.

I want to show the right page with the right message to the end user. How can I do this at the web server level?

+4
source share
2 answers

You can adapt the Tomcat conf/web.xmlfile to show a different page than the default value for the 404 error - see here for an example.

Extract:

<error-page>  
       <error-code>404</error-code>  
       <location>/NotFound.jsp</location>  
</error-page>
+1
source

In theory you should be able to change the web.xml tomcat web applications by default, but according to this , it does not work for Tomcat 7.

Another option is to expand the standard ErrorReportValve. I very much "borrowed" from the existing code ErrorReportValve:

    public class Custom404Valve extends ErrorReportValve{

        public void invoke(Request request, Response response) throws IOException, ServletException {

           if(request.getStatusCode() != 404){
               super.invoke();
               return;
           }
           // Perform the request
           getNext().invoke(request, response);

           if (response.isCommitted()) {
               if (response.setErrorReported()) {
                   try {
                    response.flushBuffer();
                  } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                  }
               response.getCoyoteResponse().action(ActionCode.CLOSE_NOW, null);
                }
          return;
          }

         response.setSuspended(false);
         //this is where your code really matters
         //everything prior are just precautions I lifted
         //from the stock valve. 
         try {
           response.setContentType("text/html");
           response.setCharacterEncoding("utf-8");
           String theErrorPage = loadErrorPage();
           Writer writer = response.getReporter();
           writer.write(theErrorPage);
           response.finishResponse();
         } catch (Throwable tt) {
           ExceptionUtils.handleThrowable(tt);
         }

         if (request.isAsyncStarted()) {
             request.getAsyncContext().complete();
         }
      }

      protected String loadErrorPage() {
          BufferedReader reader = null;
          StringBuilder errorMessage = new StringBuilder();
              try{
                 File file = new File("YourErrorPage.html");
                 reader = new BufferedReader(new FileReader(file));

                    while (reader.ready()) {
                       errorMessage.append(reader.readLine());
                    }
              }catch (IOException e){
                  e.printStackTrace();
              }finally{
                 try{
                    reader.close();
                 }catch (IOException e) {
                    e.printStackTrace();
                 }
              } 
          return errorMessage.toString();     
        }
    }

Now you need to configure a custom valve:

    <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">

        <Valve className="com.foo.bar.Custom404Valve"/>
    </Host>
+1
source

All Articles