Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I get my HelloWorld Servlet to Run?

 
Nina Savannah
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get this servlet to work but i'm getting errors.
Here is what I did:-

I added the following code to web.xml to invoke my servlet in C:\Tomcat5027\conf



I saved my HelloWorld.java to

C:\Tomcat5027\conf\classes\test



To compile the java file I went to Command Prompt and typed


C:\j2sdk1.4.2>cd bin

then

C:\j2sdk1.4.2\bin>javac C:\Tomcat5027\webapps\ROOT\publications\test\HelloWorld.java



Here are the errors I got. I'd like to know, Where am I going wrong?:-

ERRORS
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add the servlet-api.jar file to your classpath. It's in the common/lib directory in the Tomcat installation.
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a classpath setup issue, and the next point is you can save the file any where, only the generated class file should be in proper place.
I assume you have a setup of windows xp might be same in vista not very sure with tomcat server.

Step1: set the classpath
------------------------

My Computer->properties->Advanced->Environment variable

Now under user variable if there is any variable called classpath, click edit at end of the variable value put a ; paste the path of servlet-api.jar which is under lib of tomcat home for e.g.

;C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.14\lib\servlet-api.jar;.

If dont have any classpath variable , create new , variable name classpath value as follows
%classpath%;C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.14\lib\servlet-api.jar;.
Apply the settings

The mentioned path and version mentioned above may change based on your installation

Step2
-----
Compile your code.

Step3
------
Create the directory structure under tomcat. under webapps folder of tomcat home create a folder(i.e. you context root folder which will contain your web pages , servlets etc.). Under this folder create another folder called WEB-INF and under WEB-INF create another folder called classes.

Step4
------
Now copy your generated package or class file , whatever you have to this classes folder, and put the web.xml file under WEB-INF folder.

Step5
-----
Start tomcat server and execute your servlet like this
http://localhost:8080/yourcontextrootfoldername/yourservleturlpatternmentionedinweb.xmlfile

8080 is the default port of tomcat, if not reconfigured.
Most importantly if you have any ide like netbeans or myeclipse its best to work there, because it does everything for you.
 
Nina Savannah
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. I have followed all the instruction. Now I am not sure how exactly the file is compiled.
I'm doing everything manually. Is what I have below what compiling is?


C:\j2sdk1.4.2\bin> javac C:\Tomcat5027\webapps\ROOT\publications\test\HelloWorld.java
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You compile it in the way you compile a simple java code, the only thing what you need is to setup the classpath properly so that it can locate the required packages and classes.
 
Nina Savannah
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I've done

C:\j2sdk1.4.2\bin>javac C:\Tomcat5027\webapps\ROOT\publications\test\HelloWorld.java

and here are the errors I'm still getting.:-

ERRORS
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still have the problem of classpath setup, what server are you using? If tomcat can you tell me the exact path of your tomcat installation.
 
Nina Savannah
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is where Tomcat 5.0.27 is installed
C:\Tomcat5027

Java is installed in C:\j2sdk1.4.2
(There's another Java in C:\Program Files\Java\j2re1.4.2 though I am not using this one.)

Here is my classpath
.;C:\j2sdk1.4.2\bin;C:\Program Files\Java\j2re1.4.2\lib\ext\QTJava.zip;C:\Tomcat5027\common\lib\servlet-api.jar;

HelloWorld.java is saved in C:\Tomcat5027\webapps\ROOT\WEB-INF\classes\test
web.xml is saved in C:\Tomcat5027\conf, I copied it into C:\Tomcat5027\webapps\ROOT\WEB-INF\


Compiled like this again and got these errors:-

C:\Tomcat5027\webapps\ROOT\WEB-INF\classes\test>javac HelloWorld.java

 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any file called servlet-api.jar under this folder C:\Tomcat5027\common\lib?

if yes try the following once

C:\Tomcat5027\webapps\ROOT\WEB-INF\classes\test> set classpath=%classpath%;C:\Tomcat5027\common\lib\servlet-api.jar;.;

C:\Tomcat5027\webapps\ROOT\WEB-INF\classes\test> Now compile your code.
 
Nina Savannah
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey you are a star!

This part has worked, I now have HelloWorld.class

Let me work on the next part and see how it goes!
 
Nina Savannah
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm trying to run my website on my computer and i'm getting the error

The requested resource (/publications/) is not available.
I tried re-starting Tomcat but it didnt correct the error.

I inserted the code


in web.xml

I have this web.xml in two locations:
C:\Tomcat5027\conf
&
C:\Tomcat5027\webapps\ROOT\WEB-INF\


Also when I correct this error, what do I type in the url space to test my servlet?
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
under webapps create a folder called publications. under this folder create a folder called WEB-INF and copy your web.xml file here. Now under WEB-INF folder create a folder called classes and copy your package i.e test to this classes folder. Now restart tomcat and type the following url in browser

http://localhost:8080/publications/hello

Remove the things from ROOT
 
Nina Savannah
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My website is located in C:\Tomcat5027\webapps\ROOT\publications so does this mean that I have to copy WEB-INF folder to there instead?

I already have WEB-INF in C:\Tomcat5027\webapps\ROOT\WEB-INF which refuses to get deleted, error message says "Acess Denied"

The error The requested resource (/publications/) is not available. still persists
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leave that delete part, but did you try the way I suggested?
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomcat directory structure
 
Nina Savannah
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I did what you suggested and I have now deleted web.xml in C:\Tomcat5027\webapps\ROOT\WEB-INF
so I'm left with web.xml in C:\Tomcat5027\conf
but my HelloWorld.class & HelloWorld.java is in C:\Tomcat5027\webapps\ROOT\WEB-INF\classes\test

Then I changed this




to that



And now my website is available
http://localhost:8080/publications

However, the url below
http://localhost:8080/publications/hello


says
The requested resource (/publications/hello) is not available.
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are probably not getting me properly are probably I am not being able to explain you properly. In tomcat rather in any web application we need to to maintain a particular directory structure. As far as tomcat is concerned we need to follow the following directory structure. Forget about that ROOT folder. The publication folder which you are saying contains your web site contents should be under webapps not under ROOT. now the publication folder should contain WEB-INF which will contain the web.xml file(web deployment desciptor). Under the same folder i.e. WEB-INF you need another folder called classes which will contain your package/or class files. So in your case test is the package which contains the servlet class file.
After making the changes please do restart your server.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this thread is becoming too difficult for beginners'.

Moving.
 
Nina Savannah
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Swastik Dey for helping me come this far.

The system I am working on is an already operational system and is saved in :-

C:\Tomcat5027\webapps\ROOT\publications

This is why I am requesting that we use that.

So right now I have

web.xml in C:\Tomcat5027\webapps\ROOT\publications\WEB-INF\
HelloWorld.class in C:\Tomcat5027\webapps\ROOT\publications\WEB-INF\classes\test
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is incorrect:

Try this instead:
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nina Savannah wrote:I'm trying to get this servlet to work but i'm getting errors.
Here is what I did:-

I added the following code to web.xml to invoke my servlet in C:\Tomcat5027\conf





Once you get your servlet to compile,
You should create a deployment descriptor specifically for your application and place it under your application's WEB-INF directory.
The global deployment descriptor (the one in tomcat/conf/) is for configuring behavior common to all applications running under Tomcat.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic