Servlets

Servlets Chapters

Introduction to Servlets

Introduction to Servlet Technology

The servlet technology is the foundation of web application development which is based on Java programming language. Therefore, understanding the servlet technology and its architecture is important if you want to be a web application developer.

Java Servlets are server-side technology which are used to build web Applications.

Servlets requires container to run.

A servlet acts as an intermediary between the client and the server.

Servlet is integrated with the Java language and it can possesses all the features of Java language such as  portability, platform independence, security and multi threading.  database

 

Before we start developing web applications using servlets let us have a quick look at CGI technology.

CGI technology was used prior to servlet technology for developing web Applications

 

 

For any further quires do write us to madhusudan@javaconceptz.com

CGI Overview

The CGI Technology Overview

Prior to Servlet Technology, Common Gateway Interface (CGI) technology was widely used for developing web Application.

CGI is standard method for generating dynamic contents on web pages .It is an interface between the browser and the Web server that runs the external program (CGI Scripts). This external program could be written in any language such as c, c++, Perl etc

Steps for processing data using CGI

  • Parameters of user requests are encoded in a URL
  • User requests are transmitted from the Web server to the gateway program
  • Data is returned to the Web server

Disadvantages of CGI Technology

  • Large programming overheads in Memory
  • Work load on web server is too high
  • Even when browser fails to send request, the server will continue to run the program until system Administrator he turns off the faulty scripts
  • Communication issues between CGI Scripts since CGI were vender specific
  • Upgrading to new CGI scripts was difficult and cumbersome

As the Internet became more and more popular, the number of users visiting web has increased drastically and apparently, CGI technology failed to deliver the needs.Gradually, new and better technologies like Servlets replaced CGI as the main technology for web application development.

What is a Container

What is a Container?

Container is a environment or component which manages the life cycle of applications deployed in it.

Clients can send requests to the Container and Container in turn executes the application code & sends the response to the clients request.

What is Java Container

In case of Java Container , the container manages the life cycle of Java/JEE applications deployed or running in it.

 

Types of Java Containers

  1. Web Container
  2. Application Container or EJB Container

Web Container
manages the execution of JEE components like servlets & JSP’s Example : Tomcat ,Glassfish

Application Container or EJB Container
manages the execution of enterprise beans for JEE applications. Example: Weblogic ,Websphere,Jboss Etc

What is Servlet

What is Servlet?

A servlet is a simple Java class which can be dynamically loaded into and run by a web container.

Servlets are combination of Java & HTML code (within our Java code we are including HTML code) which can be run by  container.

Few Examples of Containers are :

  • Tomcat
  • Glassfish
  • Jboss
  • Weblogic
  • Websphere
  • Jetty

Advantages of Servlet Technology

  • Performance: Each Request is processed by separate servlets in the container and is destroyed once it is completed.
  • Portable: Servlets can be easily transferred from one location to another without changes.
  • Rapid Development cycle: Development is much easier than CGI since servlets has Rich java library base.
  • Security: Since servlets are server side Components, it inherits the security of server.

Servlet Architecture

Servlet Application Architecture

HTTP is the standard request-response protocol used by servlets to interact with client.

Browser sends an HTTP request to the web server. Web server checks if the request is valid or not and passes the request to the servlet container which will find the appropriate servlet to handle the client request. The servlet processes the request and generates the response in the form of HTTP response to browser via web server.

Commonly used servlet containers are Tomcat, Jetty, Jboss etc.

Web container can process both static and dynamic contents also.

Lifecycle of a Servlet

A servlet is basically a simple Java program that runs within a Web container. It can receive requests from clients and return responses.

The life cycle of a servlet is managed by servlet container. It can be divided into following phases

  1. Load Servlet Class
  2. Create Instance of Servlet
  3. Call the servlet’s init ( ) method
  4. Call the servlet’s service ( ) method
  5. Call the servlet’s destroy ( ) method

Initialization:

When the servlet is initially loaded the first 3 steps will get executed. The servlet is initialized only once by calling init ( ) method.

Note: servlet will not get loaded until first request is received. We can force the container to load by giving <load on startup> parameter in web.xml servlet configuration file.

