Servlet and JSP.

Surabhibadal
3 min readMay 31, 2021

--

Websites are collections of static files like text, images, graphics, and HTML files. These websites can be called web applications if they provide dynamic functions when hosted on the servers. So the role of Servlet and JSP is to make web applications.

Servlet

Servlets are server-side programs written in java which is used to develop enterprise application or web application. Servlet is a pre-defined interface present in javax.Servlet API.

When the client(browser) sends a request to a server, the server then sends it to the web container, then the web container forwards it to the corresponding servlet using the deployment descriptor.

Deployment descriptor — It does the mapping between URL and servlet. The web.xml file is the deployment descriptor.

Steps to create a Servlet Application

  1. Create a servlet(java) program.
  2. Set the path of Servlet_api.jar.
  3. Compile the java program.
  4. Create a client-side page(index.html).
  5. Create a directory structure.
  6. Create a deployment descriptor.
  7. Copy entire web application into web root (tomcat/webapps).
  8. Start the web server.
  9. Open the browser and execute the client-side application.

Life cycle of servlet.

  1. Loading a servlet.
  2. Servlet initalization.
  3. Request processing or service method provocation.
  4. Destroy the servlet.

Inter-Servlet Communication

  1. Using RequestDispatcher — The RequestDispatcher Interface is used for dispatching the request to a resource i.e Html, servlet, or JSP. The Contents of another resource can be included in this interface. There are two methods of RequestDispatcher forward() and include().
  2. Using sendRedirect() — This method redirects the response to another resource. This method actually makes the client(browser) create a new request to get to the resource.

JSP — Java Server Pages

Servlets is very good at business logic but not good for presentation. JSP is only an enhancement of servlet. Java code in HTML is known as JSP. Java provides 5 predefined tags and 9 implicit objects to develop JSP application.

Every JSP is internally a servlet ie: the JSP engine is responsible to convert the .jsp file to .java file. After converting the . java file servlet container is responsible to execute other steps.

JSP Life Cycle

Pre-defined Tags

  1. Scriplet: Scriplets are basically used to write a pure java code. Whatever the java code we write as a part of scriplet, that code will be available as a part of service () method of the servlet. <% …………..%>

2. Expression: Expression tags are used for writing the java valid expressions as a part of the JSP page. All the expression we write in expression tag they will be placed automatically in out.println () method and this method is available as a part of the service method.

3. Declaration: Whenever we use any variables as a part of JSP we have to use those variables in the form of a declaration tag i.e., declaration tag is used for declaring the variables on the JSP page. <%! ………………..%>

4. Directive: The directive tag is used to include JSP files or import a package into our current file. <%@ page attribute=value %>

5. Action: An action tag is used to control the flow between pages.

Implicit object in JSP

  1. request : HttpServletRequest
  2. response : HttpServletResponse.
  3. out : jspwriter~ PrintWriter object
  4. sessio : HttpSession
  5. application: ServletContext
  6. config : ServletConfig
  7. pagecontext: pageContext

In conclusion, Servlets are server-side programs and take care of the processing, whereas JSP is an interface that is built on top of Servlets to provide added functionalities.

--

--

Surabhibadal
Surabhibadal

Written by Surabhibadal

0 Followers

Software Developer

No responses yet