• Post Reply Bookmark Topic Watch Topic
  • New Topic

Running basic servlets with Tomcat

 
Brian Mulvany
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I just want to know how to I get a basic servlet running with Tomcat. I have managed to get a basic servlet running with JBuilder. This servlet takes paramaters that I enter in a html form and returns the values.
I have been reading up on www.coreservlets.com and on that website it gives me code to get the data typed in on a html form to be returned in a Servlet.
I dont know where to start in trying to get it running using Tomcat. Do I have to compile the Servlets first or when they are called from the html page will Tomcat compile them.
Sorry if I seem a bit stupid here but I honestly dont know where to start.
The code can be found at http://archive.coreservlets.com/ or if you want I can post it up here. Its the ThreeParams example from chapter 3.
Any help is greatly appreciated.
Thanks
Brian
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you need to compile it.
You will also need to create a mapping in your web.xml file. The Core Servlets book is a bit out of date in this respect. The 1st edition still makes reference to the invoker servlet which is no longer active by default in Tomcat.

In order to compile your servlet, you will need TOMCAT_HOME/common/lib/servlet-api.jar on your classpath.

If you want a basic "hello, world" war file, you can get one here:
http://simple.souther.us
Just download SimpleServlet.war to your TOMCAT_HOME/webapps directory and it will run.

You will then have a complete, working example of a servlet app -- with the correct directory structure, and proper web.xml file.
 
Brian Mulvany
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Im actually looking to get a servlet to work with a html form because I have several Java classes that search www.cdwow.com for a particular cd. They search for whatever I have built in as the search paramater in JBuilder. I want to be able to have a html form and to send whatever value is typed in the form to be returned to the servlet. Then I want to use this value as the search paramater for the Java classes.
So I need to get the htmlform servlet working.
What is the handiest way of compiling the servlet.
How do I add the classpath lets say if I use command prompt to compile it.
Also the servlet on the coreservlets web page is in a package
Thanks
Brian
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiling a servlet is no different than compiling any Java class.
You will have to satisfy the dependencies by putting servlet-api.jar on your classpath (if using Tomcat... alternatively you can add j2ee.jar to your classpath).

If you're not sure how to compile java code from the command line, I suggest googling for a Java tutorial and following it before tackling servlets.
There is a good one at http://java.sun.com/learning.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!