The signature of this method is as follows:

public void init(ServletConfig config) throws ServletException
{
//Initialization Code
}

Service:

After initialization, the servlet’s service ( ) method is called which serves the client’s request and sends the response back to browser.
For Every HTTP request the service method will get executed.

Service ( ) method can be implemented by invoking doGet ( ) or doPost ( ) methods also.

The signature of this method is as follows:

public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
//code
}

 

public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
// code
}

 

public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
// code
}

Destruction:

When all client requests are processed and the servlet will be idle for a specific amount of time will be eligible for garbage collection are destroyed by web server by calling destroy ( ) method.

The signature of this method is as follows:

public void destroy()
{
 // Finalization code
}

Types of Servlet

Types of servlet

There are two main of servlet types

  1. Generic Servlet
  2. Http Servlet

All the servlet interfaces are defined in javax.servlet and javax.httpservlet Packages.

Generic Servlet :

Extends javax.servlet.GenericServlet

It is protocol independent &support and implements servicemethod. Generic servlet does not support cookies and sessions.

Signature for service( ) method

public void service  ( ServletRequest request,ServletResponse response)
throws ServletException, IOException
{
//code//
}

Http Servlet:

Extends javax.servlet.HttpServlet

It is HTTP Protocol dependent .It support and implements (doPost & doGet) methods which is responsible for handling HTTP request & response from servlets.

Http Servlet supports cookie & session tracking.

Signature for doPost( ) method

public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
// code
}

Signature for doGet( ) method

public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
// code
}

Servlet Pre-requisite

Pre-requisite for writing a Servlet or Required Software

We need below mentioned software & API’s for developing web Applications using servlet technology.

  • Java or JDK 1.6 and above
  • Servlet container like tomcat , glassfish or any container
  • Eclipse IDE or any IDE
  • Servlet API jar file ( version 2.5 or 3.0 is preferred )

 

Click the link below to download Software’s required for development

How a Servlet Works

How a Servlet Works:

When ever a request from client is received by the servlet container , it loads the required or requested  servlet into the memory for the first time.

The servlet then forwards the user request, processes it, and returns the response to the servlet container, which in turn sends the response back to the client.

After that, the servlet stays in memory waiting for other requests—it will not be unloaded from the memory unless the servlet container sees a shortage of memory. Each time the servlet is requested, however, the servlet container compares the timestamp of the loaded servlet with the servlet class file. If the class file timestamp is more recent, the servlet is reloaded into memory.  we don’t need to restart the servlet container every time after updating the servlet.

The way in which a servlet works inside the servlet container is shown below

 

Advanced Access Specifiers

Integer non dui dignissim, tincidunt nibh eu, placerat purus. Cras sit amet tellus tempor, pulvinar neque quis; lacinia lorem. Nullam euismod, ante eget iaculis vulputate, lorem felis maximus ligula, in consectetur arcu enim pulvinar est. Nam vitae rhoncus libero, non tristique dui. Suspendisse a sem ultrices, suscipit eros sit amet, fringilla turpis. Nunc et tempor magna. Duis imperdiet tincidunt congue. Curabitur pulvinar dui turpis, sed dapibus quam tristique at. Duis commodo sem vel enim luctus imperdiet! Donec euismod mollis aliquam! Ut cursus id turpis et molestie. Sed vel nisl sit amet purus tristique aliquam. Pellentesque tempus ante mauris, non faucibus felis convallis id.

Morbi nec pharetra enim, ut laoreet dui. Donec gravida vitae ipsum id malesuada. Vivamus at aliquam lectus. Vestibulum vitae purus risus. Nunc tempus dignissim nisl eu hendrerit. Sed pharetra dolor vitae nunc rutrum ultricies. Donec efficitur felis at diam varius condimentum. Nullam aliquam accumsan suscipit. Sed sed tincidunt turpis. Integer posuere ante sed nulla vestibulum hendrerit. Quisque mi nisl, consequat vitae lectus dictum, commodo vehicula risus. Quisque faucibus velit in cursus pulvinar. Sed consectetur, nulla eu congue tempus, tellus turpis lobortis diam; a fermentum erat nisi non quam! Aliquam sollicitudin sollicitudin suscipit. Fusce in pulvinar ante.

Vestibulum ut condimentum risus. Morbi tempor vehicula est et sagittis! Etiam finibus elit non urna pharetra, quis pharetra augue posuere. Aliquam sit amet tortor orci. Donec tristique velit id lectus ultrices, nec aliquet lorem convallis. Fusce ut hendrerit enim. Mauris tristique mollis ex et porttitor.

Threads

Integer non dui dignissim, tincidunt nibh eu, placerat purus. Cras sit amet tellus tempor, pulvinar neque quis; lacinia lorem. Nullam euismod, ante eget iaculis vulputate, lorem felis maximus ligula, in consectetur arcu enim pulvinar est. Nam vitae rhoncus libero, non tristique dui. Suspendisse a sem ultrices, suscipit eros sit amet, fringilla turpis. Nunc et tempor magna. Duis imperdiet tincidunt congue. Curabitur pulvinar dui turpis, sed dapibus quam tristique at. Duis commodo sem vel enim luctus imperdiet! Donec euismod mollis aliquam! Ut cursus id turpis et molestie. Sed vel nisl sit amet purus tristique aliquam. Pellentesque tempus ante mauris, non faucibus felis convallis id.

Morbi nec pharetra enim, ut laoreet dui. Donec gravida vitae ipsum id malesuada. Vivamus at aliquam lectus. Vestibulum vitae purus risus. Nunc tempus dignissim nisl eu hendrerit. Sed pharetra dolor vitae nunc rutrum ultricies. Donec efficitur felis at diam varius condimentum. Nullam aliquam accumsan suscipit. Sed sed tincidunt turpis. Integer posuere ante sed nulla vestibulum hendrerit. Quisque mi nisl, consequat vitae lectus dictum, commodo vehicula risus. Quisque faucibus velit in cursus pulvinar. Sed consectetur, nulla eu congue tempus, tellus turpis lobortis diam; a fermentum erat nisi non quam! Aliquam sollicitudin sollicitudin suscipit. Fusce in pulvinar ante.

Vestibulum ut condimentum risus. Morbi tempor vehicula est et sagittis! Etiam finibus elit non urna pharetra, quis pharetra augue posuere. Aliquam sit amet tortor orci. Donec tristique velit id lectus ultrices, nec aliquet lorem convallis. Fusce ut hendrerit enim. Mauris tristique mollis ex et porttitor.

Collections

Integer non dui dignissim, tincidunt nibh eu, placerat purus. Cras sit amet tellus tempor, pulvinar neque quis; lacinia lorem. Nullam euismod, ante eget iaculis vulputate, lorem felis maximus ligula, in consectetur arcu enim pulvinar est. Nam vitae rhoncus libero, non tristique dui. Suspendisse a sem ultrices, suscipit eros sit amet, fringilla turpis. Nunc et tempor magna. Duis imperdiet tincidunt congue. Curabitur pulvinar dui turpis, sed dapibus quam tristique at. Duis commodo sem vel enim luctus imperdiet! Donec euismod mollis aliquam! Ut cursus id turpis et molestie. Sed vel nisl sit amet purus tristique aliquam. Pellentesque tempus ante mauris, non faucibus felis convallis id.

Morbi nec pharetra enim, ut laoreet dui. Donec gravida vitae ipsum id malesuada. Vivamus at aliquam lectus. Vestibulum vitae purus risus. Nunc tempus dignissim nisl eu hendrerit. Sed pharetra dolor vitae nunc rutrum ultricies. Donec efficitur felis at diam varius condimentum. Nullam aliquam accumsan suscipit. Sed sed tincidunt turpis. Integer posuere ante sed nulla vestibulum hendrerit. Quisque mi nisl, consequat vitae lectus dictum, commodo vehicula risus. Quisque faucibus velit in cursus pulvinar. Sed consectetur, nulla eu congue tempus, tellus turpis lobortis diam; a fermentum erat nisi non quam! Aliquam sollicitudin sollicitudin suscipit. Fusce in pulvinar ante.

