It is a part of java programming language. It is an advance technology or advance version of java specially designed to develop web based.
Size: 559.01 KB
Language: en
Added: Feb 06, 2022
Slides: 18 pages
Slide Content
PRESENTED BY A.KEERTHIKA M.SC(IT) NADAR SARASWATHI COLLEGE OF & SCIENCE THENI
LIFE CYCLE OF SERVLET java servlet life cycle consists of a series of events that begins when the servlet container loads servlet, and ends when the container is closed down servlet. A servlet container is the part of a web server or an application server the controls a servlet by managing its life cycle. A servlet life cycle can be define as the entire process from its creation till destruction. The following are the path followed by a servlet The servlet is initalized by calling the init()method. The servlet calls service()method to process a client’s request. The servlet is terminated by calling the destroy()method. Finally, servlet is grabage collected by the grabage collector of the JVM.
THE INIT()METHOD The init technique is called just once. It is called just when the servlet is made, and not require any client ask for thereafter. In this way, it is utilized for one-time instatements, similary as with the init strategy for applets. The servlet is typically made when a client initally conjures a URL comparing to the servlet, yet you can likewise indicate that the servlet be stacked when the server is first begum. At the point when a user invoke the servlet, a solitary example of each servlet gets made, with every client ask for bringing about another string that is given off to doGet or doPost as proper. The init() technique just make or loads a few infoemation that will be utilized for the duration of the servlet.
The web container calls the init method only once after creating the servlet instance. The init method is used to init method is used to initilized the servlet. It is the life cycle of method of the javax.servlet.servlet interface.
The init method definition public void init Throws servletException{ //Initialization code… THE SERVICE() METHOD The administration() strategy is the principel technique to play out the genuine errand. The servlet compartment(i.e. web server) calls the administration() stragy to deal with demands originating from the customer(program) and to compos the arranged reaction back to the customer. Each time the server gets a demand for a servlet, the server brings forth another string and calls benefit. The administration() technique checks the HTTP ask for type(GET,POST,PUT, DELETE, and so forth) and calls doGet, doPost, doPut, doDelete, and so forth strategies as proper.
The web container calls the server method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. Notice that servlet is initilized only once.
The server method definition public void service(serviceRequest request, ServletResponse response) Throws SerletException, IOException{ } THE doGET() METHOD A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method. the do Get method definition public void doGet(httpServletRequest request, httpServletResponse response)
throws ServletExepion,IoException{ //Servlet code } THE doPOST() METHOD A POST request result from an HTML form that specifically list POST as the METHOD and it should be handled by doPost() method public void doPost(httpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ //Finalization code…
} THE DETORY() METHOD The destory() method is called only once at the end of the life cycle of a servlet. This method gives you servlet a chance to close database connection, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities. The destory method definition public void destory(){ //Finalization code… }
The web container calls the destroy method before removing the servlet instance from the service. it gives the servlet on opportunity to clean up any resource for example memory, thread etc. the syntax of the destroy method of the servlet interface. Public void destroy()
Initially the HTTP ash for going to the server are designated to the servlet compartment. The servlet holder stacks the servlet before summoning the admnistration() strategy. At the point the servlet holder handles various demands by producing different string , each string excuting the administration() technique for a solitary occasion of the servlet. Servlet are java classes which service HTTP request and implement the javax.servlet.servlet interface.
Web application developers typically write servlets that exten javax. Servlet. http.HttpServlet,an abstract class that implements the Servlet interface and is specially designed to handle HTTP requests.
// java program to show servlet example // importing required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // extend Httpservlet class Public class AdvanceJavaconcepts extends HttpServlet {
private String output; // Initializing servlet Public void init() throws ServletException { output =“ Advance java concepts”; } // requesting and printing the output Public void doget(HttpServletRequest req,HttpServletResponse rep) throws servletException, IoException {
resp. setContentType (“text/html”); printWriter out = resp.getWriter(); out.println(output); } Pubic void destroy() { system.out.println (“over”); } }