Servlet And JSP

Sudhanshu Paliwal
4 min readMar 7, 2021

In this blog we are going to learn about servlets and jsp and what is the requirement of them.

Photo by Desola Lanre-Ologun on Unsplash

Need of Servlet and JSP

We are aware that in today’s world we need a dynamic website that displays content based on the request received from the client-side. So if you are using Java as your back-end language you can implement this using servlets and JSP. Servlet gets a request from the server, processes it, produces a response, and sends it back to the server.

Servlet

Servlet is a java program that receives a request from the web client and processes it and sends a response back across a web protocol.

You can create a servlet by extending GenericServlet or HttpServlet. This interface characterizes techniques to introduce a servlet, to support demands, and to eliminate a servlet from the worker.

Life Cycle Of Servlet

  1. A servlet is created, then initialized.
  2. Calls from the client are handled and sent back a response.
  3. Servlet is taken out of service, then destroyed and garbage collected.

Methods Of Servlet

1. init()

The init method is called only once if the instance of the servlet required does not exist already. It is done through a web container by loading the servlet class, creating an instance, and initializing it.

The init method must be completed successfully before the servlet starts receiving any requests.

2.service()

It is called after the servlet init method has been completed successfully. It handles the HTTP request types like do, post, put, delete, and then calls the appropriate methods.

3.destroy()

It is called by the container to take the servlet out of service. It is only called after all the threads executing the service method have exited or a timeout period has occurred. After this container will not call the service method again.

Example Of Servlet

Here we are creating a Servlet named as ContactUsFormServlet which is intercepting an HTTP request which in this case is a post request.
So the data of the contact us form is stored in the request object. So first we create an object of the ContactUsDao which is a Data Object Class which do the storing of the data in our database. Then after completing the required task we send a response back to the client. Here we are redirecting to the index.JSP.
Now for the web-mapping is important so we can target our servlet at a specific URL.
We can achieve this either by a @WebServlet annotation or by adding the mapping in the web.xml file.

JSP

JSP stands for the Java Server Pages. It is a server-side technology and used for the creation of web appa]s and dynamic content pages.

JSP is different from the servlet as here we embed the Java code using scripting tags like

  1. <% — comment — %> To add comments
  2. <%@ directive %> To import classes and include pages
  3. <%! declarations %> declare variables in class outside service method
  4. <% scriplet %> To add Java code
  5. <%= expression %>To print Java expressions

LifeCycle Of JSP

  1. Converting of JSP page into the servlet .
  2. Loading the servlet class and creating a servlet instance.
  3. Initializing the servlet, processing the request and destroying the servlet.

Example Of JSP

Here we are creating a JSP page named login.JSP which basically is a login page for the user and will send the request to a Servlet named as AuthenticatorServlet which checks the login credentials and sends a response basis on which the page is redirected to show the data page or refreshed to the current page.
Since it is a basic page so here we didn’t have a scriptlet but based on your need you can add it. Here we have added the contentType and language for the page using <%@ page %> and comment using the <% — — %> tag.
Then we have created a login form of method post and specified the URL in action.

--

--