Skip to main content

Servlets



WHAT IS SERVLET?

Servlet technology is used to create web application resides at server side and generate web pages.
Servlet technology is robust and scalable because of java language.Before servlet ,CGI(common gateway interface) scripting language was popular as a server side programming language.But there was many disadvantages of this technology.
There are many interfaces and classes in the servlet API such as servlet,GenericServlet,HttpServlet,ServletRequest,ServletResponse etc.
Servlet is a class that extend the capabilities of the servers and respond to any type of request.

Servlet is a web component that is deployed on the servers to create dynamic web page.


HOW WEB SERVERS WORKS?

  •  To retrieve a static page , a user enters a url in to a browser.
  • Browser generates an HTTP request to appropriate server.
  •  Web server maps the request to the particular file and that file is returned to the browser in an HTTP response.
  • HTTP header in response indicates the type of content.
  • Multipurpose internet mail extension (MIME) are used for this purpose.
  •  ASCII text file has MIME-text/plain.
  • HTML file has MIME-text/html.            
 HOW TO FETCH DYNAMIC WEB PAGE?

  •       If a user wants to request for dynamic content ,like availability of tickets,price,orders etc.        
  •         The content of such web pages must be dynamically generated to reflect the latest information in database.
  •       This can be done by creating web application.


  • WEB APPLICATION

  •          A web application is an application accessible from the web.
  •           A web application is composed of web components like servlets ,JSP,CGI etc and other components like HTML.
  •       The web components typically execute in web server and respond to HTTP request.
    COMMON GATEWAY INTERFACE(CGI):
  •           Used to create dynamic web pages prior to servlet technology.
  •          CGI technology enables the web server to call an external program and pass HTTP request information to the external program to process the request.
  •           For each request it start a new process.

   DISADVANTAGES OF SERVLET:-
There are many problems in CGI technology:
  1. If number of clients increases, it takes more time for sending response.
  2. For each request, it starts a process and Web server is limited to start processes.
  3. It uses platform dependent language e.g. C, C++, perl.

   ADVANTAGES OF SERVLET:-

·         There are many advantages of servlet over CGI.
·          The web container creates threads for handling the multiple requests to the servlet.

·          Threads have a lot of benefits over the Processes such as they share a common memory area, lightweight, cost of communication between the threads are low.
·         The basic benefits of servlet are as follows:

·         PORTABILITY:- Servlet are supported on all platforms that support java and servlet work with all major web servers.They are highly portable across operating systems and across server implementations.

·         POWER:-Servlets has full power of the core java API networking and url access ,multithreading ,image manipulation ,data compression ,remote method invocation ,and legacy integration .Servlet can also take the advantage of the J2EE Platform.

·         EFFICIENCY AND ENDURANCE:-Servlet stays in server memory as a single object instance,it automatically maintains its state and can hold on to external resources.

·         SAFETY:-As they are written in java ,servlets inherits the strong type safety of java language.

·         INTEGRATION:-Servlets are highly integrated with the server.This integration allows a servlet to cooperate with the server in a ways that a CGI program cannot.

·         EXTENSIBILITY AND FLEXIBILITY:-Servlets could be extended and optimized for another type of servlets.They can create simple content using a println() statements, or they can generate complicated set of pages using a template engine.

    LIFE CYCLE OF A SERVLET:-
  A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet.
·        The servlet is initialized 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 garbage collected by the garbage collector of the JVM.

1.)        THE INIT() METHOD:-
·         The init method is called only once. It is called only when the servlet is created, and not called for any user requests afterwards. So, it is used for one-time initializations, just as with the init method of applets.
·         The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can also specify that the servlet be loaded when the server is first started.
·         When a user invokes a servlet, a single instance of each servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate. The init() method simply creates or loads some data that will be used throughout the life of the servlet.
·         The init method definition looks like this –

   public void init() throws ServletException {
       // Initialization code...
}

2.)        THE SERVICE() METHOD:-
·         The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.
·         Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
·         Here is the signature of this method −
  public void service(ServletRequest request, ServletResponse response)
   throws ServletException, IOException {
  }
·         The service () method is called by the container and service method invokes doGet, doPost, doPut, doDelete, etc. methods as appropriate. So you have nothing to do with service() method but you override either doGet() or doPost() depending on what type of request you receive from the client.
·         The doGet() and doPost() are most frequently used methods with in each service request. Here is the signature of these two methods.
            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.
   public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
   // Servlet code
   }

            The doPost() Method
·         A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method.
   public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
   // Servlet code
   }
    
      3.)  THE DESTROY() METHOD:-
·         The destroy() method is called only once at the end of the life cycle of a servlet. This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities.
·         After the destroy() method is called, the servlet object is marked for garbage collection. The destroy method definition looks like this −
   public void destroy() {
   // Finalization code...







Comments

Popular posts from this blog

Creating JDBC Connection in web application

There are following are six steps involved in creating a JDBC application − ·         Import Require packages:  we need to import JDBC packages that are Requires while creating connection with mysql. JDBC classes needed for database connectoin. Most often, using  import java.sql.*  will suffice. ·         Register the JDBC driver:  Driver manager class will attempt to load driver referenced in the jdbc.drivers. This makes User to customize the JDBC drivers Used by their application. ·          Open a connection :  it need the  DriverManager.getConnection()  method to create a Connection object, which represents a physical connection with the database. ·         Execute a query: For executing a query we will call execute methods from different statements 1.   Execute : Return true if the fir...

REST API in PHP

Step 1 . Create a table in database mysql with below script.           CREATE TABLE `facts` (                 `Id` int(11) NOT NULL,                  `title` varchar(512) NOT NULL,                  `myth` varchar(512) NOT NULL,                  `fact` varchar(512) NOT NULL            ) ENGINE=InnoDB DEFAULT CHARSET=latin1; Step 2 . Create a folder in xampp/htdocs/ testRestAPI       Here we need to create 2 main files 1.     Database.php 2.     GetAllFacts.php In database.php we wi...

LOGIN AND SIGN UP USING BOOTSTRAP

WHAT IS BOOTSTRAP? ·         Bootstrap is a sleek, intuitive, and powerful, mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. HISTORY:- ·          Bootstrap was developed by  Mark Otto  and  Jacob Thornton  at  Twitter . It was released as an open source product in August 2011 on GitHub. WHY USE BOOTSTRAP? ·          Mobile first approach  − Bootstrap 3, framework consists of Mobile first styles throughout the entire library instead them of in separate files. Browser Support  − It is supported by all popular browsers. ·          Easy to get started  − With just the knowledge of HTML and CSS anyone can get started with Bootstrap. Also the Bootstrap official site has a good documentation. ·     ...