Introduction to Java Servlets.

Hello Friends,

This is my first tutorial on the Java Servlets. This will be helpful for all the newbie who wants to learn the advance java programming. This is the first and basic step to enter the java enterprise environment. In this tutorial we will be focusing on what actually are servlets, what are its advantages some simple things related to Servlets.

What are Servlets?

Servlets are replacement to CGI (Common Gateway Interface) programming with inclusion of java technology. These are simple Java classes that run at the server side and provide the means through which Web Applications can be written in Java. Web Applications are the one that allows a web site to be dynamic instead of being static. In other words, Servlets transform a web site with plain text and images into a rich interactive java enabled environment for the user. It is designed on the simple request-response type model.  Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. They are not tied to a specific client-server protocol, but are most often used with the HTTP protocol. Therefore, the word “Servlet” is often used in the meaning of “HTTP Servlet”.

Java Servlet is a generic server extension that means a java class can be loaded dynamically to expand the functionality of a server. They are used with web servers and run inside a Java Virtual Machine (JVM) on the server which makes them safe and portable to use on any java enabled environment. Actually, it is a Java Applet that runs on a server instead of a browser. Also, they do not require support for java in the web browser as that of the applets, this is because it runs at server side so it does not checks the browser for compatibility. Unlike CGI, servlets don’t use multiple processes to handle separate request. Servets can be handled by separate threads within the same process.

How Servlet runs?

To deploy and run the servlet, any web server can be used. Some of the available servers are JBoss, WebSpeher, WebLogic, Apache Tomcat etc. Among these Apache tomcat is used widely, as it’s an open source servlet container developed by the Apache Software Foundation. Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications and provides a “pure Java” HTTP web server environment for Java code to run smoothly.

The servlet API, contained in the Java package hierarchy javax.servlet, defines the expected interactions of a Web container and a servlet. A Web container is the essential component of a web server that interacts with the servlets. The container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights (for more,on how the container works click here.).

As the above image shows-

  1. Client requests for the data by clicking on the link from browser.
  2. Request comes to the container and then container asks for the response from the servlet, somewhere in the server.
  3. Server sends the response to the container.
  4. Container converts the response into the HTTP Response and sends back to the browser and displays the results.

Advantages and Characteristics of Servlets :-

  • Servlets are server as well as platform-independent,.
  • Servlets run entirely inside the Java Virtual Machine.
  • Servlets can access the entire family of Java APIs, including the JDBC API to access enterprise databases.
  • Servlet provides the benefits of the mature java language including portability, performance, reusability, and crash protection.
  • Each request to be handled by a servlet is handled by a separate Java thread created by the container within the Web server process. This improves the performance of the application.
  • Only a single instance answers all the incoming requests concurrently. This reduces memory usage and makes the management of persistent data easy.

Hope this introductory chapter will help you understand the Servlet’s basics.


One thought on “Introduction to Java Servlets.”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.