It's not a secret anymore!*
The moose likes Servlets and the fly likes classpath problem.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "classpath problem.." Watch "classpath problem.." New topic
Author

classpath problem..

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Hi All,
I am using tomcat 4.1 for the servlet and JSp page development.
In the Tomcat directory structure all the default examples are in the "example" directory.
Under that directory they have created subdirecories like "jsp", "servlets" etc.
If I don't want to put my servlets and Jsp pages under this directory. so I created a different directory under the "Root" directory and tried to access my servlets and jsps from that directory ..but I got an error " the servlet and jsp doesn't exist" ..but the same servlets and jsps I am able to access if I put them in the "examples" directory.
Is there any default setting that I have to do; so that I can access those from the directory which I created under the "root" directory.
thanks in advance..
trupti
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Try this example if you like..
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.
#2 Create a servlet and deploy it
Code a simple servlet for testing purposes:

Put TemplateServlet.java 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.
#3 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:
http://localhost:8080/myContext/test
and you should get the same servlet. Alternatively, you could use its alias, e.g
http://localhost:8080/myContext/servlet/template
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
Great Reply Anthony!Couldnt be better!Keep helping us ranchers in times of need.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Thanks Anthony a lot..
nice explaination..
Trupti
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classpath problem..
 
Similar Threads
Tomcat6 and Java Beans
JSP's dynamic content not getting displayed
document root for JSP
Tomcat - Where to put servlets
Small Tomcat configuration problem