默认配置 tomcat 仅提供一个网站

Configure tomcat to serve only one website as the default

我被迫将我的网站移动到静态 IP。这要求我更改 tomcat (Tomee-Plume) 上的配置。我不知道该怎么做是将端口 80 直接映射到网站。因此,例如,旧设置具有以下隐形转发设置。

旧的隐身设置。

my.domain -> http://999.999.999.999/Websites/HomeServlet

现在我需要将“/Websites/HomeServlet”部分的分辨率推送到 tomcat (Tomee-Plume) 配置中,因为我只使用互联网 "A" 记录来映射域名到IP。无法将“/Websites/HomeServlet”部分放入 Internet "A" 记录中。

我猜这个设置很常见,但我不知道通常叫什么来进行互联网搜索。


以下内容是应 Shankar P S 的要求添加的内容。


Eclipse 中的文件布局


Header.java

package software..website;

public class Header {

    public Header() {
    }

    static StringBuffer getStandardHTMLHeader(){
        StringBuffer sb = new StringBuffer();
        sb.append("<?xml version='1.0' encoding='UTF-8'?> ");
        sb.append(
                "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>");
        sb.append("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>");
        sb.append("<head>");
        sb.append("<meta http-equiv='content-type' content='text/html; charset=UTF-8' />");
        sb.append("<META NAME=\"ROBOTS\" CONTENT=\"NOINDEX, NOFOLLOW\" />");
        sb.append("<META NAME=\"description\" content=\" is a software application that does mathematical models of the space elevator.\" />");

        sb.append("<!-- **** layout style-sheet **** -->");
        sb.append("<link rel='stylesheet' type='text/css' href='./style/style.css' />");

        sb.append("<!-- **** color scheme style-sheet **** -->");
        sb.append("<link rel='stylesheet' type='text/css' href='./style/blue.css' />");
        sb.append("</head>");
        return sb;

}

}

HomeServlet.java

package software..website;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class home
 */
