GeeCON Prague 2014*
The moose likes Servlets and the fly likes How to test servlets? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How to test servlets?" Watch "How to test servlets?" New topic
Author

How to test servlets?

Ghazala Islam
Ranch Hand

Joined: Nov 22, 2000
Posts: 73
I have downloaded tomcat 3.1 .Tomcat's default page is displaying from browser.I have gone through some documents in its docs
directory to how to install and run tomcat but it is very
lenghty and technical for a new user.My question is again
the same that which else software is required to test servlet
or no other is required?Second question is where can I store
and how can I compile servlets?
Lisa Strite
Greenhorn

Joined: Dec 19, 2000
Posts: 4
I have not used TomCat, I am using WebSphere but I believe it is similar. You can compile your servlets using whatever jdk the tomcat server runs (probably 1.2.1 or 1.2.2). You will probably then copy them into the 'servlet' directory. Then you will start the Tomcat server. You will test your servlets by entering in a web browser "http://localhost/servlet/MyServletName".
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
Just to supplement Lisa's comments..
If your servlet does not require any user input, you can call it directly. However, most servlets are built as part of a larger application so you will typically have to provide HTML pages with <form> elements to be filled in by the user, etc.
The Java API for servlets lays out exactly where class files have to be kept in relation to the other elements of a web application. Download that API from java.sun.com - It does get complicated, but you can follow the examples that Tomcat provides.
You need to understand how the web.xml file is used to control the way Tomcat refers to servlets and to provide initialization parameters.
Bill

------------------
author of:
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
When you say "software to test servlets" do you mean just the software to run them so you can test them manually, or are you looking for automated test tools?
For manual tests you just need a reasonable servlet container (Tomcat, Resin, JRun, Weblogic, etc.) For automatic tests you could try HTTPUnit, but it's a notoriously difficult area.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ghazala Islam
Ranch Hand

Joined: Nov 22, 2000
Posts: 73
Ok,as far as I understand,to compile a servelet I
have to go in 'tomcat/src/javax/servlet' directory and
there I can save and complile my servelets?
Second question is, as for compiling java
programs we give at the prompt 'javac myprog.java'
so what is the command to compile servelet?
Don't forget to answere my first question!!!
Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
I'm not using Tomcat, but I'm trying to learn servlets as well. The way I've been able to test them is to compile them exactly as you would a normal .java file. That is at the command prompt, type:
javac myfile.java
or if you're using some kind of software, like I'm using Jbuilder, I just compile it from the menu.
But once that's done, you move the .class file that was created after you compiled the .java file to the /servlet directory. Then from a web browser you type http://localhost/servlet/myfile
(replace localhost with the url to your web server where you saved your file followed by the path to where you saved your .class file).
This worked for me up till now, so I hope it works for you too!
Annette
eric moon
Ranch Hand

Joined: Nov 26, 2000
Posts: 133
The class files for the servlets that come with tomcat are in :
....tomcat\webapps\examples\WEB-INF\classes
The way I tested my servlet was to compile it and put it in that directory, go to the servlet example page in tomcat, and rename the reference of one of their examples to point to my servlet instead.
If your servlet creates a form, that's not the address to put in the form. I looked at the URL displayed in my browser for their examples:
http://localhost:8080/examples/servlets/
and that worked.
I assume this path is stored in the web.xml file, but I haven't gotten into that yet.
HTH
[This message has been edited by eric moon (edited December 20, 2000).]


<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
Ghazala Islam
Ranch Hand

Joined: Nov 22, 2000
Posts: 73
As per your advices,I save and compile
my servlet in jdk1.2/bin directory(where I save
and compile my other java files) and compile it
with command (javac myservlet.java) but there are errors whereever
I have use word 'httpservlet' in my example that 'cannot resolve symbol'.
Where I'm wrong now?
Rajesh Hegde
Ranch Hand

Joined: Sep 15, 2000
Posts: 112
May be u have forgotten to import javax.servlet.http.* .Just check it up
Ghazala Islam
Ranch Hand

Joined: Nov 22, 2000
Posts: 73
These are the imported files:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
then why its giving error,'can't resolve symbol'.
Steps I followed to save and compile servlet are right?which
I wrote in my previous reply.
 
GeeCON Prague 2014
 
subject: How to test servlets?