paulhr
paulhr

Reputation: 379

Configure tomcat to serve only one website as the default

I have been forced to move my website to a static IP. This is requiring me to change my configuration on tomcat (Tomee-Plume). What I don't know how to do is map port 80 directly to the web site. So for example the old set had the following stealth forwarding setup.

Old Stealth setting.

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

Now I need to push the resolution of the "/Websites/HomeServlet" part into the tomcat (Tomee-Plume) configuration because I am using just an internet "A" record to map the domain name to an IP. There is no ability to put the "/Websites/HomeServlet" part into a internet "A" record.

I am guessing this setup is common, but I don't know what it is commonly called to do internet search on.


Below content was added content as per Shankar P S's request.


File layout in 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>


Still get the tomcat welcome page.

enter image description here


This works on my Windows 7 workstation and Eclipse Mars 2 (4.5.2)

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

But this does not work on my Fedora 23 server.

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

Window 7 (Works)

"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 (Throws exceptions)

/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

Upvotes: 0

Views: 1756

Answers (2)

paulhr
paulhr

Reputation: 379

Final answer...There are changes that need to be made to the web.xml file and to the server.xml file.

Changes that need to be made to the web.xml file.

The below list of changes are for the WEB-INF/web.xml file not the server level conf/web.xml file. A prerequisite is that the and are in the XML stanza. The XML element is not required for the task at hand. It was added so the first user, after a tomee restart, did not experience poor performance.

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

A XML stanza needs to be made also. The in this stanza must agree with the in the stanza. The does not need to match the . The tag must match the tag in the stanza.

<!-- 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 stanza must also be added. The entries are processed in the order listed. The value for the must match the value for the in the stanza earlier in the configuration file. In this example the value is 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>

A complete web.xml file is listed below. 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>

Changes that need to be made to the server.xml file

The below list of changes are for the conf/server.xml file. A new XML element needs to be added to the XML stanza. Within the element stands a element needs to be added. The path= attribute for the element needs to be blank. The docbase= attribute needs to be equal to the .WAR file root directory. In this example Abc_Website. I'm not sure the debug= attribute needs to be in the context element. But I wasn't going to spend time testing it. I am also not sure if the reloadable= attribute is required. But once again in the interest of saving time it was not tested with the reloadable= attribute removed.

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

A restart of the Tomee server is required for these changes to take effect.

The Tomcat welcome page gets overridden automatically. In order to access the manager application going forward the URI, http://999.999.999.999/manager/html, needs to be entered, not just the domain name.

Its seems the final answer was not final after all.

As per a reply from Mark Tomas in the Tomcat users email list there is still more room for improvement with this technique. The documentation sited is (http://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Naming) page.

For my eclipse Mars.2 workspace I also have to do a few more steps.
1. Stop Tomcat.
2. Move, not copy, the "${CATALINA_HOME}/webapps/ROOT" to another directory. It is not recommended to just delete the "${CATALINA_HOME}/webapps/ROOT" so that it can be restored in the event that there is something different in your workspace that prevents these instructions from working for you.
3. Rename the web project ROOT##000, where 000 is a version number. If you are using git don't forget to also rename the OS folder name.
4. From the Web project context menu select the "properties" option.
5. Pick "Web Project Settings".
6. Change the "Context root" to "/"
7. Press the "Apply" button, then press "OK" button.
8. Find the "Servers" project that contains the Eclipse instance of your Tomcat server that it integrated with Eclipse.
9. Expand the instance of Tomcat that it to be changed.
10. Double click the "server.xml" file.
11. Find the XML element.
12. Change the "path" XML attribute to just "/".

-<Context docBase="ROOT##000" path="/" reloadable="true" source="org.eclipse.jst.jee.server:ROOT##000"/>  
  1. Start Tomcat from within Eclipse.
  2. Test that entering just the domain (http://example.com) name will now render the web page that was configured in the tag of the WEB-INF/web.xml file.

Upvotes: 1

Shankar
Shankar

Reputation: 2825

You need to do two things. Change the default port of Tomcat from 8080 to 80, and allow the requests to Tomcat to hit your project. Try this.

In the file {TOMCAT_CATALINA}/conf/server.xml, look for the Connector tag for Protocol HTTP/1.1. This will be mapped to 8080. Change it to 80, as below.

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

Then, in same file, add this under Host tag. (I am assuming that your WAR file name is Websites. This is explained in Method 2.1 in this page - 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>

Now, calls to http://my.domain will reach your WAR file.

Next, map the servlet to the default path in your project's web.xml.

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

Now, calls to http://my.domain are equivalent to http://YOUR_IP/Websites/HomeServlet

Upvotes: 0

Related Questions