I apology for the ugly formatting and the abrupt end to my previous post (first
to this group*). I mistakenly pressed ctrl+s (send) instead of ctrl+v (paste) which
caused my mail to be sent before I had finished it. Here is the full version:
* Hi all!! Sorry to hear you are thinking of unsubbing Nic!
Martin Lew wrote:
> Ok, here the code for the servlet that generates HTML:
[snip]
I noticed that you have a quite common misconception about what servlets are.
A servlet should generally not have any member variables, only local variables
that are passed as arguments to other methods if necessary. (I'm talking about
pageHtml, selectedLang and selectedType in your example.) This is because
servlets are instantiated ONCE by the servlet container and called on each
request from separate threads. It might help to think of a servlet's member
variables as static variables.
Your code has sharing violations in that if two (or more) request are made at the
same time, they will both enter doPost and write to THE SAME pageHtml
instance. There is no telling of what the output would be, presumably some
mixture of the output from both threads...
<HTML><HTML><HEAD><HEAD>...</HTML>...</BODY></HTML>
... or even:
<H<HTMTLM><>HLHTTM...
Needless to say, if you are confused by this you'd have to do some reading on
threading and the servlet life cycle.
Another thing that might have to do with the problem you're having, is that the
servlet's init method will only be called once (when the servlet is instantiated),
so even if no two requests are ever made concurrently (at the same time) you
never reset pageHtml. This will have the effect that all output is accumulated.
After 4 requests pageHtml will contain:
<HTML><HEAD>...</BODY></HTML>
<HTML><HEAD>...</BODY></HTML>
<HTML><HEAD>...</BODY></HTML>
<HTML><HEAD>...</BODY></HTML>
But don't feel bad. There is a simple solution to all these problems, and that is to
only use local variables. Then every thread calling doPost will create their own
instances of the mentioned variables and the problems will go away. Try this:
void doPost(/*...*/)
{
StringBuffer pageHtml = new StringBuffer();
String selectedLang = request.getParameter("lang");
String selectedType = request.getParameter("type");
response.setContentType(CONTENT_TYPE);
// change constructPage accordingly
constructPage(pageHtml, selectedLang, selectedType);
PrintWriter out = response.getWriter();
out.println(pageHtml.toString());
out.flush();
out.close();
}
...johahn
PS. I got 3 "out of office" messages to my previous message. May these
people be haunted and drawn to court by the evil lawyers of Kodak. DS.
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html