Container�s importance is emphasized in this chapter :
1)The following things the container does: (First container loads the Servlet class [when the container starts up , it will search for class files and will load them in m/m]) a)It instantiates the servlet when request comes in (if this is the first time this servlet is being requested) i.e by calling its no arg constructer of our servlet class and then it calls the init() method of the servlet (init() method is called once in the Servlet Life cycle) b) It creates a new thread for this servlet (if this servlet is being requested second time i.e. its instance already exists in m/m then above (a) step is missed by the container) c) It makes sure that calls to doGet() or doPost() occurs, it does this by calling the service method. d) It provides HTTP request and response objects as gifts to servlet. e) In nutshell, it manages the Life cycle of the servlet
2) We have HTTP methods and corresponding to them we have methods in HttpServlet.
3)Servlet�s Life Cycle moments include 5 stages as below : a)Servlet class loading b)Servlet class instantiation c)Calling the inIt() method d)Calling the service() method e)Callng the destroy() method
4)Q- What all things a file and directory structure of a web application can contain? Ans: Below things: a)Static content (i.e HTML pages) b)JSP pages c)Servlet classes e)Tag libraries f)Jar files g)Java class files h)deployment descriptor
5)I must understand the purpose and semantic of deployment descriptor elements , they are asked in many exam questions.
6) What is a servlet in technical terms? It is a Java class extending HttpServlet class to have characteristics of a servlet It implements doGet() or doPost() methods as servlet�s service method gives calls to dem. Important thing is that , it doesn�t have main() method It is under the control of other Java application whose name is Container J
7)Don�t get confused ever : Container(is Tomcat here) and Web Server (is Apache here)
8)What happens on server side when a request for a servlet comes from client ? Ans : 1) Client sends the request for a servlet to web server 2)Web server hands over the request to container in which servlet is deployed. 3)Then container does the things I mentioned in first bullet. 4)In the end reponse is passed from servlet to Container to Web server to client back.
9)Imagine what wud have been the case if there wud not have been servlets and container : Then we had to write all server side code ourselves i.e all socket programming and all��..very tough��..so we have been provided with servlets and container who manage all this�� that is we have been provided with Servlet APIs to do server side programming.
10)Why the web-server cant talk himself to servlets , why he cant manage all servlets , why he cant manage all servlet life cycle events? Is talking thru container is not an over-head ? Ans: Because container takes the all below responsibilities : 1)Takes the headache of communication among servlets and web-server. 2)Does the Life Cycle management of servlets. 3)provides the multithreading support for servlets requests : whenever it receives a request for servlet , it creates a new thread for that servlet and when servlet service() method completes , thread dies off. 4)It provides the deployment descriptor where you can declare and configure all security without hardcoding it in your servlet code 5) Container only converts the all JSPs into servlets , that means it takes care of JSPs also.
11) How does container handles all requests from clients from beginning only? Ans: It is as below: 1)User clicks on the link that has a URL to servlet desired. 2)HTTP GET/POST request goes from client to web-server 3)Web server hands over the request to container 4)Container before looking for the desired servlet presence , creates the request and response objects as from client request it knows that request is for servlet and every servlet service method requires these two objects. 5)Now container start searching for desired servlet based on the URL , it creates a instance (or it creates a new thread for already existing instance depends..) and hands over the request and response objects to servlet thread. 6)Now Container calls the servlet service method passing them the request and response objects. 7)Now service method calls doGet() or doPost() methods of the servlet depending upon the type of HTTP request from client that is whether it is GET or POST request. 8)The doGet() method generates the dynamic page and stuff it into response object. (Container already has reference to above response object )
9)The doGet() method completes , thread dies and now container converts the response object into HTTP response and sends it back to client and then deletes the unneeded request and response objects.
Q-what are my observations while writing a servlet? Ans:These are below : 1)It is must to import three packages as below : a)import javax.servlet.*; b)import javax.servlet.http.*; c)import java.io.*; 2)It should extend HttpServlet class to become a servlet 3)We have to implement doGet() or doPost() methods in our servlet class. 4)These doGet() or doPost() methods take two arguments : HttpServletRequest and HttpServletResponse objects 5)These methods may throw IOException so it is must to declare that it throws exception 6)First line in doGet() or doPost() method must(generally) be response.setContentType(�text/html�); to specify the content to client which is sent by server back to make it ready to accept J 7)Next line must(generally) must be of borrowing the writer object from response object to write the dynamic response that is : PrintWriter out = reponse.getWriter() ; 8)Now start writing the dynamic response (which you want to send to client as a result of servlet) in out.println(). (It�s the HTML code only but enclosed in out.println() )
Q-9- Does user types exact path to Servlet class on server and servlet class name in URL? Ans-NO (Because of security and for ease of doing any basic code change , it is encapsulated) We achieve all this taking advantage of deployment descriptor .
10) See deployment descriptor is like index (in a book ) for container : How? :-> It searches for servlet name being requested in this file , it must find it here , it finds all the configuration this application requires , what all configuration these servlets require from here and it is used for so many purposes also.
11) Every URL is mapped to Servlet in deployment descriptor and every user request must map to a servlet .
12)Q-how many names can be there for a single servlet ? Ans : Three. a)One is in URL (which client sees) (PUBLIC) b)Second is in Deployment descriptor (which deployer decides and sees) (HIDDEN) c)Third one is in also DD , which is actual name of servlet class (which web component developer decides) (HIDDEN)
13)Every servlet on server has path also like where it is present on the server like in which package it is present on server.
14)See URL can be directly written in address bar or it can be written in HTML form in form of link where user will click.
15)Whats the use of doing mappings (extra overhead) ? Ans : To achieve security and flexibility.
16)For every Servlet mapping must be done to URLs in DD.
17)What is deloyment? Deployment is creating first the DD with all mappings and restarting the server J , to bring DD into container�s knowledge.
18)How many XML elements are used to do mapping in DD?Tell their name also. Ans: Two : a)Public URL -> Deployement name b)Deployment name -> Actual Servlet class name
The names are repectively : 1) Servlet mapping 2) Servlet
And Off-course it must be all under <web-app> tag.
Q-19)What is beautiful about deployment descriptor ? Ans : It gives you a way to declaratively modify your application without changing the source code.
Q-20)Write what all benefits are provided by DD Ans : 1)Source code remains untouched. 2)Application can adapt to any of the resources (DBs) without recompiling any source code. 3) Security factory as earlier said. 4) Advantage to non-programmers.
21) Remember a web-application has lots of pages , for each of those pages we have to write HTML pages OR servlets OR JSPs etc. Every servlet has its own purpose , its own biz. logic and finally it writes dynamic html to writer object which is stuffed into response object and sent back to client
Our servlet code always contains two basic things : HTML response + Biz. logic , But having HTML into it is problematic , boring and error prone so JSPs came into picture.
So servlet just now can contain only biz. logic , HTML part is shifted to JSPs , we call as � Presentation is now in JSPs�
Please note that here servlets after completing biz logic , forwarding the response to JSP , note that there is direct communication , we don�t want it, we don�t like it , because this biz logic will work with this view only (as it has been coded this way) , what if or client wants different kind of view ? then there will be a problem , we have to change our servlet code , this is not reusable.
Still it is not good web-application , here biz logic is imprisoned in the servlet killing its reusability J�..we want our application to be robust��..
So this biz logic (methods that operate on data) should be kicked out of the servlets and should be put into plain Java Class file which we call Model.
Now our servlet has designation of Controller which will send the request data to model , model will perform biz. logic , sends the result back to container , container put the result at a some global place where view can access it and present it to client.
22)J2EE spec includes Servlets 2.4 specs and JSP 2.0 spec for web-container. J2EE 1.4 spec includes above plus EJB 2.1 spec for EJB container .
23)A J2EE application server includes both a web-container and an EJB container .
24)Tomcat and resin are two standalone web-containers which are configured to HTTP web server like Apache
25)Q- Name somecommon J2EE servers Ans :BEA�s weblogic IBM�s websphere JBoss
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad: