On Tue, Feb 18, 2020 at 5:41 PM Mark Thomas <ma...@apache.org> wrote:

> On 18/02/2020 22:32, calder wrote:
> > On Tue, Feb 18, 2020, 14:12 Thad Humphries <thad.humphr...@gmail.com>
> wrote:
> >
> >> I am trying to understand how to build and run an app from local with
> >> Tomcat 9 embedded using Java 8. I've started with this example written
> for
> >> Tomcat 7:
> >>
> >>
> https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/basic_app_embedded_tomcat/basic_app-tomcat-embedded.html#overview
> >>
> >> I am able to get it to run with Tomcat 9 after a few changes to the
> pom.xml
> >> and one addition to the main() method:
> >>
> >>     public static void main(String[] args) throws Exception {
> >>
> >
> >
> >       String contextPath = "" ;
> >>
> >
> > May not be the issue, cause there's other code to consider, but should
> the
> > above line be
> >
> > String contextPath = "/" ;
>
> No. "" is the correct path for the ROOT context.
>
>
> >>       String appBase = ".";
> >>       Tomcat tomcat = new Tomcat();
> >>       tomcat.setPort(Integer.valueOf(PORT.orElse("8888") ));
> >>       // next line added for Tomcat 9
> >>       tomcat.setConnector(tomcat.getConnector());
> >>
> >>       tomcat.setHostname(HOSTNAME.orElse("localhost"));
> >>       tomcat.getHost().setAppBase(appBase);
> >>       tomcat.addWebapp(contextPath, appBase);
> >>       tomcat.start();
> >>       tomcat.getServer().await();
> >>     }
> >>
> >> However although this runs it returns a 404 on /employee. Why?
>
> There aren't (doesn't appear to be)  any Servlets mapped.
>
> Are there any web applications in the appBase?
>

There is a servlet
in src/main/java/com/example/employees, EmployeeServlet.java, with the
annotation

  @WebServlet(
    name = "EmployeeServlet",
    urlPatterns = {"/employee"}
  )

In src/main/webapp is the file index.jsp which redirects to /employee:

<%@ page info="sample index page" %>
<html>
<body>
<h2>Hello World!</h2>
<jsp:forward page="/employee" />
</body>
</html>

With Tomcat 7.0.57, I can run `java -jar
employees-app-1.0-SNAPSHOT-jar-with-dependencies.jar` in target, and load
the page at http://localhost:8888/.  With Tomcat 9.0.31, I get a 404

-- 
"Hell hath no limits, nor is circumscrib'd In one self-place; but where we
are is hell, And where hell is, there must we ever be" --Christopher
Marlowe, *Doctor Faustus* (v. 111-13)

Reply via email to