Home » Servlets » Servlets Basic Concepts

Servlets Basic Concepts

Servlets Basic Concepts

Servlets are Java objects that implement the javax.servlet.Servlet interface. They are loaded by a Servlet server such as Tomcat, Jetty, Resin, or BEA's WebLogic Server. Any web access to that address is picked up by the Servlet server and directed to the corresponding Servlet, the Servlet analyses the request and responds, usually with a html page containing the answer to the request.

There are some key points that must be known by the Java programmer like server, container, get request, post request etc. Let's first discuss these points step by step.

Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.

HTTP (Hyper Text Transfer Protocol) :

Before we can start writing the first Servlet, we need to know some basics of HTTP ("HyperText Transfer Protocol"), the protocol which is used by a WWW client (e.g. a browser) to send a request to a Web Server.

Http is the protocol that allows web servers and browsers to exchange data over the web.It is a request response protocol.

It is stateless means each request is considered as the new request. In other words, server doesn't recognize the user by default.

Http Request Methods :

Every request has a header that tells the status of the client. There are many request methods. Get and Post requests are mostly used.

Following are list of http request methods :

  • GET
  • POST
  • HEAD
  • PUT
  • DELETE
  • OPTIONS
  • TRACE

HTTP RequestDescription
GETAsks to get the resource at the requested URL.
POSTAsks the server to accept the body info attached. It is like GET request with extra info sent with the request.
HEADAsks for only the header part of whatever a GET would return. Just like GET but with no body.
TRACEAsks for the loopback of the request message, for testing or troubleshooting.
PUTSays to put the enclosed info (the body) at the requested URL.
DELETESays to delete the resource at the requested URL.
OPTIONSAsks for a list of the HTTP methods to which the thing at the request URL can respond

Difference between Get and Post :

GETPOST
1) In case of Get request, only limited amount of data can be sent because data is sent in header.In case of post request, large amount of data can be sent because data is sent in body.
2) Get request is not secured because data is exposed in URL bar.Post request is secured because data is not exposed in URL bar.
3) Get request is idempotent. It means second request will be ignored until response of first request is delivered.Post request is non-idempotent
4) Get request is more efficient and used more than PostPost request is less efficient and used less than get.


What is a Servlet Container ?

To know what is a Servlet container, we need to know what is a Web Server first.

1. What is a Web Server ?

Servlet Architecture


A web server uses HTTP protocol to transfer data. In a simple situation, a user type in a URL (e.g. www.facebook.com) in browser (client), and get a web page to read. So what the server does is sending a web page to the client. The transformation is in HTTP protocol which specifies the format of request and response message.

As we see here, the user/client can only request static webpage from the server. This is not good enough, if the user wants to read the web page based on his input. The basic idea of Servlet container is using Java to dynamically generate the web page on the server side. So servlet container is essentially a part of a web server that interacts with the servlets.

Servlet Architecture


Servlet container is the container for Servlets.


Server :

It is a running program or software that provides services.

There are two types of servers:

  1. Web Server
  2. Application Server


Web Server :

Web server contains only web or servlet container. It can be used for servlet, jsp, struts, jsf etc. It can't be used for EJB.

Example of Web Servers are: Apache Tomcat and Resin.


Application Server :

Application server contains Web and EJB containers. It can be used for servlet, jsp, struts, jsf, ejb etc.

Example of Application Servers are:

  • JBoss Open-source server from JBoss community.
  • Glassfish provided by Sun Microsystem. Now acquired by Oracle.
  • Weblogic provided by Oracle.
  • Websphere provided by IBM.


Content Type :

Content Type is also known as MIME (Multipurpose internet Mail Extension) Type. It is a HTTP header that provides the description about what are you sending to the browser.

There are many content types as listed below:

  • text/html
  • text/plain
  • application/pdf
  • images/jpeg etc...


Previous Article Next Article

comments powered by Disqus