Two Laptop Bag
The moose likes Tomcat and the fly likes Tomcat 4 : How to deploy and run JSP and Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat 4 : How to deploy and run JSP and Servlets" Watch "Tomcat 4 : How to deploy and run JSP and Servlets" New topic

Tomcat 4 : How to deploy and run JSP and Servlets

Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
Dear friends,
After unpacking TOMCAT 4 - binary , i hv set CATALINA_HOME,JAVA_HOME and now tomcat-4 is running successfully (OS is win2k)
But i don't know where to put my JSPs, servlets and beans(not EJB) to access it through tomcat - 4
As per my knowledge it need to manipulate server.xml and web.xml file also
Please help - immediately
Thanks in advanced

SCJP2 (93%),SCWCD(88%)<br />-------------------------------<br />Never under estimate yr self, just represent yr profile in proper manner.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
There are two ways to deploy a servlet--an old way and a new way. The new way is to deploy your servlet as a web application. If you use the web application approach, then you just have to copy your webapp into the webapps directory and restart tomcat.
A good way to learn about web applications is to read chapter nine of the Java Servlet 2.3 Specification.
Download Java Servlet 2.3 Specification

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Let's create a context step by step:
#1 create your context root directory
Let's suppose the name of your Tomcat install directory is catalina.
In C:\catalina\webapps create a new folder, say myContext. Inside myContext, create a WEB-INF folder (all caps). Inside WEB-INF, create a classes and lib folders.
Go to C:\catalina\conf and copy the web.xml file there to C:\catalina\webapps\myContext\WEB-INF. Edit this web.xml so that you will have

Go to C:\catalina\conf and edit the server.xml file. Look for this entry:

Under it, insert this:

Putting <!-- User Defined Contexts --> is strictly unnecessary, of course, but I find it helpful to find my contexts.
Now all we need to do is test the setup. Create a simple JSP called index.jsp:

and put it in C:\catalina\webapps\myContext.
Go to C:\catalina\bin and type startup to start Tomcat. A DOS window will appear with the following message:

indicating Tomcat started normally.
Open a browser and type in this URL: http://localhost:8080/myContext
If your index page shows up, you have defined your context successfully.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
#2 Create a servlet and deploy it
Code a simple servlet for testing purposes:

Put in C:\catalina\webapps\myContext\WEB-INF\classes.
Get your favorite IDE, compile it, so that the resulting TemplateServlet.class is in the same folder.
(At this point some will argue that there is no need to put source code inside the classes folder, that you can use the javac -d option, etc. These are all valid points, but I'm trying to keep it simple now.)
Using the browser, type in this URL: http://localhost:8080/myContext/servlet/TemplateServlet
and you should get Test.
Note that we have NOT edited the web.xml file.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
# registering the servlet in the web.xml file
Go to C:\catalina\webapps\myContext\WEB-INF and edit the web.xml so that we have:

Use only Notepad or DOS edit. Do NOT use WordPad or other editors as it will change the formatting.
We have now "registered" the TemplateServlet with Tomcat with the alias template and it has the servlet path /test.
Please note that if you edit any Tomcat config file like web.xml or server.xml you have to restart Tomcat. After shutting Tomcat down, do not start up again immediately, since it is still releasing resources. The most common error of this type is a bind exception on the 8080 HTTP port that Tomcat uses.
After restarting Tomcat, type in this URL now:
and you should get the same servlet. Alternatively, you could use its alias, e.g
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
#4 create a JavaBean and deploy it
Now, let's create a simple JavaBean that works with a JSP and a servlet. We will make:
1. a main.html that takes a String parameter and forwards it to
2. a mainController.jsp that instantiates a
JavaBean and populates it with the String parameter, then forwards it to a servlet
3. a JavaBean called InputBean that has a property called "input"
4. an OutputServlet that takes the property from InputBean and displays it on the browser.
For main.html we have:

We'll put this html in the app root, C:\catalina\webapps\myContext.
For the JSP we have:

For more information on JSP tags, refer to the Sun link. Basically, since I will create the bean inside a package I will import it using the page directive, instantiate it with useBean, populate its "input" property with setProperty, and then finally forward it to the OutputServlet with servlet URL "/output". We'll also put this JSP in the app root, C:\catalina\webapps\myContext.
For the JavaBean, go to C:\catalina\webapps\myContext\WEB-INF\classes and create a folder named beans. Inside beans, create a JavaBean named InputBean:

Note that the public no-args constructor, as well the private fields and public accessor methods are part of the JavaBean specs.
Finally, we create the OutputServlet:

Note that we instantiate the actual JavaBean, retrieve its property and then display it to the client browser. Of course, this servlet resides in C:\catalina\webapps\myContext\WEB-INF\classes
We must register this servlet since the JSP uses a servlet mapping to forward the request. We must make the following changes to the web.xml file:

Note that all the <servlet> elements precede the <servlet-mapping> elements. This is not mere coincidence.
Finally, to "run" this webapp, simply type the URL
Reuben Cleetus
Ranch Hand

Joined: Jul 13, 2001
Posts: 50
Excellent answer(s) Anthony!
Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
Anthony , u r simply gr8 !
I mean yr answers were complete and can not have counter quetions !
Would like to be yr friend and would like to take yr further guidence.
Please note my email - id.
I agree. Here's the link:
subject: Tomcat 4 : How to deploy and run JSP and Servlets
It's not a secret anymore!