How Does a Java Servlet Work? JDBC

A servlet is a web component that generates dynamic content. Servlets are small, platform independent
Java classes compiled to an architecture-neutral bytecode that can be loaded dynamically into and run by a servlet container. Servlets interact with web clients via a request-response paradigm implemented by the servlet container. This request-response model is based on the behavior of HTTP. Servlets allow state, can use the JDBC API, and have a significant performance increase because they have no heavy process startup and initialization for each client request as CGI does.

According to Sun , a servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed via a requestresponse programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java servlet technology defines HTTP-specific servlet classes. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods.

Web and java servlet technology

Web and java servlet technology

Servlets can be used for any number of web-related applications. Let’s look at a few examples. Developing e-commerce “store clients” has become one of the most common uses for Java servlets. A servlet can build an online catalog based on the contents of a database. It can then present this catalog to the customer using dynamic HTML. The customer will choose the items to be ordered, enter the shipping and billing information, and then submit the data to the servlet. When the servlet receives the posted data, it will process the orders and place them in the database for fulfillment. Every one of these processes can be easily implemented using Java servlets.

Servlets can be used to deploy websites that open up large legacy systems on the Internet. Many companies have massive amounts of data stored on large mainframe systems. These businesses do not want to re-architect their systems, so they choose to provide inexpensive web interfaces into them. Because you have the entire JDK at your disposal and security provided by the servlet container, you can use servlets to interface with these systems using anything from Transmission Control Protocol/Internet Protocol (TCP/IP) to Common Object Request Broker Architecture (CORBA).

The following is a skeleton of a servlet:

HttpServlet (defined in the javax.servlet.http package) is an abstract class to be subclassed to create an HTTP servlet suitable for a website. A subclass of HttpServlet must override at least one method, usually one of these:

  • doGet(), for HTTP GET requests
  • doPost(), for HTTP POST requests
  • doPut(), for HTTP PUT requests
  • doDelete(), for HTTP DELETE requests
  • init() and destroy(), to manage resources that are held for the life of the servlet
  • getServletInfo(), which the servlet uses to provide information about itself

Figure shows one of the most common ways of using a Java servlet. A user

  1. requests (using a web browser or other means) some information by filling out an HTML form containing a link to a servlet and clicking the Submit (by invoking a GET or POST operation) button
  2. The server
  3. locates the requested servlet
  4. The servlet then executes the doGet() or doPost() method and gathers the information needed (by using some databases
  5. or other resources such as file systems) to satisfy the user’s request and constructs a web page
  6. containing the information (this can be HTML, XML, etc.). Finally, that web page is displayed on the user’s browser
  7. For details on Java servlets, refer to the following websites:

Most common way of using a java servelet.

Most common way of using a java servelet

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

JDBC Topics