JSP and Servlet

Bharath K C
3 min readMay 31, 2021

--

Java Server Pages(JSP) is a Java technology that enables developers to create dynamic web pages. A Servlet is a class that runs in a webserver. Servlets receive and respond to requests from web clients.

Let’s consider a user(client) sends a request asking for a page if the page already present in the server that page goes to the user as a response. If the page is not present and has to be built at runtime then it’s called a Dynamic web page.

Client-server Architecture

Dynamic Web Page

The user sends a request asking for a web page that request goes to the web container at the server-side. The web container contains Servlets. Servlets take the requests and process the information and send the responses back to the user. For different requests, different Servlets will be called, this mapping is done by Deployment Descriptor(web.xml) present in the web container. Servlets are normal classes created by extending HttpServlet which is an abstract class and provides all functionalities required for servlets to process the requests and send responses back.

Life Cycle of a Servlet

The life cycle of a Servlet is managed by the Web Container. There are four stages in the Servlet life cycle. Loading a Servlet, Initializing the Servlet, Request handling, and Destroying the Servlet.

Servlet Life Cycle Methods:

init() method: The init() method is called when a Servlet Instance is Intantiated and ready to put into service.

service() method: This method is called when there is a client request. ServletRequest object is used to collect the data requested by the client. ServletRespone object is used to generate the output content.

destroy() method: This method destroys the instance of a servlet.

Building Dynamic Webpage

<!DOCTYPE html><html>
<body>
<form action="add" method="get">
Enter first number : <input type="text" name="num1"> <br>
Enter second number : <input type="text" name="num2"> <br>
<input type="submit">
</form>
</body>
</html>

The above code displays a webpage for two numbers with a submit button. The below image is displayed on the browser.

Web page to add two numbers

Let’s consider the user fills the above page and sends a request(clicks on submit). The Servlet that adds two numbers needs to be created and the user request needs to be mapped in a web.xml file to call this Servlet. The Servlet is created like a normal class but it extends HttpServlet.

The servlet mapping in web.xml<servlet>
<servlet-name>AddServlet</servlet-name>
<servlet-class>com.kcb.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddServlet</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>

The code above shows mapping in the web.xml page. <servlet-name> is used because when there are multiple mappings, servlets can be identified by names. When there is add request (/add) the Addservlet is called(com.kcb.AddServlet). The add is a tag given in the HTML page that accepts the user request.

// Servlet to Add Two numbers
package com.kcb;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class AddServlet extends HttpServlet {public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {

int i = Integer.parseInt(req.getParameter("num1"));
int j = Integer.parseInt(req.getParameter("num2"));
int k = i + j;

PrintWriter out = res.getWriter();
out.println("Result is "+k);
}
}

The above code shows AddServlet. The service() method takes the input from the Request object and processes it(as you can see in the above code) the output is displayed on the page with the help Response object.

get and doGET(): get is an HTTP method used to fetch data from a webpage. The service() method internally calls doGET() method if the request is HTTP get method.

post and doPOST(): post is an HTTP method used to post some data onto a webpage. The data passing through the post method is not displayed on URI. The service() method calls doPOST() method to handle requests of type post.

Conclusion

JSP & Servlets is a vast topic. This article provides information about few basic terms in building a dynamic web page and a simple example of adding two numbers by using Servlet. Refer to this for more information. https://www.youtube.com/watch?v=OuBUUkQfBYM

--

--