0

我有很多生产服务器在每个运行 RMI 应用程序,还有更多 4 个 Java webapps,例如:

Server A:
RMI app by JNLP file;
webapp_1 (connected by RMI with local RMI app);
webapp_2 (connected by RMI with local RMI app);
webapp_3 (connected by RMI with local RMI app);
webapp_4 (connected by RMI with local RMI app);

Server B:
...the same..OK

所有用户在默认上下文中直接在 8080 端口(直接到 Jetty)上访问此服务器,例如“主区域”,它可以通过一些 html 链接访问所有应用程序(RMI 应用程序、webapp_1、webapp_2 等)。

当某些用户访问“/”页面时,例如:

www.foo.com:8080/

main-area/
webapp_1/
webapp_2/
webapp_3/
...

Jetty 返回一个包含所有应用程序的列表(就像 Apache 的目录列表)。

有没有办法阻止它,或重定向到“主要区域”上下文?

4

3 回答 3

4

不匹配的 webapp 上下文列表"/"作为责任的一部分呈现给您org.eclipse.jetty.server.handler.DefaultHandler

DefaultHandler默认情况下启用,以保持符合 Servlet 规范。

禁用默认处理程序:

如果您只想要一个简单的 404,而 DefaultHandler 没有提供任何信息,那么只需在${jetty.home}/etc/jetty.xml

<!-- =========================================================== -->
<!-- Set handler Collection Structure                            --> 
<!-- =========================================================== -->
<Set name="handler">
  <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
    <Set name="handlers">
     <Array type="org.eclipse.jetty.server.Handler">
       <Item>
         <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
       </Item>
       <!-- Disable the DefaultHandler to avoid listing of non-matching contexts
       <Item>
         <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
       </Item>
        -->
     </Array>
    </Set>
  </New>
</Set>

"/"为(ROOT)上下文呈现静态内容:

如果您希望根上下文"/"(ROOT) 呈现其他内容,则创建一个${jetty.home}/webapps/ROOT目录并将 index.html 文件放入其中。

[jetty-distribution-7.6.13.v20130916]$ cat webapps/ROOT/index.html 
<h1>This is ROOT</h1>

这将部署一个静态内容 web 应用程序,您可以在其中放置您想要的任何内容、图像、css 等。

自动重定向"/"(ROOT)到另一个路径:

注意:这不会与上述${jetty.home}/webapps/ROOT选项、他的这个选项或那个选项同时工作,但不能同时工作。

如果您希望 Jetty"/"自动重定向到另一个 URL,请使用重写处理程序。

确保启用了重写选项,并包含一组重写规则 xml

[jetty-distribution-7.6.13.v20130916]$ grep rewrite start.ini 
OPTIONS=Server,jsp,jmx,resources,websocket,ext,rewrite
etc/jetty-rewrite.xml

接下来,您需要定义重写规则...

to的内容${jetty.home}/etc/jetty-rewrite.xml将访问从"/"to 重定向到"/test/"

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
                   "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <Get id="oldhandler" name="handler"/>

    <Set name="handler">
     <New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
      <Set name="handler"><Ref id="oldhandler"/></Set>
      <Set name="rewriteRequestURI">true</Set>
      <Set name="rewritePathInfo">false</Set>
      <Set name="originalPathAttribute">requestedPath</Set>

      <!-- redirect from the welcome page to a specific page -->
      <Call name="addRule">
        <Arg>
          <New class="org.eclipse.jetty.rewrite.handler.RedirectRegexRule">
            <Set name="regex">^/$</Set>
            <Set name="replacement">/test/</Set>
          </New>
        </Arg>
      </Call>
     </New>
    </Set>
</Configure>
于 2013-10-16T18:39:28.297 回答
3

为该位置创建一个 index.html 文件。

这将被提供,因此不需要生成列表。

然后,如果浏览器不尊重重定向,您可以在其中放置一个简单的重定向以及一个普通链接。

于 2013-10-16T14:39:49.853 回答
0

感谢Thorbjørn Ravn Andersen的解决方案:

我创建了一个带有单个 index.jsp 的基本动态 Web 应用程序,其中包含 JSP HTML/JS 重定向:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="1;url=bar/index.jsp">
        <script type="text/javascript">
            window.location.href = "bar/index.jsp"
        </script>
        <title>Page Redirection</title>
    </head>
    <body>
        If you are not redirected automatically, follow the <a href='bar/index.jsp'>main area</a>
    </body>
</html>

我部署为“ROOT.war”,并在“...jetty/contexts/”中配置了一个“root.xml”:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/</Set>
  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/ROOT.war</Set>
</Configure>
于 2013-10-16T15:10:05.877 回答