S E R V L E T S

1,132
-1

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,132
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

S E R V L E T S

  1. 1. SERVLETS
  2. 2. Servlets <ul><li>A servlet is a sever side software component, written in java, that dynamically extends the functionality of a server Servlet executes on the Java enabled Server. A servlet’s work is done “behind the scenes” on the server and the results of the servkets processing are returned to the client (usually in the form of HTML). Servlets are the Java classes that conform to a specific interface that can be invoked from the server The functionality provided by the servlet is restricted to the server. The servlet API is a specification developed by Sun that defines the classes and interfaces used to create a servlets. Servlets provide a framework for creating applications that implements the request/response model. . </li></ul><ul><li>All servlets that perform some useful function have two things common. First, they all extends one or two servlet classes – GenericServlet or HttpServlet. Extending these ckasses provide framework for creating servlet as well as default functionality. Second , all servlet override atleast one method wherein custom functionality is implemented. The method that automatically called by the server in response to the client request is called servoce() method.. There are two other method implemented by most servlet – init() and destroy(). The init() method called single time when the servlet fist loaded. It is similar to the class constructor in that it provides method wherein initialisation of code guranteed to be done. The destroy method is executed </li></ul><ul><li>When the servlet is unloaded. It is used to free the resources held by the servlet. </li></ul>Definitions
  3. 3. <ul><li>Skelton of the servlet: </li></ul><ul><li>public class SkeltonServlet extends HttpServlet </li></ul><ul><li>{ </li></ul><ul><li>public void inin() </li></ul><ul><li>{ </li></ul><ul><li>//initialization code goes here </li></ul><ul><li>} </li></ul><ul><li>public void service() </li></ul><ul><li>{ </li></ul><ul><li>//meaningful works happen here </li></ul><ul><li>} </li></ul><ul><li>public void destroy () </li></ul><ul><li>{ </li></ul><ul><li>// free resources here </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Actually a servlet is not required to extend GenericServlet or HttpServlet. An advanced developer can create a servlet interface and thus bypass the need to extend one of these classes. Extending the GenericServlet or HttpServlet is simplest and most common way to build servlet. </li></ul>concepts
  4. 4. <ul><li>Servlet Life Cycle </li></ul>Diagram
  5. 5. <ul><li>The Process of the servlet can be broken into nine steps: </li></ul><ul><li>The server loads the servlets when it is first requested by the client or, if configured to do so when server start-up. The servlet may be loaded either from local or remote location, using the standard java class load facility. This step equivalet to the following code: </li></ul><ul><li>Class c= class.forName(“com.sourcestream.MyServlet”); </li></ul><ul><li>The servlet creates one or more instance of the class. Depending on the implementation the server may create the single instance that services all requests through multiple threads or create a pool of instances from which one chosen to service each new request. This step is equivalent to following Java code: </li></ul><ul><li>Servlet s =(Servlet)c.newInstance(); </li></ul><ul><li>The servlet constructs the ServletConfig object that provides initialization information to the servlet. </li></ul><ul><li>The server calls the servlet init() method passing the object constructed in step 3 as parameter. The init() method is guaranteed to finish execution prior to the servlet processing the request. If the server has created the multiple servlet instances( step 2) , the method is called one time for each instances. </li></ul><ul><li>The server constructs ServletRequest or HttpServletRequest object from the data included in the client’s request. It also construct a Service response or HttpServiceResponse object that provides method for customizing the server’s response. The type of object passed in these two parameters depends on wether the servlet extends the GenericServlet or HttpServlet. </li></ul>concepts
  6. 6. <ul><li>The servlet calls the servlet service() method( or other more specific method like doGet() or doPost() for HttpServlet) passing the objects constructed in Step 5 parameter. When concurrent request arrives multiple request runs in the separate threads. </li></ul><ul><li>The service method calls the processes the client request by evaluating the ServletRequest or HttpServletRequestobject and responds using the ServletResponse or HttpServletResponse object. </li></ul><ul><li>The server recieves the another request for this servlet, the process begins again at Step 5. </li></ul><ul><li>When instructed unload the servlet perhaps by the server administrator programmatically by the servlet itself , the server calls the servlet’s destroy method . The servlet is then eligible for garbage collection. </li></ul>concepts

×