Servlets and JSP

Web Server, Web Container and Application Server

HTTP Server

Introduction to Important Concepts in Internet and Web Technologies

Internet is a global system of interconnected computer networks that connect various Domain Name System (DNS) servers, web hosting servers, home and business users, etc. Internet is a network of networks.


Internet Protocols

Lot of data transfer happens in an internet between computers and networks, and there should be some rules to regulate these transfers. A communications protocol is a system of digital rules for data exchange within or between computers.

Servlet Class Hierarchy and Life Cycle

A servlet is a java class that conform to the servlet specification and need to be deployed to a servlet container to work as a servelt. A servlet’s life cycle consist of Servlet class loading, Servlet instantiation, and then methods init(), service() and destroy().

Parameters and Attributes in Servlet

Parameters may come into our application from the client request, or may be configured through deployment descriptor (web.xml) elements or their corresponding annotations. When you submit a form, form values are sent as request parameters to a web application. In case of a GET request, these parameters are exposed in the URL as name value pairs and in case of POST, parameters are sent within the body of the request. 

Application, Request, Session and Page Scopes in Servlets and JSPs

Similar to scope and lifetime of variables in Java as you have seen in blocks-and-methods-in-java, parameters and attributes in a Java EE web application also have scope and lifetime in the context of the web application. The scope of a parameter/attribute denotes the availability of that parameter/attribute for use. A web application serves multiple requests from clients when it is up and running. These requests can be from same client or different clients.

Introduction to HTTP - HTTP Methods and HTTP Status Codes

HTTP is a synchronous request-response application network protocol. Client sends a request specifying one of the seven HTTP methods, the location of the resource to be invoked, a set of optional headers and an optional message body. The server sends back a response containing the version of HTTP we are using, a response code, a description of the response code, a set of optional headers, and an optional message body. HTTP is the primary protocol for most web applications on the Internet, regardless of whether they are written in Java, PHP or ASP. 

Introduction to Servlet Filters

Filters are pluggable classes that stand between the client and a target component (like a servlet or JSP), within a web application. We can do pre or post processing of request/response data while it is coming from client to a servlet or from the servlet back to a client. Before filters were introduced in Java EE, we had to use servlets with RequestDispatcher to achieve pre or post processing of request/response data. Filters add a lot of flexibility over servlet for such extra processing which is not directly involved in response creation.

HttpServletRequest and HttpServletResponse in Servlets

There are four main entities involved in the HTTP request-response model:

  1. Client
  2. Web container
  3. Request
  4. Response

We will learn about request and response here.


Container provides us with two objects corresponding to request and response, encapsulating the request details and an empty response object when a request is received. These object interfaces also contain many convenience methods. These are passed through the service method of the servlet, which will then pass to us through the doXXX methods.


List of useful methods of ServletRequest and HttpServletRequest

ServletRequest Interface

Below methods work with the metadata properties carried by requests:

  1. String getCharacterEncoding()

  2. Int getContentLength()

  3. String getContentType()

  4. Enumeration getLocales()

  5. Locale getLocale()

  6. String getProtocol () E.g. HTTP/1.1

  7. String getScheme() E.g.: http, https, ftp etc.

List of useful methods of ServletResponse and HttpServletResponse

ServletResponse Interface

ServletResponse encapsulates basic properties of a response independent of protocol and provide output streams to write content to client, independent of protocol.

There are two response streams

  1. for binary data

  2. for character data


  • Only one of these two may be used or will get IllegalStateException.


Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within!!!