wt-unit5
wt-unit5
Unit 5 2024-25
B.TECH.(CSIT) SEMESTER -V
1: Servlets: Servlet Overview and Architecture, Interface Servlet and the Servlet Life Cycle
2. Servlets handling HTTP get Requests, Handling HTTP post Requests, Redirecting Requests to Other
3. Java Server Pages (JSP): Introduction, Java Server Pages Overview, A First Java Server Page
Example, Implicit Objects, Scripting, Standard Actions, Directives, Custom Tag Libraries
Faculty
Dr. Aadarsh Malviya
(Associate Professor Department of CSE)
Affiliated to
1. Servlets: Servlet Overview and Architecture, Interface Servlet and the Servlet Life Cycle
Servlets are a key component in Java web development, acting as server-side programs designed to handle client
requests, process them, and respond dynamically. They are part of the Java EE (Enterprise Edition) platform,
providing an efficient way to develop dynamic web applications.
1. Servlet Overview and Architecture
Overview:
A servlet is a Java class used to extend the capabilities of servers that host applications accessed by web
clients. Servlets can respond to any type of requests, but they are commonly used to handle HTTP requests
in web applications.
They run on a servlet container (like Apache Tomcat or Jetty) within a web server or an application server.
The container manages the lifecycle, security, and resource management of servlets.
Architecture:
Servlets follow a request-response model, typically for HTTP requests and responses. They integrate with
other Java technologies (like JSP and EJB) to create a multi-tiered, scalable application.
The Servlet Container manages servlet instances, handles client requests, ensures proper resource
allocation, and provides essential services (e.g., multithreading, concurrency handling, request/response
management, and session management).
2. Interface Servlet and the Servlet Life Cycle
Interface Servlet:
The javax.servlet.Servlet interface is the core interface in the servlet API. Every servlet must implement
this interface (either directly or through the HttpServlet class).
This interface defines five essential methods:
o init(ServletConfig config): Called once when the servlet is first loaded into memory. Used for
initializing resources or configuration needed by the servlet.
o service(ServletRequest request, ServletResponse response): Called each time the server receives a
request for this servlet. This method handles the client request and generates a response.
o destroy(): Called when the servlet is about to be removed from memory, allowing it to release
resources.
o getServletConfig(): Returns configuration details for this servlet, mainly useful within the init
method.
o getServletInfo(): Returns information about the servlet, such as author, version, or other details.
Servlet Life Cycle:
The servlet lifecycle is defined by three main stages:
1. Initialization (init method):
o When a servlet is first requested, it is loaded into memory, and the container calls the init() method
once to initialize the servlet.
o Any resources or configurations needed for processing requests are set up here.
2. Request Handling (service method):
o For each client request, the servlet container calls the service() method of the servlet.
o service() identifies the type of request (like GET, POST, PUT, DELETE in HttpServlet) and
delegates to corresponding methods (doGet(), doPost(), etc., in HttpServlet subclasses).
o This is the main method where business logic is executed, and responses are generated and sent
back to the client.
3. Termination (destroy method):
o When a servlet is no longer needed or the server is shutting down, the container calls the destroy()
method.
o In destroy(), any resources such as database connections or open files are released to prevent
memory leaks.
Example Servlet Life Cycle Flow:
1. Servlet class is loaded and instantiated.
2. init() method is invoked to initialize the servlet.
1
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
2. Servlets handling HTTP get Requests, Handling HTTP post Requests, Redirecting Requests to Other
Resources, Session Tracking, Cookies, Session Tracking with Http Session
Servlets are widely used in Java web applications for handling HTTP requests, managing session information, and
ensuring secure communication between clients and servers. Here’s a detailed look at how servlets handle GET
and POST requests, redirect requests, and manage session tracking using cookies and the HttpSession API.
1. Handling HTTP GET Requests
HTTP GET requests are typically used for retrieving information from the server without modifying any data. In
servlets, GET requests are handled by overriding the doGet() method in the HttpServlet class.
Example:
java
Copy code
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
2
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
3
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
}
}
b. Session Tracking with HttpSession
HttpSession provides a straightforward way to store session information on the server. Each client has a unique
session ID associated with it.
Example:
java
Copy code
import javax.servlet.http.HttpSession;
Java Server Pages (JSP): Introduction, Java Server Pages Overview, A First Java Server Page Example, Implicit
Objects, Scripting, Standard Actions, Directives, Custom Tag Libraries
JavaServer Pages (JSP) are a technology in Java used for developing dynamic web content. They allow
embedding Java code directly into HTML pages, making it easier to develop web applications with a mixture of
static and dynamic content. JSP pages are compiled into servlets by the server and managed within a servlet
container like Tomcat.
1. Introduction to JavaServer Pages (JSP)
JSPs simplify web development by separating the business logic (in Java classes) from the presentation
layer (in JSP files).
They are designed to be more accessible to web designers by allowing HTML and Java code to be
intermingled.
JSP pages have a .jsp file extension and are translated by the server into servlets before execution.
2. JavaServer Pages Overview
JSP pages work in conjunction with servlets and provide a more readable way of generating dynamic
content.
JSPs use various tags and expressions that allow Java code, expressions, and HTML to coexist.
They support the Model-View-Controller (MVC) architecture by allowing JSP to act as the "view," while
servlets and other Java classes manage the business logic (the "controller" and "model").
3. A First Java Server Page Example
4
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
5
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]
jsp
Copy code
<%! int myVariable = 5; %>
6. Standard Actions in JSP
JSP provides standard actions to control the behavior of the page and perform operations such as including other
resources or forwarding requests:
<jsp:include page="..."/>: Includes content from another resource at runtime.
<jsp:forward page="..."/>: Forwards the request to another page or servlet.
<jsp:param name="..." value="..."/>: Used within <jsp:include> or <jsp:forward> to pass parameters.
<jsp:useBean id="..." class="..." />: Instantiates or locates a JavaBean.
<jsp:setProperty ... />: Sets properties on a JavaBean.
<jsp:getProperty ... />: Retrieves properties from a JavaBean.
7. Directives in JSP
Directives provide global settings for a JSP page and control how the JSP is processed:
Page Directive (<%@ page ... %>): Sets page-level attributes, such as contentType, language, and
errorPage.
jsp
Copy code
<%@ page contentType="text/html" language="java" %>
Include Directive (<%@ include file="..." %>): Includes content from another file at translation time
(static include).
jsp
Copy code
<%@ include file="header.jsp" %>
Taglib Directive (<%@ taglib uri="..." prefix="..." %>): Declares a custom tag library, allowing usage
of custom tags in JSP.
jsp
Copy code
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
8. Custom Tag Libraries
Custom tag libraries (taglibs) allow developers to define their own tags to encapsulate complex functionality and
reuse it across JSPs. Tag libraries make JSP pages cleaner and more modular.
Java Standard Tag Library (JSTL): A set of commonly used custom tags provided by Java, including
tags for core operations, formatting, SQL, and XML processing.
o Core Tag Library: Commonly used tags like <c:if>, <c:forEach>, and <c:choose> provide
conditional processing and iteration.
o Example of using JSTL to loop over a list:
jsp
Copy code
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach var="item" items="${myList}">
<p>${item}</p>
</c:forEach>
Custom Tags: Developers can create their own tag libraries by implementing Java classes and defining
the tags in a tag library descriptor (TLD) file.
Summary of JSP Key Features
Scripting Elements: Enable dynamic content with scriptlets, expressions, and declarations.
Standard Actions: Perform tasks like including content and working with JavaBeans.
Directives: Control page settings, include files, and define tag libraries.
Custom Tag Libraries: Reusable components that simplify JSP code and enhance readability.
JSP technology facilitates a clean MVC architecture, where servlets handle business logic, and JSP files provide
6
Web Technology (BCS502) 2024-25 (Odd Semester) [ Dr. Aadarsh Malviya DGI]