Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
bob von ilten
Greenhorn
Posts: 19
Linux Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in your config files. Start with server.xml.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
Linux Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic