Servlets & Servers

Q: The first thing is that i don't know how to do to create Servlets ... no more precisely, I
don't know how to compile it !!
It says that it doesn't find the packages (javax I think) ...
Plz, could someone tell me exactly what I should put on my environment variables (Class_path,
path, java_home etc...)
Answer: Well, I also started to learn about servlets. I recently downloaded JDK1.3.0
and Apache Tomcat 3.1.0 .

I set the following paths.
CLASSPATH = %TOMCAT_HOME%\webapps\examples\WEB-INF\classes\work;
(my working directory)
TOMCAT_HOME = C:\JDK\tomcat;
(or wherever you installed tomcat)
appended tomcat bin path to the path statement)
I also copied the servlet.jar found in the tomcat bin directory to both:
C:\Program Files\JavaSoft\JRE\1.3\lib\ext\
I hope this helps....
file:///C|/330_new/330_new/servlets_servers.htm (1 of 9) [2003-07-22 22:08:05]
Servlets & Servers
Gregory Pedder
Q: I do not see constructors in servlets? Am I missing something? How can servlet run
without constructor?
Answer: There is no need to use constructor in servlets for you because servlet are similar to
applet in a way it is running. The creation of servlet instance is a Server responsibility. You just
need to use init() method.
This method called first when servlet is loaded into servlet container...
AP (J.A.)
Q: Where can I find online books about servlets?
Answer: I have on the site free books link collection. Doing search for "servlet" I got 5 books.
Please check here:
Q: When can I use System.exit() in servlets?
Answer: Never! Depends on server you run on... Security exceptions on good server or full shut
down for simpler one.
Q: Why Servlets are better than CGI scripts?
Answer: Generally, every new client's request to CGI script starting a new process. With servlet
web server starts just a new thread that is much cheaper in terms of time for starting up,
memory and CPU consumption.
JVM uses just one process.
This answer is right for "standard" CGI. Now appear new modules for Perl that uses the same
approach as servlet does. Anyway, writing large programs on Java is much easier than on Perl,
at least for me.
Java has also very rich API.
Q: I was writing and testing some servlet program. Now I decided to remove it from our web
But it is still there causing confusion to another developers I am working with.
How can I unload my servlet explicitly?
Answer: It depends on server you use. Often you can unregister your servlet somehow.
file:///C|/330_new/330_new/servlets_servers.htm (2 of 9) [2003-07-22 22:08:05]
Servlets & Servers
Easier way to make your servlet empty (remove all code), compile it and copy class file to
Many servers reload automatically a new class if they see it was changed. You will still have
your servlet on the server but it will do nothing and nobody will complain...
Q: Any simple server I can download to run my java servlets...
Try Resin,
Try tomcat from - pure Java,
servlets 2.2, good stuff! Tomcat is good. It's not fast, but it's very easy to setup and good
solution for development. Since most heay-duty servers do implement SUN specification, one
can migrate application in no time.
Jigsaw is also good but doesn't support the Servlet 2.2 spec
Both of these are open source with BSD-like license.
There's also Jetty Artistic license, Servlet 2.2 spec compliant), GNU
Paperclips GPL, claims to be moving towards Servlet 2.3
spec compliance) and also
vqServer which is free but not open source.
Finally many of the commercial servers can be downloaded free for trial periods.
Simon Brooke
Q: Can someone point me to some tutorial/example/text book that covers the subject of
receiving on a server side a file upload from a browser sent as with content-type set to
Answer: There are free Java classes that will do this for you. Here's one I wrote:
If you still want to write your own, you can use mine as an example.
or here:
There you can download package which contains also examples, how to do it.
file:///C|/330_new/330_new/servlets_servers.htm (3 of 9) [2003-07-22 22:08:05]
Servlets & Servers
Walter Brameld
Q: How can I get access to Cookie set at the Client?
Answer: The following code should access a cookie on a client. It reads in all the cookies on
the machine. And checks there name for whichever one you are looking for.
Cookie[] cookies = request.getCookies();
for(int i=0; i < cookies.length; i++) {
Cookie thisCookie = cookies[i];
if (thisCookie.getName().equals("Cookiename")) {
// Do whatever you want with the cookie....
} else {
// cookie doesn't exist...
The Cookie class is in package javax.servlet.http.Cookie
Q: I'd like to provide to the showDocument() method of an applet the URL of a CGI
I'd like to provide to the showDocument() method of an applet the URL of a CGI program with
including a certain number of (URL-encoded) parameters to this URL (which is the same as
doing a GET HTTP request).
What is the maximum size I can give to this URL ?
Answer: If I remember exactly it something around 240 for many servers. Maybe less, but not
I read it last year in "Java Servlet Programming" from O'Reily.
Q: I am experimenting a Java server application. This program has worked well
It did start on the Red Hat Linux 6.0 server, but it does not open the socket, in other words, it
cannot communicate with the client applet on the Linux. On this Linux server I have installed
every components and all of them were running at the experiment time. Why does this server
application communicate with the client applet only on the Linux? Does anyone give me a
Answer: Take a look at your port number. If it is under 1024, it is a protected port
number and non-privileged users cannot touch it on Linux or any other
Q:Where Can I find a server to try my servlets?
I am creating a client/server application. I don't run my own server
file:///C|/330_new/330_new/servlets_servers.htm (4 of 9) [2003-07-22 22:08:05]
Servlets & Servers
and my ISP won't allow me to install and run applications from their
Does anyone know of anywhere (preferably FREE) that will allow
me to use server side Java? Any help is GREATLY appreciated.
Q: Hi, I am using servlets. I need to store an object NOT a string in a cookie. Is that
possible? The helpfile says BASE64 encoding is suggested for use with binary values. How
can I do that???
Answer: You could serialize the object into a ByteArrayOutputStream and then Base64 encode
the resulting byte []. Keep in mind the size limitations of a cookie and the overhead of
transporting it back and forth between the browser and the server.
Limitations are:
* at most 300 cookies
* at most 4096 bytes per cookie (as measured by the characters that comprise the cookie
non-terminal in the syntax description of the Set-Cookie2 header, and as received in the Set-
Cookie2 header)
* at most 20 cookies per unique host or domain name
For more details please refer to RFC 2965.
Q: Hi, I want to send a POST request, but I can't find such functionality in the servlet API,
how can I do this? Must I implement this with a socket connection to port 80?
Answer: A servlet can do anything a standalone Java application can do. It doesn't need
anything beyond what the package already provides. You can use an
httpURLConnection to POST to a server program like a servlet or CGI script:
// Create a string with the parms you want to post and convert it to a byte array. You may need
// pass the values through
// if they have embedded blanks or special characters
String parms = "a=10" + "&b=20" + "&c=30";
byte[] bytes = parms.getBytes();
// Create a URL pointing to the servlet or CGI script and open an HttpURLConnection on that
URL url = new URL(TARGET_URL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Indicate that you will be doing input and output, that the method is POST, and that the
// length is the length of the byte array
file:///C|/330_new/330_new/servlets_servers.htm (5 of 9) [2003-07-22 22:08:05]
Servlets & Servers
con.setRequestProperty("Content-length", String.valueOf(bytes.length));
// Write the parameters to the URL output stream
OutputStream out = con.getOutputStream();
// Read the response
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
while (true) {
String line = in.readLine();
if (line == null) break;
Phil Hanna
Author of Instant Java Servlets
Q: I am writing an application, using Java Servlets, which requires me to set and read
cookies. It works okay, but fails if I set the domain of the cookie to something other than the
current server.
I am writing an application, using Java Servlets, which requires me to set and read cookies.
Using the servlet API, and the javax.servlet.http.Cookie class.
I created a new cookie, and added it to the http response, using its addCookie() method. It
works okay, but fails if I use the setDomain method, on the newly created cookie, to set the
domain of the cookie to something other than the current server.
Answer: I suspect that is not a legal operation for any browser to
accept a cookie that has a domain inconsistent with the source
of the cookie.
by William Brogden
Q: I am working on weblogic server 5.1 with MsSQLSERVER7 i am able to load the driver
but it says unable to get socket connection
I am working on weblogic server 5.1 with MsSQLSERVER7 i am able to load the driver but it
file:///C|/330_new/330_new/servlets_servers.htm (6 of 9) [2003-07-22 22:08:05]
Servlets & Servers
says unable to get socket connection.It says connect to the MSSQLSERVER's host and port
How do I get these name and value.
Answer: The MS Sql Server's host is usually the name or ip of the server that run SQL Server,
if you know the IP (ping <nameoftheserver>), put the IP in it, it will be faster, for the Port
number, sincerely I don't
remember the standard port, but look into the SQL Server documentation and you will find it.
by Davide
Q: Whenever I compile my servlets it always says "can't find package javax.*" even though I
downloaded the JSDK. Where the JSDK files so it'll find that package?
Answer: There are no classes in the javax.* package. There are classes in javax.servlet.* and
javax.servlet.http.*, but neither are really related to javax.* -- importing javax.* won't affect
them. You should import the packages that you really want to use!
Q: I have a list of html links on a web page, and I want to be able to call a servlet based on
what the user clicked on...
I used:
<a href=/servlet/SomeServlet">
for each link, and I want the servlet to display information based on what the user clicked on. If
the user click on an applet link, I want the Servlet to print, "You just clicked on an
applet", etc.
My question is, how do I send information to a servlet, based on what html link the user clicked
on? I know i can use getParamaterValue() for getting information off of forms, but I'm not sure
how to do this with html tags.
Answer: Change the link to:
<A href="/servlet/SomeServlet?click=apple">link</A>
In the servlet, use request.getParameter("click") to retrieve the value.
Give each link a unique "click" value and that will tell you what was
B Russell
file:///C|/330_new/330_new/servlets_servers.htm (7 of 9) [2003-07-22 22:08:05]
Servlets & Servers
Q: I'm looking for a Java HTTP server framework that can be used and modified for a
project I'm going to be working on.
My boss asked me what the equivalent in Java was that IIS was in the Windows world or
Apache was in the UNIX. I've looked at Jigsaw, but am wondering if anyone out there knows of
other source would be great. Thanks in advance for any input.
Answer: There are other pure Java web servers, like the Apache PicoServer,
Jetty ( ???). Perhaps you could take one of those to write a http block for
the Apache Server Framework avalon ( )
Another tip is to search sourceforge for web servers.
Glen Shelly
Q: I am currently running Microsoft's IIS server. I have the Java Virtual machine installed on
this system. What else will I need to run my servlet. Do need the Apache web server instead?
Will IIS support Java servlet?
Answer: You will need a Servlet Engine to run your servlets.
You can use either Allaire's JRun ( or Tomcat
Both of them work with IIS.
Madhusudhanan Challur
Q: How can I avoid browser caching?
In an applet, I call the function showDocument to send a GET message to one of my servlets,
which sends me back a PDF file dynamically generated by this servlet.
My problem is : the browser (in this case IE) always sends me back a PDF file with the same
Answer 1: There are a few possibilities to avoid this problem.
A simple workaround is to add a random number URL variable with the request, such as "ranx"
with a value of the milliseconds since midnight. This will make the request unique and, hence,
avoid browser caching.
by Duane Morse
Answer 2: There are two other response setHeader attributes you can set:
response.setHeader("pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0 )
Just in case.
Answer 3: // for Answer 1 and 2 please go to yesterday's tip.
When you generate the PDF file, make sure you set the header to tell the browser that the file
will expire at a certain time. The browser should not cache the response past the given time.
The Java code looks something like
import java.text.SimpleDateFormat;
import java.util.Date;
SimpleDateFormat dateFormat
= new SimpleDateFormat("EEE, dd MMM yyyy - HH:mm:ss z");
response.setHeader("Expires", dateFormat.format(new Date()));
The format of the date is very particular.
Scott Gartner
Q: Wich free software can I use to debug Servlets?
Answer: What you need to do is use Jakarta-Tomcat to run your servlets. Then, bring in all the
tomcat jars into your "required libraries".
Then set up your "project run" to actually run the Tomcat class. If you run it in debug mode, you
should be able to put breakpoints in your servlets.
We did this at a project I was on with weblogic and EJBs, and Jbuilder 4 does it with tomcat, so
I'm assuming you can do this as well.