Servlet And JSP

surya mdl
4 min readMar 7, 2021

To understand about servlet and JSP , we should know about client and server communication.

Client and Server communication

Clients are the service requesters whereas Servers are service providers.

Client sends the request to the server and server fetches the data for that request and sends it to client.

There are two types of pages : Static page and Dynamic page

Static page : When client sends request to server and the required data is there in server and is displayed to client, then that page is called static page.

Dynamic Page : When client sends the request to server, some operations to be performed on request data for producing output then that page is called dynamic page. Here comes the use of servlet and JSP .

Process of producing dynamic page

In server there is web container which contains web.xml file that contains the mappings of url and servlet. When client sends the request to server, with the use of web.xml file respective servlet is called and operation is performed on request and response is sent to client.

So servlet is a small Java program that runs within a web server which receives requests and send response to web clients.

We can create servlet in java by simply creating the class extending HttpServlet class.

The operation done on request should be written in service method whose parameters are objects of HttpServletRequest and HttpServletResponse.

Lifecycle of servlet

  1. The classloader is responsible to load the servlet class. The servlet class is loaded when the first request for the servlet is received by the web container.
  2. The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle.
  3. The web container calls the init method only once after creating the servlet instance. The init method is used to initialize the servlet.
  4. The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once.
  5. The web container calls the destroy method before removing the servlet instance from the service.
  • We can call a servlet from another servlet using requestdispatcher and sendredirect

RequestDispatcher

RequestDispatcher is an interface which provides the methods for sending request from one servlet to another servlet.

The methods in RequestDispatcher interface are

  1. forward() -> sends the request object from first servlet to second servlet and the response of second servlet is sent to the client.
  2. include()-> sends the request object from first servlet to second servlet and the combined response of first and second servlet is sent to the client.

sendRedirect()

The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another servlet.

Here the response will be sent from first server and new request is sent to second server from client,that is why it is client side.

Session and Cookie

  • Not only request object can be sent from one servlet to another servlet but also other values can also be sent.
  • This is done in three ways .
  • They are using HttpSession object,Cookie object,url rewriting .
  • Session is an object that remains between user login and logout.
  • When we add any value to session it can be accessed by any servlet called in that session,like that it is used to transfer data from one servlet to another servlet.

Filters

Filters are used when same code is repeated in different servlets.To avoid rewriting code filters are used.That filters are placed in servlets.

When an request is sent from client, first it is passed through filters in that servlet , if the request satisfies the validation then it is sent to servlet,if not response is sent from respective filter to client.So it is also used for validation purpose.

JSP

JSP means java server pages where we can both design and define the working of web page.

Use Of JSP

Writing html tags inside servlet is difficult for that jsp becomes handy.

We can write java code in html page. For that code to be simple, JSP provides some implicit objects which are to be explicitly defined in servlets.They are created by web container.

  • out -> to print on web page
  • request -> object of HttpServletRequest class contains the data sent by the client
  • response ->object of HttpServletResponse class contains the data received by client
  • session -> object of HttpSession interface that contains the data added in session can be accesed by multiple servlets.
  • config -> object of ServletConfig used to initialize jsp page.It can be created for each jsp page in a project.
  • context -> object of ServletContext used to initialize.It is created only once for a project.
  • pageContext -> object of pageContext class used to set,get,remove attributes from page,request,session,application scopes.
  • page -> object of Object class.
  • exception -> object of java.lang.Throwable class,used to print the exception .It is used in error pages(which are called when an error is occured).

JSP Tags

There are 4 tags in JSP for different uses

  • Scriptlets -> java code is written in scriptlets.
  • Declaration ->variables are declared in this section.
  • Expression ->The value to be printed on web page is written in this section
  • Directive -> Importing packages is written in this section.

--

--