Servlet and JSP in JAVA ..

RAKESH KUMAR SINGH
4 min readMay 30, 2021

when we open any website in our mobile or computer then as a client if we make any request to website then that request will go to server. If our request is for static web page then server will return that static page directly but what happen if we make request for dynamic web page ?

If client make a request which needs to formed dynamically then server send that request to web container like tomcat. In web container there will many servlet for making dynamic web page and In server there is deployment descriptor file(web.xml) in which mapping is present which is responsible for calling a particular servlet for a particular request.

HTTP (Hypertext Transfer Protocol)

  • HTTP is a protocol that clients and servers on the web to communicate.
  • It is similar to other internet protocols such as SMTP (Simple Mail Transfer Protocol) and FTP (File Transfer Protocol.
  • HTTP is a stateless protocol i.e it supports only one request per connection. This means that with HTTP the clients connect to the server to send one request and then disconnect. This mechanism allows more users to connect to a given server over a period of time.
  • The client sends an HTTP request and the server answers with an HTML page to the client, using HTTP.

The HTTP request can be made using a variety of methods, but the ones which we use widely are Get and Post. The method name itself tells the server the kind of request that is being made, and how the rest of the message will be formatted.

Now, with the help of the below table, let’s understand the difference between Get and Post methods of HTTP.

Servlet :

It is basically a java program which extends class HttpServlet and responsible for creating dynamic web page. When any servlet will called then service method inside servlet will called by tomcat .

In this figure you can see, a client sends a request to the server and the server generates the response, analyses it and sends the response to the client.

Example-

package com.rakesh;

import javax.servlet.HttpServlet;

public class MyServlet extends HttpServlet {

public void service(HttpServletRequest req, HttpServletResponse res){

// code…

}

}

The Servlet Life Cycle :

The life-cycle of a servlet is managed by the Servlet container. The life-cycle methods for the servlet interface are the init(), destroy() and service() methods.

The init() method is first called when the servlet is initialized, then the service() method to handle the clients request and then the destroy() method is called once during the end of the servlet life-cycle.

The configuration method for the servlet interface include serverletConfig() and getServerletInfo() methods. As the names suggest, the former is used to get configuration info about the servlet from the web.xml file and the latter is used to get information about the servlet itself like its name or copyright notice.

The service() method is where the main work is done using the two params, request and response. It is worth noting that we do not implement the servlet interface directly, but there are other implementations for the interface provided for us to work with.

JSP(Java Server Page) :

When client make request for any dynamic web page then we simply use servlet and after processing send back response to client but let us suppose we have to do some CSS thing in that web page then we have to send that HTML code tag by tag from servlet(java program) which is not a good idea in case of complex project , So removing this kind of difficulty Jsp came.

Jsp is like a HTML page in which we can add our java code. When any front end designer has to look at backend code then after seeing java code they may not be comfortable, But if we use Jsp then we can make it like HTML by using JSTL.

Now the question is if Jsp is that much easier then why we learned about servlet we can simply use Jsp? We have to know about servlet because at the end every Jsp file will automatically converted to servlet. That’s why when Jsp file executed first time then it is a bit slow due to conversion.

JSP to Servlet Conversion :

Java code which we write in Jsp file is divided in four tags -

  1. Directive tag: This tag is used to declare anything that is used by the whole page like if we want to import any file, include any Jsp file or include any Jsp standard tag library.

Syntax: <%@ code%>

2. Declarative tag: This tag is used to declare anything outside the service method but inside the servlet class.

Syntax: <%! code%>

3. Script tag: All the code which we write in this tag will go to inside service method of servlet class.

Syntax: <% code%>

4. Expression tag: In Jsp if we want to print something directly on screen then we use this tag , all the code written inside this tag will go to out.println after converting.

Syntax: <%= code%>

--

--