Vestibulum ut condimentum risus. Morbi tempor vehicula est et sagittis! Etiam finibus elit non urna pharetra, quis pharetra augue posuere. Aliquam sit amet tortor orci. Donec tristique velit id lectus ultrices, nec aliquet lorem convallis. Fusce ut hendrerit enim. Mauris tristique mollis ex et porttitor.

Control Flow

Integer non dui dignissim, tincidunt nibh eu, placerat purus. Cras sit amet tellus tempor, pulvinar neque quis; lacinia lorem. Nullam euismod, ante eget iaculis vulputate, lorem felis maximus ligula, in consectetur arcu enim pulvinar est. Nam vitae rhoncus libero, non tristique dui. Suspendisse a sem ultrices, suscipit eros sit amet, fringilla turpis. Nunc et tempor magna. Duis imperdiet tincidunt congue. Curabitur pulvinar dui turpis, sed dapibus quam tristique at. Duis commodo sem vel enim luctus imperdiet! Donec euismod mollis aliquam! Ut cursus id turpis et molestie. Sed vel nisl sit amet purus tristique aliquam. Pellentesque tempus ante mauris, non faucibus felis convallis id.

Morbi nec pharetra enim, ut laoreet dui. Donec gravida vitae ipsum id malesuada. Vivamus at aliquam lectus. Vestibulum vitae purus risus. Nunc tempus dignissim nisl eu hendrerit. Sed pharetra dolor vitae nunc rutrum ultricies. Donec efficitur felis at diam varius condimentum. Nullam aliquam accumsan suscipit. Sed sed tincidunt turpis. Integer posuere ante sed nulla vestibulum hendrerit. Quisque mi nisl, consequat vitae lectus dictum, commodo vehicula risus. Quisque faucibus velit in cursus pulvinar. Sed consectetur, nulla eu congue tempus, tellus turpis lobortis diam; a fermentum erat nisi non quam! Aliquam sollicitudin sollicitudin suscipit. Fusce in pulvinar ante.

Vestibulum ut condimentum risus. Morbi tempor vehicula est et sagittis! Etiam finibus elit non urna pharetra, quis pharetra augue posuere. Aliquam sit amet tortor orci. Donec tristique velit id lectus ultrices, nec aliquet lorem convallis. Fusce ut hendrerit enim. Mauris tristique mollis ex et porttitor.

Networking In Java

Integer non dui dignissim, tincidunt nibh eu, placerat purus. Cras sit amet tellus tempor, pulvinar neque quis; lacinia lorem. Nullam euismod, ante eget iaculis vulputate, lorem felis maximus ligula, in consectetur arcu enim pulvinar est. Nam vitae rhoncus libero, non tristique dui. Suspendisse a sem ultrices, suscipit eros sit amet, fringilla turpis. Nunc et tempor magna. Duis imperdiet tincidunt congue. Curabitur pulvinar dui turpis, sed dapibus quam tristique at. Duis commodo sem vel enim luctus imperdiet! Donec euismod mollis aliquam! Ut cursus id turpis et molestie. Sed vel nisl sit amet purus tristique aliquam. Pellentesque tempus ante mauris, non faucibus felis convallis id.

Morbi nec pharetra enim, ut laoreet dui. Donec gravida vitae ipsum id malesuada. Vivamus at aliquam lectus. Vestibulum vitae purus risus. Nunc tempus dignissim nisl eu hendrerit. Sed pharetra dolor vitae nunc rutrum ultricies. Donec efficitur felis at diam varius condimentum. Nullam aliquam accumsan suscipit. Sed sed tincidunt turpis. Integer posuere ante sed nulla vestibulum hendrerit. Quisque mi nisl, consequat vitae lectus dictum, commodo vehicula risus. Quisque faucibus velit in cursus pulvinar. Sed consectetur, nulla eu congue tempus, tellus turpis lobortis diam; a fermentum erat nisi non quam! Aliquam sollicitudin sollicitudin suscipit. Fusce in pulvinar ante.

Vestibulum ut condimentum risus. Morbi tempor vehicula est et sagittis! Etiam finibus elit non urna pharetra, quis pharetra augue posuere. Aliquam sit amet tortor orci. Donec tristique velit id lectus ultrices, nec aliquet lorem convallis. Fusce ut hendrerit enim. Mauris tristique mollis ex et porttitor.