@WebServlet("/HomeServlet")
public class HomeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        // response.getWriter().append("Served at:
        // ").append(request.getContextPath());
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {

            out.println(Header.getStandardHTMLHeader() );

            out.println("<body>");

            out.println(Menu.getStandardHTMLMenu());


            blah, blah, blah......
            blah, blah, blah......
            blah, blah, blah......


            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name></display-name>
  <description>
    The web site for  software
    </description>
<!-- -->
  <servlet>
    <servlet-name>HomeServlet</servlet-name>
    <servlet-class>software..website.HomeServlet</servlet-class>
     <init-param>
        <param-name>isVirtualWebappRelative</param-name>
        <param-value>1</param-value>
     </init-param>    
  </servlet>
<servlet-mapping>
  <servlet-name>HomeServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping> 
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Protect Donation page</web-resource-name>
        <url-pattern>/DonateServlet</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>        
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>  
</web-app>

**web.xml v2 **

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<!-- ========================================================== -->
<!-- General Info -->
<!-- ========================================================== -->
<display-name>Abc</display-name>
<description>
   The web site for Abc software
</description>

<!-- ========================================================== -->
<!-- Home Servlet -->
<!-- ========================================================== -->
<servlet>
   <servlet-name>HomeServlet</servlet-name>
   <servlet-class>software.abc.website.HomeServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
 </servlet>

<!-- Map the HomeServlet name to the URI /HomeServlet (main page for site) --> 
 <servlet-mapping>
   <servlet-name>HomeServlet</servlet-name>
   <url-pattern>/AbcLandingPage</url-pattern>
 </servlet-mapping> 

 <security-constraint>
     <web-resource-collection>
         <web-resource-name>Protect Donation page</web-resource-name>
         <url-pattern>/DonateServlet</url-pattern>
         <http-method>GET</http-method>
         <http-method>POST</http-method>        
     </web-resource-collection>
     <user-data-constraint>
         <transport-guarantee>CONFIDENTIAL</transport-guarantee>
     </user-data-constraint>
 </security-constraint>  


<!-- ========================================================== -->
<!-- Welcome Files -->
<!-- ========================================================== -->

<!-- The main page for the site will be the HomeServlet servlet (http://website/AbcLandingPage) -->
<!-- No mapping is defined for other folders (http://website/someFolder/AbcLandingPage), -->
<!-- so one of the other files will be displayed (index.html, index.htm, index.jsp) -->
<welcome-file-list>
    <welcome-file>AbcLandingPage</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>


仍然得到 tomcat 欢迎页面。

enter image description here


这适用于我的 Windows 7 工作站和 Eclipse Mars 2 (4.5.2)

  <Context docBase="Websites" path="" reloadable="true" source="org.eclipse.jst.jee.server:Websites"/></Host>

但这在我的 Fedora 23 服务器上不起作用。

    <Context path="/Websites" docBase="Websites/HomeServlet" debug="0" reloadable="true"></Context>

Window 7(有效)

"C:\apache-tomee-plume\wtpwebapps\Websites\WEB-INF"
"C:\apache-tomee-plume\wtpwebapps\Websites\META-INF"
"C:\apache-tomee-plume\wtpwebapps\Websites\style"

Fedora 23(抛出异常)

/opt/tomee/webapps/docs/{the directory has files in it}
/opt/tomee/webapps/host-manager/{the directory has files in it}
/opt/tomee/webapps/manager/{the directory has files in it}
/opt/tomee/webapps/ROOT/{the directory has files in it}
/opt/tomee/webapps/Websites/{the directory has one directory, no files in it}
/opt/tomee/webapps/Websites/HomeServlet.unpacked/{the directory is empty}
/opt/tomee/webapps/Websites.war

你需要做两件事。将 Tomcat 的默认端口从 8080 更改为 80,并允许对 Tomcat 的请求命中您的项目。试试这个。

在文件 {TOMCAT_CATALINA}/conf/server.xml 中,查找协议 HTTP/1.1 的连接器标记。这将映射到8080。将其更改为80,如下所示。

<Connector port="80" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
    maxThreads="750"
    acceptCount="750"
    redirectPort="8443" />

然后,在同一个文件中,将其添加到 Host 标签下。 (我假设您的 WAR 文件名为 Websites。这在本页的方法 2.1 中有解释 - http://wiki.apache.org/tomcat/HowTo#How_do_I_make_my_web_application_be_the_Tomcat_default_application.3F

<Context path="" docBase="Websites" debug="0" reloadable="true"></Context>

现在,对 http://my.domain 的调用将到达您的 WAR 文件。

接下来,将 servlet 映射到项目 web.xml 中的默认路径。

<servlet-mapping>
  <servlet-name>HomeServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping> 

现在,对 http://my.domain 的调用等同于 http://YOUR_IP/Websites/HomeServlet

最终答案...需要对 web.xml 文件和 server.xml 文件.

需要对 web.xml 文件进行更改。

下面的更改列表是针对 WEB-INF/web.xml 文件而不是服务器级别 conf/web。 xml 文件。先决条件是 XML 节中。 XML 元素不是手头任务所必需的。添加它是为了让第一个用户在 tomee 重启后不会遇到性能不佳的情况。

<servlet>
   <servlet-name>HomeServlet</servlet-name>
   <servlet-class>software.abc.website.HomeServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
 </servlet>

一个 XML节也需要制作。此 节中的 必须与 节中的 一致节。 不需要匹配 标签必须与 节中的 标签匹配。

<!-- Map the HomeServlet name to the URI /HomeServlet (main page for site) --> 
<servlet-mapping>
   <servlet-name>HomeServlet</servlet-name>
   <url-pattern>/AbcLandingPage</url-pattern>
</servlet-mapping> 

A 节也必须添加。 条目按列出的顺序处理。 的值必须与配置文件前面 节中 的值匹配。在此示例中,值为 AbcLandingPage.

<!-- so one of the other files will be displayed (index.html, index.htm, index.jsp) -->
<welcome-file-list>
    <welcome-file>AbcLandingPage</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

下面列出了一个完整的web.xml文件。 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<!-- ========================================================== -->
<!-- General Info -->
<!-- ========================================================== -->
<display-name>Abc</display-name>
<description>
   The web site for Abc software
</description>

<!-- ========================================================== -->
<!-- Home Servlet -->
<!-- ========================================================== -->
<servlet>
   <servlet-name>HomeServlet</servlet-name>
   <servlet-class>software.abc.website.HomeServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
 </servlet>

<!-- Map the HomeServlet name to the URI /HomeServlet (main page for site) --> 
 <servlet-mapping>
   <servlet-name>HomeServlet</servlet-name>
   <url-pattern>/AbcLandingPage</url-pattern>
 </servlet-mapping> 

<!-- ========================================================== -->
<!-- Welcome Files -->
<!-- ========================================================== -->

<!-- The main page for the site will be the HomeServlet servlet (http://website/AbcLandingPage) -->
<!-- No mapping is defined for other folders (http://website/someFolder/AbcLandingPage), -->
<!-- so one of the other files will be displayed (index.html, index.htm, index.jsp) -->
<welcome-file-list>
    <welcome-file>AbcLandingPage</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

需要对 server.xml 文件进行的更改

下面的更改列表适用于 conf/server.xml 文件。需要将新的 XML 元素添加到 XML 节中。 在 元素中需要添加一个 元素。 元素的 path= 属性需要为空。 docbase= 属性需要等于.WAR 文件根目录。在这个例子中 Abc_Website。我不确定 debug= 属性是否需要位于上下文元素中。但我不打算花时间测试它。我也不确定是否需要 reloadable= 属性。但是再次为了节省时间,它没有在删除 reloadable= 属性的情况下进行测试。

.
.
.
<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
    <Context path="" docBase="Abc_Website" debug="0" reloadable="true"></Context>
.
.
.

需要重新启动 Tomee 服务器才能使这些更改生效。

Tomcat 欢迎页面被自动覆盖。为了访问管理器应用程序,需要输入 URI http://999.999.999.999/manager/html,而不仅仅是域名。

看来最后的答案终究不是最终的

根据 Mark Tomas 在 Tomcat 用户电子邮件列表中的回复,此技术仍有更多改进空间。文档位于 (http://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Naming) 页。

对于我的 eclipse Mars.2 工作区,我还需要执行更多步骤。
1.停止Tomcat.
2. 移动而不是复制“${CATALINA_HOME}/webapps/ROOT”到另一个目录。不建议只删除“${CATALINA_HOME}/webapps/ROOT”,以便在您的工作区中有不同的东西阻止这些指令为您工作时可以恢复它。
3.重命名web项目ROOT##000,其中000是版本号。如果您使用的是 git,请不要忘记重命名 OS 文件夹名称。
4. 从 Web 项目上下文菜单 select "properties" 选项。
5. 选择 "Web Project Settings".
6.将"Context root"改为“/”
7. 按"Apply"按钮,然后按"OK"按钮。
8. 找到包含与 Eclipse 集成的 Tomcat 服务器的 Eclipse 实例的 "Servers" 项目。
9. 展开要更改的Tomcat实例。
10. 双击 "server.xml" 文件。
11. 找到 XML 元素。
12. 将 "path" XML 属性更改为“/”。

-<Context docBase="ROOT##000" path="/" reloadable="true" source="org.eclipse.jst.jee.server:ROOT##000"/>  
  1. 从 Eclipse 中启动 Tomcat。
  2. 测试仅输入域 (http://example.com) 名称现在将呈现在 WEB-INF/web.xml 文件的标记中配置的网页。