Alex Strasheim wrote:
> Here's a very vague question: What core skills should people who want
> to be able to architect scalable web apps using JSP have?
>
> I'm fuzzy on how many instances of servlets there might be floating
> around, how you cantrol or monitor that, how application servers
> spread them around across several machines, etc. I have no idea how
> to predict what would happen to a JSP site under even a moderate load,
> or how to describe what happens inside the JVM.
>
Some of the rules for what happens are defined in the Servlet API Specification and
the JavaServerPages Specification for the versions of the APIs supported by your
server. See <http://java.sun.com/products/servlet> and
<http://java.sun.com/products/jsp> for links to these documents on the download
pages.
For example, the number of instances of a servlet or JSP page will be one per
servlet definition (one per JVM if your container supports distributed
environments), unless you declare that you implement SingleThreadModel -- in which
case the server may or may not implement a pool of them.
A way to find out more would be to examine the source code for one of the open
source servlet containers (such as Tomcat at <http://jakarta.apache.org>. This
won't tell you how *every* server is implemented (they can do whatever they want as
long as they conform to the spec requirements), but this is a good starting point
for understanding things.
>
> What you have to know about writing thread safe servlets to make your
> JSP application safe and scalable?
>
The principles for writing multi-thread aware applications haven't changed much in
the last twenty years. The key principles to keep in mind when dealing with
servlets and JSP pages:
* Instance variables (as their name implies) are shared between
the threads that are operating in a single instance. Local
variables are not shared -- there is a stack per thread that
contains them.
* Plan on your user session, and the beans you store in it,
to be accessed from multiple threads at the same time. Two
examples: a framed presentation where the browser issues
multiple simultaneous requests, or a user that starts a long
request, presses STOP, and starts another request in the
same session.
* In general, simultaneous read access to things does not
cause problems, so you don't need to worry about locking
or synchronization.
* If you are writing something, you should lock if the operation
is not "atomic" -- always think like a paranoid and ask yourself
what is the worst possible sequence of accesses from the
multiple threads that are running?"
* Always try to lock the smallest amount of stuff you can,
for the smallest amount of time. For example, in the
following logic:
public class Foo {
private Object var = ...; // An object
public synchronized void setVar1() {
... modify something about var ...
}
public void setVar2() {
synchronized (var) {
... modify something about var ...
}
}
}
you can call either setVar1() or setVar2() to modify the internal
state of "var". But the first one locks all synchronized methods
of the Foo class, while the second one only locks the setVar2()
method.
* Often, standard Java library classes will deal with locking for you,
but you need to understand the classes you are using. For example,
Hashtable and Vector do internal locking to avoid corrupting their
own state, so there is no problem adding elements to them from
multiple threads at the same time. But the Collections classes
introduced in Java2 (HashMap, ArrayList, etc.) are *not*
thread safe and you need to take care of it yourself if necessary.
* Stay away from SingleThreadModel, and write your servlet classes
to be thread safe in the first place. STM can mislead you into thinking
it has solved all your multithread access problems, but it hasn't -- it
only deals with the servlet instance variables.
>
> Is this stuff I'll find in servlets books? Websphere (or Resin) docs?
> EJB books?
>
Many servlet books talk about the issues of programming for a multithreaded world.
There are also general computer science texts that talk about the issues, which are
the same as those faced in GUI-based applications, multi-processor servers, and
other multi-thread environments where the fundamental issues are exactly the same.
Craig McClanahan
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets