This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Tomcat and the fly likes tomcat 7 servlet examples work but I don't know why Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "tomcat 7 servlet examples work but I don Watch "tomcat 7 servlet examples work but I don New topic
Author

tomcat 7 servlet examples work but I don't know why

bob von ilten
Greenhorn

Joined: Apr 29, 2013
Posts: 19

I installed tomcat 7 on apache on os x 10.8 so far so good, checked out the examples the all worked. so whats the problem?
I don't understand why they work.

the path on the tomcat server is /Library/Tomcat/webapps/examples/WEB-INF/classes/HelloWorldExample.class

the link on the servlet example page is http://localhost:8080/examples/servlets/servlet/HelloWorldExample

There is no path for examples/servlets/servlet there is a path for examples/servlets but the only thing in that directory is helloworld.html and
nothing seems to point to it.

I have tried numerous times to create my own HelloWorldExample but I'm just not getting it. Can someone tell me how
http://localhost:8080/examples/servlets/servlet/HelloWorldExample gets to /Library/Tomcat/webapps/examples/WEB-INF/classes/HelloWorldExample.class



Thank You for your time
Bob
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

The configuration to map servlets to their URLs is in the web app's deployment descriptor (web.xml).

P.S. It is considered amateurish to use the word "servlet" in a mapping despite the examples.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Look in your config files. Start with server.xml.


Please ignore post, I have no idea what I am talking about.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

William Barnes wrote:Look in your config files. Start with server.xml.


Server.xml has little to do with servlet mapping. The only influence it might have is when configuring multiple hosts, but that will have no influence on the servlet path.
bob von ilten
Greenhorn

Joined: Apr 29, 2013
Posts: 19

I did find the web.xml file and there did not seem to be anything pointing to the actual location

<servlet>
<servlet-name>HelloWorldExample</servlet-name>
<servlet-class>HelloWorldExample</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorldExample</servlet-name>
<url-pattern>/servlets/servlet/HelloWorldExample</url-pattern>
</servlet-mapping>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

The <servlet-class> element identifies the class of the servlet within the classpath. This is a bad example because the servlet is not in a package and it should be.

You will never see a file path to the servlet, or any other Java class, because that's not the way Java works. Classes are searched for in the classpath, not a file path. The classes in the package structure under WEB-INF/classes are automatically added to the web app's classpath. (As are any jars in WEB-INF/lib).
 
Don't get me started about those stupid light bulbs.
 
subject: tomcat 7 servlet examples work but I don't know why
 
Similar Threads
New Wep app servlets don't work
basic Tomcat setup and MissingResourceException
setting up Tomcat
Path Not Reaching Servlet
confusion err message "HTTP method GET is not supported by this URL"