Chris:
Thank you very much.

That worked as expected .

The following additional lines are printed:


Sep 13, 2019 12:40:46 AM org.apache.coyote.http11.Http11NioProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]

The tests under the script tests work as expected against Tomcat this time.

I'm using the latest jar versions from Maven. 9.0.24

It would help to have more documented examples for the embedded Tomcat
versions.

Thanks for the link to the StackOverflow question.

However, that does not show up among the top results for a query on Google
Mobile Search.
Regards,
Linus.




>
>
> ---------- Forwarded message ----------
> From: LINUS FERNANDES <linus.fernan...@gmail.com>
> To: users@tomcat.apache.org
> Cc:
> Bcc:
> Date: Thu, 12 Sep 2019 19:03:25 +0530
> Subject: Embedded Tomcat server starts but unable to connect via browser
> on mobile phone.
> I have set up an embedded Tomcat server program on Arch Linux on Termux
> using OpenJDK version 12.
>
> However, I am unable to connect to the server using the browser. The same
> WAR file with a web.xml works fine on Jetty.
>
> Could you point me in the right direction?
>
> The Java class is as follows:
>
> package launch;
>
> import java.text.MessageFormat;
> import java.util.logging.Logger;
> import org.apache.catalina.Context;
> import org.apache.catalina.LifecycleException;
> import org.apache.catalina.WebResourceRoot;
> import org.apache.catalina.startup.Tomcat;
> import org.apache.catalina.webresources.DirResourceSet;
> import org.apache.catalina.webresources.StandardRoot;
>
> @SuppressWarnings("PMD.ShortClassName")
> public final class Main {
>   private static final Logger LOGGER =
> Logger.getLogger(Main.class.getName());
>   private static final String WORKING_DIR =
>       System.getProperty("java.io.tmpdir") + "/webapps";
>
>   private Main() {
>     throw new IllegalStateException("Private constructor");
>   }
>
>   public static void main(String[] args) {
>     try {
>
>       Tomcat tomcat = new Tomcat();
>
>       // The port that we should run on can be set into an environment
> variable
>       // Look for that variable and default to 8080 if it isn't there.
>       String webPort = System.getenv("PORT");
>       if (webPort == null || webPort.isEmpty()) {
>         webPort = "8080";
>       }
>       System.out.println(webPort);
>       tomcat.setSilent(false);
>       tomcat.setPort(Integer.valueOf(webPort));
>       tomcat.setBaseDir(WORKING_DIR);
>       tomcat.getHost().setAppBase(WORKING_DIR);
>       tomcat.getHost().setAutoDeploy(true);
>       tomcat.getHost().setDeployOnStartup(true);
>
>
>       System.out.println(System.getProperty("catalina.home"));
>       System.out.println(System.getProperty("catalina.base"));
>
>       String userDir = System.getProperty("user.dir");
>       String webappDirLocation = userDir + "/dist/Webapps-2.0.0.war";
>
>
> tomcat.getHost().getAppBaseFile().mkdir();
>       // Ensure that the webapps directory exists
>
>       Context appContext =
>           tomcat.addWebapp(tomcat.getHost(), "/Webapp", webappDirLocation);
>       appContext.setParentClassLoader(
>           Thread.currentThread().getContextClassLoader());
>       WebResourceRoot resources = new StandardRoot(appContext);
>       resources.addPreResources(
>           new DirResourceSet(resources, "/WEB-INF/classes", "", "/"));
>       appContext.setResources(resources);
>       tomcat.start();
>       LOGGER.info(() -> {
>         return MessageFormat.format("Deployed {0} as {1}",
>                                     appContext.getBaseName(),
>                                     appContext.getBaseName());
>       });
>       tomcat.getServer().await();
>     } catch (LifecycleException lce) {
>       System.err.println(lce);
>     }
>   }
> }
>
> The web.xml is as follows. It's probably irrelevant.
>
> <?xml version="1.0" encoding="iso-8859-1"?>
> <web-app xmlns=" http://java.sun.com/xml/ns/javaee "
>          xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
>          xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "
>          version="3.0">
>   <servlet>
>     <servlet-name>FirstServlet</servlet-name>
>     <servlet-class>servlets.FirstServlet</servlet-class>
>   </servlet>
>   <servlet>
>     <servlet-name>DbServlet</servlet-name>
>     <servlet-class>servlets.DbServlet</servlet-class>
>   </servlet>
>   <servlet>
>     <servlet-name>GetPreferences</servlet-name>
>     <servlet-class>servlets.GetPreferences</servlet-class>
>   </servlet>
>   <servlet>
>     <servlet-name>Checkout</servlet-name>
>     <servlet-class>servlets.Checkout</servlet-class>
>   </servlet>
>   <servlet>
>     <servlet-name>AdderServlet</servlet-name>
>     <servlet-class>servlets.AdderServlet</servlet-class>
>   </servlet>
>   <servlet>
>     <servlet-name>PersonalServlet</servlet-name>
>     <servlet-class>servlets.PersonalServlet</servlet-class>
>   </servlet>
>   <servlet>
>     <servlet-name>Selection</servlet-name>
>     <servlet-class>servlets.Selection</servlet-class>
>   </servlet>
>   <servlet>
>     <servlet-name>ShowSum</servlet-name>
>     <servlet-class>servlets.ShowSum</servlet-class>
>   </servlet>
>   <servlet>
>     <servlet-name>Weight</servlet-name>
>     <servlet-class>servlets.Weight</servlet-class>
>   </servlet>
>   <servlet-mapping>
>     <servlet-name>FirstServlet</servlet-name>
>     <url-pattern>/FirstServlet</url-pattern>
>   </servlet-mapping>
>   <servlet-mapping>
>     <servlet-name>Checkout</servlet-name>
>     <url-pattern>/Checkout</url-pattern>
>   </servlet-mapping>
>   <servlet-mapping>
>     <servlet-name>DbServlet</servlet-name>
>     <url-pattern>/DbServlet</url-pattern>
>   </servlet-mapping>
>   <servlet-mapping>
>     <servlet-name>PersonalServlet</servlet-name>
>     <url-pattern>/PersonalServlet</url-pattern>
>   </servlet-mapping>
>   <servlet-mapping>
>     <servlet-name>AdderServlet</servlet-name>
>     <url-pattern>/AdderServlet</url-pattern>
>   </servlet-mapping>
>   <servlet-mapping>
>     <servlet-name>GetPreferences</servlet-name>
>     <url-pattern>/GetPreferences</url-pattern>
>   </servlet-mapping>
>   <servlet-mapping>
>     <servlet-name>Selection</servlet-name>
>     <url-pattern>/Selection</url-pattern>
>   </servlet-mapping>
>   <servlet-mapping>
>     <servlet-name>ShowSum</servlet-name>
>     <url-pattern>/ShowSum</url-pattern>
>   </servlet-mapping>
>   <servlet-mapping>
>     <servlet-name>Weight</servlet-name>
>     <url-pattern>/Weight</url-pattern>
>   </servlet-mapping>
>   <welcome-file-list>
>     <welcome-file>index.html</welcome-file>
>   </welcome-file-list>
> </web-app>
>
> The project is hosted at
> https://github.com/Fernal73/LearnJava/tree/master/Webapps
>
> Regards,
> Linus Fernandes.
>
>
>
> ---------- Forwarded message ----------
> From: Christopher Schultz <ch...@christopherschultz.net>
> To: users@tomcat.apache.org
> Cc:
> Bcc:
> Date: Thu, 12 Sep 2019 13:52:56 -0400
> Subject: Re: Embedded Tomcat server starts but unable to connect via
> browser on mobile phone.
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> Linus,
>
> On 9/12/19 09:33, LINUS FERNANDES wrote:
> > I have set up an embedded Tomcat server program on Arch Linux on
> > Termux using OpenJDK version 12.
>
> What version of Tomcat?
>
> > However, I am unable to connect to the server using the browser.
> > The same WAR file with a web.xml works fine on Jetty.
> >
> > Could you point me in the right direction?
>
> You are probably missing a call to Tomcat.getConnector() to create a
> default connector.
>
> https://stackoverflow.com/questions/48998387/code-works-with-embedded-ap
> ache-tomcat-8-but-not-with-9-whats-changed/49011424#49011424
> <https://stackoverflow.com/questions/48998387/code-works-with-embedded-apache-tomcat-8-but-not-with-9-whats-changed/49011424#49011424>
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/
>
> iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAl16hfgACgkQHPApP6U8
> pFjUshAAhft5Xl8JHEn1mR//jMUIczh6IwLzytyXFV4XahSFXxsKDSP2YdIJT0Pi
> OeWckF+toejXvlpZ8D2tNjqJeyMJwAijVn9EqVacz/uxXmXCpXUAAfBqqwwwVCwg
> uzhWwf1E7CFtXL+jG9YtEcoRWKxXbL2Jsr3zNW1jPlMH/3vdi3jgUtidshn4iovG
> q99JUNrMIfHVDafd9n6KDR3jzVBMUBrMHy2nO3G9bw2wVeaFHXv2rgXT5hzTe8oD
> w5O3DztKnEIY6LhKILmkrDRBFs6zT1oUX5XDdQmtpzoFfzDzvP6I0EhoUr8aLhOi
> f8W35Y1R0gAd0XG1KhwNlNXXwVtKztYGVAtwkhvzwTt5XCM2nDjRt4wpakw5XkoX
> VBJBrhn0X5D/iICrU0TbnJqHwfmoTpwji0ISTfVSCzmy2V36sfVE0ud/DhlxIEhO
> CWBTpsYlBrRs2/kpeskqzsDQ/XxdVU6vZ34YfbYmJHdK3UBkCeo8wdK4CgrN4pTb
> 7pyY0u9tNZPMnpkyaPtauB2GSlMGdEGRHtIOXZpJU0mUmvQ5OAXGprAFnx6Vif2I
> TL+GWFv/yLn8AVB/Jp2wRDeplCmz6Qt2aoaH+KpfDC7vvNC3F+OtOdLVsH9ipPEV
> lXH1C2xtFaUuNqi/Q+a0Qj3HGgwTSYc0TjB6oRXDiuAutSQL2DI=
> =MBh6
> -----END PGP SIGNATURE-----
>
>
>
>
>

Reply via email to