aspose file tools*
The moose likes Servlets and the fly likes How can I get my HelloWorld Servlet to Run? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How can I get my HelloWorld Servlet to Run?" Watch "How can I get my HelloWorld Servlet to Run?" New topic
Author

How can I get my HelloWorld Servlet to Run?

Nina Savannah
Ranch Hand

Joined: Oct 15, 2008
Posts: 35
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
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
You need to add the servlet-api.jar file to your classpath. It's in the common/lib directory in the Tomcat installation.


Ping & DNS - my free Android networking tools app
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

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.


Swastik
Nina Savannah
Ranch Hand

Joined: Oct 15, 2008
Posts: 35
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

Joined: Jan 08, 2009
Posts: 1477
    
    6

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

Joined: Oct 15, 2008
Posts: 35
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

Joined: Jan 08, 2009
Posts: 1477
    
    6

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

Joined: Oct 15, 2008
Posts: 35
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

Joined: Jan 08, 2009
Posts: 1477
    
    6

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

Joined: Oct 15, 2008
Posts: 35
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

Joined: Oct 15, 2008
Posts: 35

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

Joined: Jan 08, 2009
Posts: 1477
    
    6

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

Joined: Oct 15, 2008
Posts: 35
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

Joined: Jan 08, 2009
Posts: 1477
    
    6

leave that delete part, but did you try the way I suggested?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Tomcat directory structure
Nina Savannah
Ranch Hand

Joined: Oct 15, 2008
Posts: 35
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

Joined: Jan 08, 2009
Posts: 1477
    
    6

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

Joined: Oct 13, 2005
Posts: 38765
    
  23
I think this thread is becoming too difficult for beginners'.

Moving.
Nina Savannah
Ranch Hand

Joined: Oct 15, 2008
Posts: 35
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
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
This is incorrect:

Try this instead:
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I get my HelloWorld Servlet to Run?