Monday, March 5, 2012

Servlet basic

SampleServlet extends HttpServlet{
init();
service(httprequest, httpresponse);

}

server engine call service() to handle request, return response.
when the servlet first loaded, init() is called once for initialization
after service() is done, servlet waits till next request

the server can decide to unload this servlet by calling destroy(), you want to
release all the shared resources that were created in init()

two packages
javax.servlet
javax.servlet.http

HttpServlet class methods
service()--serve all requests,defaut implementation is calling doGet, doPost,doDelete,doPut in response to http request
doHeader() defaut calling doGet(), chop off body, only return header

Thread safe Servlet
An application is thread safe if it always behaves predictablly regardless of the number of concurrent threads running in this
process space.
Synchronization in java is the process of locking a variable so that it can be read and modified by only a single thread at a time.
The synchronized keyword eliminates race conditions by locking variables for a specific duration
synchronized(this){
...
..
}

it is possible to synchronize the entire service() method rather than blocks of code, in this way, only on thread at a time
can execute the method. the more code is synchronized, the greater the performance penalty. To aoid this penalty,minimie the amount
of code.

a new servlet object is not instantiated for each new client request, Rather, a single servlet usually services all requests.
This behavior explains how instance variables can often be shared
Because every call to service(), doget(), doPost() is executed in its own thread, variables local to these methods are not shared between
requests and are automatically thread safe.

public class AServlet extends HttpServlet
{
//instance variable
String UserName; // not thread safe

}

public class BServlet extends HttpServlet
{
//instance variable
String userName; // not thread safe

public void service(){
String userName; //thread safe
}

or use SingleThreadModel interface

public class CServlet extends HttpServlet implements SingleThreadModel
{
//instance variable
String userName; // thread safe

}
If servlet implemnts the SinglethreadModel interface, the server guarantees that no more than on thread can execute the service,
doGet, doPost method at a time for a particular servlet instance. this gurantees thread safe.

Http redirects
An Http redirect is a set of instructions included in the header of an Http response that instruct the browser
to issue a new request to a new URL.

An HTTP redirect response uses an HTTP status code of either '301 Moved Permanently" or "302 Moved Temporarily" accompanied
by a Location header containing a valid URL.the browser receives an Http response with status code 301,302,it will
immediately request the resource at the URL designated by the Location Header
sample redirect response

HTTP/1.1 301 Moved Permanently
..
Location:http://www..../....html

sending a redirect with the servlet API
two ways:
service(HttpServletRequest request,HttpServletResponse response){
..
reponse.status(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", someurl);
}
or
response.setRedirect(url); //this way only set 302, url has to be full qualified url

No comments:

Post a Comment