How to run a Tomcat example

R

Richard

Hi All,

Tomcat servlets-examples directory includes an HellowWorld.html
example. How I am I supposed to run this to as a Tomcat example?

I've got Tomcat running, as evidenced by the "Apache Tomcat/5.5.12"
webpage displayed in my Firefox browser at address http://localhost:8080/

Double-clicking HellowWorld.html
displays a webpage with the heading "Source Code for HelloWorld
Example" followed, of course, by HellowWorld.html's text beautifully
color coded.

My configuration:
WinXP-Pro/SP2, Ruby 1.8.2-15, Rails 1.1.6, Gem 0.9, MySQL 5.0.27-nt,
SciTE 1.72,
FireFox 2.0.0.1, IE 7.0, OE 6.0, MS Office 2003 SP2,
Java JVM 1.5.0_11-b03, Apache Tomcat/5.5.12,
AVG-Free 7.5.430, Prevx1 2.0.2.23, Startup Cop Pro 2.03

TIA,
Richard
 
C

Carl

Richard said:
Hi All,

Tomcat servlets-examples directory includes an HellowWorld.html
example. How I am I supposed to run this to as a Tomcat example?

I've got Tomcat running, as evidenced by the "Apache Tomcat/5.5.12"
webpage displayed in my Firefox browser at address http://localhost:8080/

Double-clicking HellowWorld.html
displays a webpage with the heading "Source Code for HelloWorld
Example" followed, of course, by HellowWorld.html's text beautifully
color coded.

My configuration:
WinXP-Pro/SP2, Ruby 1.8.2-15, Rails 1.1.6, Gem 0.9, MySQL 5.0.27-nt,
SciTE 1.72,
FireFox 2.0.0.1, IE 7.0, OE 6.0, MS Office 2003 SP2,
Java JVM 1.5.0_11-b03, Apache Tomcat/5.5.12,
AVG-Free 7.5.430, Prevx1 2.0.2.23, Startup Cop Pro 2.03

TIA,
Richard

The simple answer is to use the menu options. From the
"Welcome Page" @ localhost:8080, you should see an examples box/menu
on the left side of the screen which will walk you through all the
examples.

I think it's worth mentioning that if you want the know the url for
a particular example, you can take a look inside WEB-INF/web.xml for
that web application. In this case, a search for the
HelloWorldExample class matches a servlet with the name
"HelloWorldExample", which is then mapped to the url
/servlet/HelloWorldExample . So you can go directly to
http://localhost:8080/servlet/HelloWorldExample

Hope that helps,
Carl.
 
C

Carl

Carl said:
I think it's worth mentioning that if you want the know the url for
a particular example, you can take a look inside WEB-INF/web.xml for
that web application. In this case, a search for the
HelloWorldExample class matches a servlet with the name
"HelloWorldExample", which is then mapped to the url
/servlet/HelloWorldExample . So you can go directly to
http://localhost:8080/servlet/HelloWorldExample

Doh!, that should have been:
http://localhost:8080/servlets-examples/servlet/HelloWorldExample
as setup by default on 5.5 .
 
R

Richard

Doh!, that should have been:http://localhost:8080/servlets-examples/servlet/HelloWorldExample
as setup by default on 5.5 .

Hi Carl,

Thanks! That was great help ... it worked as advertised both with
Firefox 2 and IE 7.
I think it's worth mentioning ...

Absolutely. This is even better! I opened
Tomcat 5.5\webapps\servlets-examples\WEB-INF\web.xml
in Oxygen, an XML editor, and followed your excellent analysis.

I'm just have one question:
- the original example was helloworld.html
- searching for that fails
- dropping the extension turns up
- <servlet-name>HelloWorldExample</servlet-name>

What gives us assurance the "helloworld.html" is related to the
"HelloWorldExample" servlet (aside from just trying it)?

Thank you again for your excellent response. I regret having to pile
on one more question.

Best wishes,
Richard
 
C

Carl

Richard said:
Hi Carl,


Thanks! That was great help ... it worked as advertised both with
Firefox 2 and IE 7.


Absolutely. This is even better! I opened
Tomcat 5.5\webapps\servlets-examples\WEB-INF\web.xml
in Oxygen, an XML editor, and followed your excellent analysis.

I'm just have one question:
- the original example was helloworld.html
- searching for that fails
- dropping the extension turns up
- <servlet-name>HelloWorldExample</servlet-name>

What gives us assurance the "helloworld.html" is related to the
"HelloWorldExample" servlet (aside from just trying it)?

Thank you again for your excellent response. I regret having to pile
on one more question.

Best wishes,
Richard

Hi Richard,

In this example, the "helloworld.html" file is not actually related
to the HelloWorldExample servlet. The "helloworld.html" file is a
static html page that displays the source code for a servlet class
named HelloWorld . To make matters more confusing, the actual
servlet included in the examples with tomcat 5.5 is named
"HelloWorldExample", not "HelloWorld", as shown in the example
source.

