You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Thad Humphries <th...@gmail.com> on 2020/02/18 20:10:43 UTC

Embedding with Tomcat 9

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 = "" ;
      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?

I've tried variations on other Tomcat 9 examples (such as
https://nkonev.name/post/101). There main() methods are considerably more
involved, and I don't follow all that's going on. I've had no success. Some
don't run, or, in the nkonev example earlier, JSPs aren't processed
(probably because of "tomcat.setAddDefaultWebXmlToWebapp(false);" but if I
commnent that out, the Jar won't run).

What's the minimum to get the above main() to serve /employee?

-- 
"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)

Re: Embedding with Tomcat 9

Posted by Thad Humphries <th...@gmail.com>.
On Tue, Feb 18, 2020 at 6:29 PM Mark Thomas <ma...@apache.org> wrote:

> On 18/02/2020 23:13, Thad Humphries wrote:
> > 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 <th...@gmail.com>
>
> <snip/>
>
> >>>> 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"}
> >   )
>
> OK. That will be relying on the StandardJarScanner finding that class -
> which it should.
>
> > In src/main/webapp is the file index.jsp which redirects to /employee:
>
> OK. Maven is copying that to META-INF/resources and hoping that Tomcat
> will treat the entire JAR as a resource JAR. That should work.
>
> > <%@ 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
>
> Not sure which part is failing at this point. Using a purely static JSP
> without the forward and accessing /employee directly should tell you which.


If I comment out the jsp:forward, http://localhost:8888/ loads. However
http://localhost:8888/employee
still returns a 404.

I'd then recommend debugging your way though the Tomcat start process to
>
see where whichever element is failing is going wrong.
>

I'll see if I can figure out how to do that. Eclipse and I are not on the
best of terms. Frankly, loading JSPs is not something I need to do for how
I envision using embedded Tomcat. But I figure I need to know what's
happening because if I don't, I'm sure to run into problems down the road
when I'm dealing with something more complex.

-- 
"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)

Re: Embedding with Tomcat 9

Posted by Mark Thomas <ma...@apache.org>.
On 18/02/2020 23:13, Thad Humphries wrote:
> 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 <th...@gmail.com>

<snip/>

>>>> 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"}
>   )

OK. That will be relying on the StandardJarScanner finding that class -
which it should.

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

OK. Maven is copying that to META-INF/resources and hoping that Tomcat
will treat the entire JAR as a resource JAR. That should work.

> <%@ 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

Not sure which part is failing at this point. Using a purely static JSP
without the forward and accessing /employee directly should tell you which.

I'd then recommend debugging your way though the Tomcat start process to
see where whichever element is failing is going wrong.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Embedding with Tomcat 9

Posted by Thad Humphries <th...@gmail.com>.
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 <th...@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)

Re: Embedding with Tomcat 9

Posted by Mark Thomas <ma...@apache.org>.
On 18/02/2020 22:32, calder wrote:
> On Tue, Feb 18, 2020, 14:12 Thad Humphries <th...@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?

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Embedding with Tomcat 9

Posted by calder <ca...@gmail.com>.
On Tue, Feb 18, 2020, 14:12 Thad Humphries <th...@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 = "/" ;


In the future, you should also check the full stack trace for clues


      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?
>
> I've tried variations on other Tomcat 9 examples (such as
> https://nkonev.name/post/101). There main() methods are considerably more
> involved, and I don't follow all that's going on. I've had no success. Some
> don't run, or, in the nkonev example earlier, JSPs aren't processed
> (probably because of "tomcat.setAddDefaultWebXmlToWebapp(false);" but if I
> commnent that out, the Jar won't run).
>
> What's the minimum to get the above main() to serve /employee?
>
>