Datasheet

Patrick c01.tex V3 - 09/18/2009 12:15pm Page 5
Chapter 1: Building Web Applications in WebLogic
use. The specification encourages developers to protect critical sections of servlet code using syn-
chronization logic. Of course, using synchronization logic around non-thread-safe code comes
with a price — it invariably creates bottlenecks and latency in high volume systems as threads
wait for their turn to execute the protected code. If the code within the critical section takes too
long to execute, overall performance and scalability of your system will suffer. Avoid using the
SingleThreadModel
interface in your applications. Design your servlets be thread-safe and mini-
mize their use of synchronization blocks.
Best Practice
Avoid using single-threaded servlets. Design your servlets be thread-safe and
minimize their use of synchronization blocks to avoid potential performance
issues.
Servlets May Access Request Data
The
HttpServletRequest
parameter passed in to the
service()
or
doXXX()
method contains a
wealth of information available to the servlet during the processing of the request. Useful data in
the
HttpServletRequest
is summarized in Table 1-1.
This is not an exhaustive list of the methods available on the
HttpServletRequest
class or its super-
class,
ServletRequest
. Refer to the servlet documentation at Link 1-1 in the book’s online Appendix
at
http://www.wrox.com
or a good reference book on servlets for a complete list including parame-
ter types, return types, and other details.
Servlets Use Session Tracking
A servlet is a request/response mechanism that treats each incoming request as an independent
processing event with no relationship to past or future requests. In other words, the processing
is stateless. The HTTP protocol is also a stateless protocol: Each request from the web browser is
independent of previous or subsequent requests. Linking current requests to previous requests
from the same client requires a mechanism for preserving context or state information from request
to request. A number of HTML-based techniques exist for preserving context or state information:
Cookies may be set in responses and passed back to the server on subsequent requests.
URL-rewriting may be used to encode small amounts of context information on every
hyperlink on the generated page.
Hidden form fields containing context information may be included in forms.
These techniques all have limitations, and none provides the robust data types and flexibility needed
to implement true state management. Fortunately, the session tracking capability defined in the Java
EE servlet model provides an excellent solution.
Session tracking provides a flexible hashtable-like structure called an
HttpSession
that can be used
to store any serializable Java object and make it available in subsequent requests. To identify the
specific client making the request and look up its session information, session tracking uses a cookie
or URL-encoded session ID passed to the server on subsequent requests. In WebLogic Server, this
5