Java Arrays

Integer non dui dignissim, tincidunt nibh eu, placerat purus. Cras sit amet tellus tempor, pulvinar neque quis; lacinia lorem. Nullam euismod, ante eget iaculis vulputate, lorem felis maximus ligula, in consectetur arcu enim pulvinar est. Nam vitae rhoncus libero, non tristique dui. Suspendisse a sem ultrices, suscipit eros sit amet, fringilla turpis. Nunc et tempor magna. Duis imperdiet tincidunt congue. Curabitur pulvinar dui turpis, sed dapibus quam tristique at. Duis commodo sem vel enim luctus imperdiet! Donec euismod mollis aliquam! Ut cursus id turpis et molestie. Sed vel nisl sit amet purus tristique aliquam. Pellentesque tempus ante mauris, non faucibus felis convallis id.

Morbi nec pharetra enim, ut laoreet dui. Donec gravida vitae ipsum id malesuada. Vivamus at aliquam lectus. Vestibulum vitae purus risus. Nunc tempus dignissim nisl eu hendrerit. Sed pharetra dolor vitae nunc rutrum ultricies. Donec efficitur felis at diam varius condimentum. Nullam aliquam accumsan suscipit. Sed sed tincidunt turpis. Integer posuere ante sed nulla vestibulum hendrerit. Quisque mi nisl, consequat vitae lectus dictum, commodo vehicula risus. Quisque faucibus velit in cursus pulvinar. Sed consectetur, nulla eu congue tempus, tellus turpis lobortis diam; a fermentum erat nisi non quam! Aliquam sollicitudin sollicitudin suscipit. Fusce in pulvinar ante.

Vestibulum ut condimentum risus. Morbi tempor vehicula est et sagittis! Etiam finibus elit non urna pharetra, quis pharetra augue posuere. Aliquam sit amet tortor orci. Donec tristique velit id lectus ultrices, nec aliquet lorem convallis. Fusce ut hendrerit enim. Mauris tristique mollis ex et porttitor.

Java Strings

Integer non dui dignissim, tincidunt nibh eu, placerat purus. Cras sit amet tellus tempor, pulvinar neque quis; lacinia lorem. Nullam euismod, ante eget iaculis vulputate, lorem felis maximus ligula, in consectetur arcu enim pulvinar est. Nam vitae rhoncus libero, non tristique dui. Suspendisse a sem ultrices, suscipit eros sit amet, fringilla turpis. Nunc et tempor magna. Duis imperdiet tincidunt congue. Curabitur pulvinar dui turpis, sed dapibus quam tristique at. Duis commodo sem vel enim luctus imperdiet! Donec euismod mollis aliquam! Ut cursus id turpis et molestie. Sed vel nisl sit amet purus tristique aliquam. Pellentesque tempus ante mauris, non faucibus felis convallis id.

Morbi nec pharetra enim, ut laoreet dui. Donec gravida vitae ipsum id malesuada. Vivamus at aliquam lectus. Vestibulum vitae purus risus. Nunc tempus dignissim nisl eu hendrerit. Sed pharetra dolor vitae nunc rutrum ultricies. Donec efficitur felis at diam varius condimentum. Nullam aliquam accumsan suscipit. Sed sed tincidunt turpis. Integer posuere ante sed nulla vestibulum hendrerit. Quisque mi nisl, consequat vitae lectus dictum, commodo vehicula risus. Quisque faucibus velit in cursus pulvinar. Sed consectetur, nulla eu congue tempus, tellus turpis lobortis diam; a fermentum erat nisi non quam! Aliquam sollicitudin sollicitudin suscipit. Fusce in pulvinar ante.

Vestibulum ut condimentum risus. Morbi tempor vehicula est et sagittis! Etiam finibus elit non urna pharetra, quis pharetra augue posuere. Aliquam sit amet tortor orci. Donec tristique velit id lectus ultrices, nec aliquet lorem convallis. Fusce ut hendrerit enim. Mauris tristique mollis ex et porttitor.

Leave a Reply

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