2015-02-23 74 views
4

我有2个应用程序/项目部署在tomcat 7 web服务器上。上下文路径与“project1”,“project2”不同。当我用这些上下文路径打网址时,相应的应用程序加载并正常工作。如何在应用程序上下文级别处理404

有效网址是:

http://localhost:8080/project1

http://localhost:8080/project2

现在,当我打任何错误的网址与正确的主机名和像/项目3不正确的上下文路径,它给我的错误信息 404找不到,并向用户显示一个奇怪的屏幕。

我想显示一个适当的消息给最终用户的页面。我怎么能在Web服务器级别做到这一点?

+0

@Magnilex感谢编辑我的问题。你能帮我解决吗? – 2015-02-23 09:42:28

+0

对不起,我没有时间。顺便说一下,你真的使用JEE堆栈吗?为此,通常使用像JBoss这样的应用服务器。如果没有,那么你已经用错误的标签标记了问题。 – Magnilex 2015-02-23 09:46:16

+0

@Magnilex我正在使用Web服务器(Apache Tomcat 7.0),而不是任何应用程序服务器。 – 2015-02-23 09:57:46

回答

1

您可以修改Tomcat的conf/web.xml文件以显示除404错误的默认值以外的其他页面 - 有关示例,请参见here

提取物:

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

感谢您的宝贵答案。但使用这种方法,我可以处理404项目资源。但是我正在寻找服务器级别404。请再次阅读我的问题。 – 2015-02-23 17:19:19

+0

@SunilSharma:我再次读过你的问题,但我会说我的awer仍然有效 - 你的tomcat试图提供错误的URL并且没有上下文,所以它自己产生了一个丑陋的404网页。所以为了单独处理这个问题,你必须修改tomcat的配置(我提到的'web.xml')并将它指向一个更好的错误页面。这与您部署的项目无关,它在服务器级别上。 – 2015-02-24 06:45:51

+0

你是对的它工作正常!现在是啤酒的时间了! – 2015-03-24 09:03:55

1

从理论上讲,你应该能够修改默认的Tomcat Web应用程序的web.xml中,但根据this,不为Tomcat 7

工作

您的其他选择是扩展标准ErrorReportValve。我已经从现有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();  
     } 
    } 

你现在需要做的是配置自定义阀:

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

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

你在Tomcat 7中试过了吗?我没有,但根据BalusC对你的链接的回答中存在的错误已修复。 – 2015-02-24 06:53:56

+0

谢谢@Geziefer;我没有收到关于链接帖子的最新评论(它指出它已经在新版本的TC7中修复),并且我从JIRA不清楚它是否已修复。我们会让OP确认。阀门非常简单。我只添加了股票'ErrorReportValve'中的部分内容,因为我需要一些预防措施。所有OP确实需要做的是最后几行的内容 – kolossus 2015-02-24 16:05:10

相关问题