Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Servlet beginners problem  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I'm trying to run servlets using apache tomcat.

The examples that come packaged with Apache work when I put http://localhost:8080/examples/servlets/servlet/HelloWorldExample in as the URL fpr example.

If I add my own java class to the same directory as this example however ie \Tomcat 6.0\webapps\examples\WEB-INF\classes and use the URL
http://localhost:8080/examples/servlets/servlet/MyOwnServletClass I get an HTTP 404 error saying that my servlet is not available.

Does anyone please have some tips on whether I'm using the wrong directory or possibly have a classpath issue. The only Apache realted environment variable I have set currently is a PATH to ..\Tomcat 6.0\bin.

Apologies if my error is obvious.

Any help much appreciated.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Does anyone please have some tips on whether I'm using the wrong directory or possibly have a classpath issue.


My tip: read the servlet spec.

Then add mappings for your servlet in the web.xml
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is configured your web.xml ?
how the servlet is called, get method or post?
ciao,
gianni
 
And Green
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet is using the get method:


The Web XML file is here (sorry its big):


[BPSouther: Added UBB Code tags]
[ October 17, 2007: Message edited by: Ben Souther ]
 
Mladen Grabowsky
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't mapped the servlet.

You have to map a class to a servlet name


then that servlet name has to be mapped to an url

This is just an example, in real life you do not map servlets to the /servlets/... url.
[ October 15, 2007: Message edited by: Mladen Grabowsky ]
 
And Green
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats great, worked a treat, thanks for you time on that one.

Is it the case then that I have to modify web.xml, every time, to create a servlet? seems like a bit of a pain and I expect that file is going to get pretty big!

One last thing, when you say, in reality you don't have to map to "servlets/" this is just an example, I'm not sure what you mean. Do you mind just giving a quick explanation. All the other servlets in the web.xml file provide paths that include the servlets directory.

Thanks again.
 
Author and ninkuma
Marshal
Posts: 66801
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by And Green:
Is it the case then that I have to modify web.xml, every time, to create a servlet? seems like a bit of a pain and I expect that file is going to get pretty big!


That's what Front Controllers are for. Perhaps this article might be helpful.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!