The thing to note here is that content in the root of the web-app
directory ('tomcat-home/webapps/servlets-examples/') can be accessed
directly by the client. i.e. in order to view
servlets-examples/helloworld.html you just point your browser to
http://localhost:8080/servlets-examples/helloworld.html
and tomcat will serve it to you. In this example, you receive
a plain old html page, not a jsp page or servlet output.

The example servlet, however, is accessed
using the url-pattern shown in the web.xml file. In this case, any
request to a url that matches the pattern
"/servlet/HelloWorldExample" will be sent to a servlet named
HelloWorldExample. The source for this servlet should be found in:
servlet-examples/WEB-INF/classes/HelloWorldExample.java
This is the code that is called when you actually run the example.

Hope that makes sense :\
Carl.
 
R

Richard

Hi Carl,
Hope that makes sense

It sure does, though I'm going have to check out all the pieces, one
by one.

Thank you for such a clear and detailed explanation.

Yours truly,
Richard
 
R

Richard

Hi Carl,

It's starting to make sense to me, but I'm still in woods with at
least one detail.
In this case, any request to a url that matches the pattern
"/servlet/HelloWorldExample" will be sent to a servlet named
HelloWorldExample.

I gave Firefox the address http://localhost:8080/ and got the Apache
Tomcat 5.5.12 home page, so Tomcat's running fine.

But the address http://localhost:8080/servlet/HelloWorldExample gave
me a status 404: The requested resource (/servlet/HelloWorldExample)
is not available. (I tried a bunch of things to no avail: adding
extensions, more of the path, etc. Maybe I should reinstall Tomcat in
a path that does not have any spaces, e.g as "Tomcat 5.5" does.)

The files HelloWorldExample.java and .class are in
Tomcat 5.5\webapps\servlets-examples\WEB-INF\classes
and the .java file defines HelloWorldExample
as a public class that extends HttpServlet.

BTW, http://localhost:8080/servlets-examples/helloworld.html still
displays the servlet HelloWorldExample.java code as text.

Can you guess where I'm going wrong. If you need more info, I'd be
pleased to provide whatever you might think helpful in extracting me
from this quagmire.

Best wishes,
Richard
 
R

Richard

Hi Carl,

I've made progress:
- I upgraded to Tomcat 6.0.13
- installed it in Apache\Tomcat_6.0 (no blanks in path!)
- noted the Servlets Examples link on Tomcat's homepage
- noted the Execute link for Hello World:
- - http://localhost:8080/
- - examples/servlets/
- - servlet/HelloWorldExample
- double-clicked that link

That gave me a vanilla "Hello World!" page, i.e.
- <title>Hello World!</title>
- <h1>Hello World!</h1>
and miscellaneous HTML.

I'm still researching the how it all hooks together, especially the
WEB-INF\web.xml. So no more questions for you (at least from me for
you).

Thanks for your help,
Best wishes,
Richard
 
C

Carl

Richard said:
Hi Carl,

It's starting to make sense to me, but I'm still in woods with at
least one detail.

"/servlet/HelloWorldExample" will be sent to a servlet named
HelloWorldExample.

I gave Firefox the address http://localhost:8080/ and got the Apache
Tomcat 5.5.12 home page, so Tomcat's running fine.

But the address http://localhost:8080/servlet/HelloWorldExample gave
me a status 404: The requested resource (/servlet/HelloWorldExample)
is not available. (I tried a bunch of things to no avail: adding
extensions, more of the path, etc. Maybe I should reinstall Tomcat in
a path that does not have any spaces, e.g as "Tomcat 5.5" does.)

The files HelloWorldExample.java and .class are in
Tomcat 5.5\webapps\servlets-examples\WEB-INF\classes
and the .java file defines HelloWorldExample
as a public class that extends HttpServlet.

BTW, http://localhost:8080/servlets-examples/helloworld.html still
displays the servlet HelloWorldExample.java code as text.

Can you guess where I'm going wrong. If you need more info, I'd be
pleased to provide whatever you might think helpful in extracting me
from this quagmire.

Best wishes,
Richard

Hi Richard,

Sorry for the late reply, I entirely missed this followup.
I hope you've found the problem by now, but just in case...

In your first example, you're simply missing the web application
directory in your url. The url patterns in the web.xml file are
going to be relative to the web application directory, not the
tomcat directory for web applications (a bit confusing, i know...)
in this case, the directory named "servlets-examples"

http://localhost:8080/servlet/HelloWorldExample
should have been
http://localhost:8080/servlets-examples/servlet/HelloWorldExample

Hope that helps,
Carl.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,483
Latest member
TedDvb6626

Latest Threads

Top