Servlet Life Cycle:
init()
service()
destroy()
§ container load the servlet class.
§ Container Instantiate servlet. It runs the constructor. The constructor is default one given by compiler.
§ Container calls the init() method after Servlet Instance is created. It will be called only once in servlet lifecycle, must complete before container call the service().
§ Then container call the service() method.
§ Container calls to give the servlet a chance to clean up before the servlet is killed(garbage collection). It is called only once as like init().
init() method we can override . If we have intialization code like getting database connection or registering object, tghen we can override init() method.
service() method we should not override. We have to override doGet() or doPost() and service() will call either doGet() or doPost() depends upon the request.
doGet()/doPost() always we have to override it.
By the above figure, one thread per request will be created.For every incoming request means a new thread/stack.
And for single JVM there will be only one instance will be created for a servlet.
First container finds the servlet class. Then it loads the class. Loading the class depends, it can be loaded while container startup or first client use.
When Object become Servlet:
After the constructor call the init() method called. In that time Object become Servlet. Because constructor is makes an Object, not Servlet. When Object become Servlet it get all unique privileges like ability to use its ServletContext to get information about the Container.
ServletConfig Object:
§ One ServletConfig Object per Servlet
§ Use it to pass deploy-time information to servlet(like DB name, lookup for EJB)
§ Use it to access the ServletContext
§ Parameters are configured in Deplyoment Descriptor(DD).
ServletContext:
§ One ServletContext per web app.
§ Use it to access web app parameters(also configured in DD).
§ Can be useful to store and share the data across web applicaion
§ Use it to get server info, including name and version of container.
Request:
Who implements Interface of HttpServletRequest and HttpServletResponse:
Container implements. These are not part of API, there are left to vendor specific.
Using Request object we can use many methods to get data;s.
request.getParameter(“string”); è return string.
request.getParameterNames(“string”); è return array of string
request.getHeader(“User-Agent”); è return string
request.getgCookies(); è return array of Cookies[].
request.getSession(); è return HttpSession.
request.getInputStream(); è return InputStream.
request.getInputStream:
Used to get the raw bytes of everything that comes in the request, both header information and body.
getHeader():
String forwards = request.getHeader(“max-forwards”);
Int forwardNum = Integer.parseInt(forwards);
getIntHeader():
int forwardNum = request.getIntHeader(“max-forwards”);
getRemotePort():
gives the client port number. For server, client is the remote.
getServerPort():
The port where the request are sent to server.
getLocalPort():
after the request sent to server, the server turns around and finds different local port for each thread.
Response:
Response is just send data back to the client. Important methods of response are
a. setContentType()
b. getWriter()
c. getOutputStream()
some of the other methods are
a. sendError();
b. addCookies()
c. addHeader()
d. encodeURL();
response.setContentType(“application/jar”); is used to send the jar.
ServletContext ct = getServletContext();
InputStream is = ct.getResourceAsStream(“/bookcode.jar”);
Int read =0;
Byte[] bytes = new byte[1024];
OutputStream os = response.getOutputStream();
While((read = isread((bytes))!=-1){
os.write(bytes,0,read);
}
os.flush();
os.close();
bookcodejar will be search in webapps/project/bookcode.jar. It will be placed in the same place where WEB-INF is placed.
PrintWriter:
PrintWriter writer = response.getWriter();
Writer.println(“ some txt”);
It is for character steam/data.
OutputStream:
ServletOutputStream out = response.getOutputStream();
Out.write(ByteArray);
It is for bytes.
SetHeader(“bar”,”bar”) è overwite the existing value
AddHeader(“add”,”add”) è adds an additional value
SendRedirect():
Response.sendRedirect(www.google.com); it takes only string not the URL object. We cant call sendRedirect after the response is committed.
Using relative URL in sendRedirect():
Client originally typed in:
http://www.wicked.com/myApp/cool/bar.do
sendRedirect:
sendRedirect(“foo/stuff.html”);
note this is without “/” forward start in the starting. So container builds the URL
http://www.wicked.com/myApp/cool/foo/stuff.html
Client originally typed in:
http://www.wicked.com/myApp/cool/bar.do
sendRedirect:
sendRedirect(“/foo/stuff.html”);
note this is with “/” forward start in the starting. So container builds the URL
http://www.wicked.com/myApp/foo/stuff.html
forward slash at the beginning means “relative to the root of this web app”
Difference between redirect and request dispatch:
a. redirect makes the client do the work, while request dispatch makes something else on the server do the work.
b. In request dispatch, the browser location bar didn’t change. So user does not know that the jsp generated the response. But in redirect the user see new URL in the browser.
