Accessing Database from servlets through JDBC!

Accessing Database from servlets through JDBC! Java Servlets J ava Servlets are server side components that provides a powerful mechanism for developing server side of web application. Earlier CGI was developed to provide server side capabilities

Accessing Database from servlets through JDBC!

Java Servlets - Downloading and Installation


  Java Servlets are server side components that provides a powerful mechanism for developing server side of web application. Earlier CGI was developed to provide server side capabilities to the web applications. Although CGI played a major role in the explosion of the Internet, its performance, scalability and reusability issues make it less than optimal solutions. Java Servlets changes all that. Built from ground up using Sun's write once run anywhere technology java servlets provide excellent framework for server side processing.

   With Java servlets web developers can create fast and efficient server side application   and can run it on any Servlet enabled web server. Servlets runs entirely inside the Java Virtual Machine. Because the Servlet is running on the server side, it does not depend on browser compatibility. I just send the result in html formats.

  Java Servlets have a number of advantages over CGI and other API's. They are:

  1. Platform Independence
    Java Servlets are 100% pure Java, so it is platform independent. It can run on any Servlet enabled web server. For example if you develop an web application in windows machine running Java web server. You can easily run the same on apache web server (if Apache Serve is installed) without modification or compilation of code. Platform independency of servlets provides a great advantage over alternatives of servlets.
  2. Performance
    Due to interpreted nature of java, programs written in java are slow. But the java servlets runs very fast. These are due to the way servlets run on web server. For any program initialization takes significant amount of time. But in case of servlets initialization takes place very first time it receives a request and remains in memory till times out or server shut downs. After servlet is loaded, to handle a new request it simply creates a new thread and runs service method of servlet. In comparison to traditional CGI scripts which creates a new process to serve the request. This intuitive method of servlets could be use to develop high speed data driven web sites.
  3. Extensibility
    Java Servlets are developed in java which is robust, well-designed and object oriented language which can be extended or polymorphed into new objects. So the java servlets takes all these advantages and can be extended from existing class the provide the ideal solutions.
  4. Safety
    Java provides a very good safety features like memory management, exception handling etc. Servlets inherits all these features and emerged as a very powerful web server extension.
  5. Secure
    Servlets are server side components, so it inherits the security provided by the web server. Servlets are also benefited with Java Security Manager.

Java Servlet API

Java Servlet API contains two core packages:
  • javax.servlet
  • javax.servlet.http

   Servlets implement the javax.servlet.Servlet interface. The javax.servlet package contains the generic interfaces and classes that are implemented and extended by all servlets. While the javax.servlet.http package contains the classes that are used when developing HTTP - specific servlets. The HttpServlet is extended from GenericServlet base class and it implements the Servlet interface. HttpServlet class provides a framework for handling the HTTP requests.

The  javax.servlet.Servlet defines five methods:

  1. service() metho called by servlet to handle the client request in a new thead. service() methos accepts ServletRequest and a ServletResponse ohjects as parameters. ServletRequest object represents the client request. It contains the data sent in name/value pairs. ServletResponse object is used to send the response to the client.
  2. init() is called once when servlet is loaded. It is a good place to initialize global variables. This methos accepts ServletConfig as parameter which provides initialization arguments for the servlet.
  3. getServletConfig() returns the ServletConfig object passed to init().
  4. destory() is called when wervlet is unloaded from memory. This is a good place to clean up any resources(such as open files or database connections).
  5. getServletInfo() returns a string with version, copyright informations etc.


Installing Servlets

The servlet run inside a Web Server program. After it has been compiled, it must be installed onto Web Server in order to test it. Thus we have to follow the following steps:
  1. Install the servlet in a hosting server
  2. Request the servlet through web browser.

Java servlets are supported by a number of web servers. Java Web Server from JavaSoft was first servlet enabled web server. Now a days a number of web server supports java servlets. In this article we will use Java Web Server, whose evaluation copy can be downloaded from . In order to compile your servlet you also need Java Servlet Development Kit (JSDK) which is available at same place.


Installing on Windows95 and Win NT

  1. Download the distribution of Java Web Server from . Distribution is in the form of self-extracting file.
  2. The Java Web Server can be installed in any directory, so you can choose your favorite directory for installation. Move the downloaded file into your favorite directory.
  3. To begin the installation just double-click on the self-extracting file and the file extract itself and then installation begins.
  4. Follow the instruction and complete the installation.

To run the Web Server go to your installation directory and then to bin directory and then double click httpd.exe. To test the Web Server open your browser and type http://localhost:8080 in address bar and then press enter. Browser should display default index.html into browser.
Links To Servlets Tutorials