0% found this document useful (0 votes)
5 views54 pages

Servlets 1

The document provides an overview of the client-server model, detailing the roles of clients and servers, advantages and disadvantages of this architecture, and the types of servers including application and web servers. It explains Java Servlets, their lifecycle, and how they handle requests and responses within a web application. Additionally, it covers the Servlet API, including key interfaces and classes, and outlines the installation prerequisites for setting up a servlet environment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views54 pages

Servlets 1

The document provides an overview of the client-server model, detailing the roles of clients and servers, advantages and disadvantages of this architecture, and the types of servers including application and web servers. It explains Java Servlets, their lifecycle, and how they handle requests and responses within a web application. Additionally, it covers the Servlet API, including key interfaces and classes, and outlines the installation prerequisites for setting up a servlet environment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Servlets

Client Server Model


Client Server Model
 Client: A client is a program that runs on the local machine requesting service from the server. A client
program is a finite program, which means that the service is started by the user and terminates when
the service is completed.
 Server: A server is a program that runs on the remote machine providing services to the clients. When
the client requests for a service, then the server opens the door for the incoming requests, but it never
initiates the service.
 A server program is an infinite program, which means that when it starts, it runs infinitely unless a
problem arises. The server waits for the incoming requests from the clients. When the request arrives at
the server, then it responds to the request.
 The Client-server model is a distributed application structure that partitions tasks or workloads between
the providers of a resource or service, called servers.
 and service requesters called clients
 In the client-server architecture, when the client computer sends a request for data to the server through
the internet the server accepts the requested process and delivers the data packets requested back to
the client
 Clients do not share any of their resources
 Examples of the Client-Server Model are Email, World Wide Web, etc.
Advantages of Client-server
 Centralized: Centralized backup is possible in client-server networks, i.e., all the
data is stored in a server.
 Security: These networks are more secure as all the shared resources are centrally
administered.
 Performance: The use of a dedicated server increases the speed of sharing
resources. This increases the performance of the overall system.
 Scalability: We can increase the number of clients and servers separately, i.e., the
new element can be added, or we can add a new node in a network at any time.
Disadvantages of Client-Server
 Traffic Congestion is a big problem in the Client/Server model. When a large
number of clients send requests to the same server may cause the problem of Traffic
congestion.
 It does not have the robustness of a network, i.e., when the server is down, then the
client requests cannot be met.
 A client/server model is very decisive. Sometimes, regular computer hardware does
not serve a certain number of clients. In such situations, specific hardware is required
on the server side to complete the work.
 Sometimes the resources exist in the server but may not exist in the client. For
example, if the application is on the web, then we cannot take the printout directly on
printers without taking out the print view window on the web.
Server Types
 Application Server:
 An application server is one that is designed to generate dynamic content
 It is a software framework that transforms the data to provide specialized
functionalities offered by a business, service, or application.
 Application servers enhance the interactive parts of a website depending on the
context of the request.
 Application servers contain web containers and EJB containers
 Application servers are entirely responsible for creating an environment for
enterprise applications
 These servers are capable of supporting HTTP as well as RPC/PMI protocols
 Application servers consume more resources like CPU, memory as compared to web
servers.
 Most Application Servers have a Web Server as an integral part, which means an
Application Server can perform all the tasks that a Web Server does.
 Examples : WebLogic, Glassfish, JBoss.
Server Types
 Web Server:
 A Web Server is defined as a server which accepts a request for data and sends
the relevant document in return
 In other words, it is a computer program that accepts a request for a specific
document and sends it to the client machine.
 Web servers are designed to serve HTTP content to the client computer
 In most cases, the web servers are the integral parts of the application servers
 Web servers accept the HTTP requests and interpret them to serve the requested
content
 Although Web Servers are designed to serve static content, most Web Servers have
plugins to support scripting languages like PHP, Perl, etc. through which they can
generate dynamic HTTP content.
Difference between Web Server and
Application Server
What is Java Servlet?
 Java Servlets are the Java programs that run on the Java-enabled web
server or application server
 They are used to handle the request obtained from the web server, process
the request, produce the response,
 and then send a response back to the web server.
 Servlet is a technology which is used to create a web application
 Servlet is an API that provides many interfaces and classes including
documentation
 Servlet is an interface that must be implemented for creating any Servlet.
 Servlet is a class that extends the capabilities of the servers and responds
to the incoming requests. It can respond to any requests.
 Servlet is a web component that is deployed on the server to create a
dynamic web page
Servlet – Life Cycle
 The servlet container maintains the life cycle of a servlet instance. Let's see the life
cycle of the servlet:
 Loading a Servlet.
 Initializing the Servlet.
 Request handling.
 Destroying the Servlet.
Servlet – Life Cycle
 Loading a Servlet :
 The classloader is responsible for loading the servlet class.
 The servlet class is loaded when the first request for the servlet is received by the web
container.

 Initializing the Servlet:


 The web container creates the instance of a servlet after loading the servlet class.
 The web container calls the init method only once after creating the servlet instance.
 The init method is used to initialize the servlet. It is the life cycle method of the
javax.servlet.Servlet interface. Syntax of the init method is given below:
 public void init(ServletConfig config) throws ServletException
 If the Servlet fails to initialize, then it informs the Servlet container by throwing
the ServletException or UnavailableException
 The servlet instance is created only once in the servlet life cycle.
Servlet – Life Cycle
 Request handling:
 After initialization, the Servlet instance is ready to serve the client requests
 The Servlet container performs the following operations when the Servlet instance is located
to service a request :
 It creates the ServletRequest and ServletResponse objects
 In case, it is an HTTP request then the Servlet container
creates HttpServletRequest and HttpServletResponse objects which are subtypes of
the ServletRequest and ServletResponse objects respectively
 After creating the request and response objects it invokes the Servlet.service(ServletRequest,
ServletResponse) method by passing the request and response objects.
 The service() method while processing the request may throw
the ServletException or UnavailableException or IOException
 Syntax :
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
Servlet – Life Cycle
 Destroying the Servlet
 When a Servlet container decides to destroy the Servlet, it performs the following operations,
 It allows all the threads currently running in the service method of the Servlet instance to complete
their jobs and get released.
 After currently running threads have completed their jobs, the Servlet container calls
the destroy() method on the Servlet instance
 After the destroy() method is executed, the Servlet container releases all the references of
this Servlet instance so that it becomes eligible for garbage collection
Servlet –Life Cycle Methods
Servlet –Life Cycle Methods
 There are three life cycle methods of a Servlet :
 init() :
 A servlet’s life begins here
 This method is called only once to load the servlet
 Since it is called only once in it’s lifetime, therefore “connected architecture” code is written inside it
because we only want once to get connected with the database.
 This method receives only one parameter, i.e ServletConfig object
 This method has the possibility to throw the ServletException.
 Once the servlet is initialized, it is ready to handle the client request.
 The prototype for the init() method
 public void init(ServletConfig con)throws ServletException{
Servlet –Life Cycle Methods
 service()
 The service() method is the most important method to perform that provides the connection between
client and server.
 The web server calls the service() method to handle requests coming from the client( web browsers)
and to send responses back to the client.
 This method determines the type of HTTP request (GET, POST, PUT, DELETE, etc.).
 This method also calls various other methods such as doGet(), doPost(), doPut(), doDelete(), etc. as
required.
 This method accepts two parameters.
 The prototype for this method 
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException
{}

 destroy()
Servlet –Life Cycle Methods
 destroy()
 The destroy() method is called only once.
 It is called at the end of the life cycle of the servlet.
 This method performs various tasks such as closing connection with the database, releasing memory
allocated to the servlet, releasing resources that are allocated to the servlet and other cleanup
activities.
 When this method is called, the garbage collector comes into action.
 The prototype for this method is
public void destroy() { // Finalization code...}
How Servlets work?
1. When the web server (e.g. Apache Tomcat) starts up, the servlet container deploy and
loads all the servlets. During this step Servlet container creates ServletContext
object. ServletContext is an interface that defines the set of methods that a servlet can
use to communicate with the servlet container.
2. Once the servlet is loaded, the servlet container creates the instance of servlet class. For
each instantiated servlet, its init() method is invoked.
3. Client (user browser) sends an Http request to web server on a certain port. Each time
the web server receives a request, the servlet container creates HttpServletRequest and
HttpServletResponse objects. The HttpServletRequest object provides the access to the
request information and the HttpServletResponse object allows us to format and change
the http response before sending it to the client.
4. The servlet container spawns a new thread that calls service() method for each client
request. The service() method dispatches the request to the correct handler
method based on the type of request.
For example if server receives a Get Request the service() method would dispatch the
request to the doGet() method by calling the doGet() method with request parameters.
Similarly the requests like Post, Head, Put etc. are dispatched to the corresponding
handlers doPost(), doHead(), doPut() etc. by service() method of servlet.
How Servlets work?

5. When servlet container shuts down, it unloads all the servlets and calls
destroy() method for each initialized servlets.
How web container handles the
Servlet request
The web container is responsible for handling the client request
1. maps the request with the servlet in the web.xml file.
2. creates request and response objects for this request
3. calls the service method on the thread
4. The public service method internally calls the protected service method
5. The protected service method calls the doGet method depending on the type
of request
6. The doGet method generates the response and it is passed to the client.
7. After sending the response, the web container deletes the request and
response objects. The thread is contained in the thread pool or deleted
depends on the server implementation.
Client-Server Model setup
 Pre-requisites
 JDK
 Maven -- > https://www.geeksforgeeks.org/how-to-install-apache-maven-on-windows/
 Tomcat 9  See next slide
 Vscode and Extensions for maven and Tomcat servers  follow instructions in class
Tomcat Installation Guide
 Follow the steps mentioned here :
 Using zip
https://www.geeksforgeeks.org/how-to-install-apache-tomcat-on-windows/
 Using installer
https://www.liquidweb.com/blog/installing-tomcat-9-on-windows/
Introduction to Servlet API
 The javax.servlet and javax.servlet.http packages represent
interfaces and classes for servlet api.
 The javax.servlet package contains many interfaces and classes that
are used by the servlet or web container. These are not specific to any
protocol.
 The javax.servlet.http package contains interfaces and classes that
are responsible for http requests only.
Interfaces in javax.servlet package

 There are many interfaces in javax.servlet package. They are as follows:


1. Servlet
2. ServletRequest
3. ServletResponse
4. RequestDispatcher
5. ServletConfig
6. ServletContext
7. SingleThreadModel
8. Filter
9. FilterConfig
10. FilterChain
11. ServletRequestListener
12. ServletRequestAttributeListener
13. ServletContextListener
14. ServletContextAttributeListener
Classes in javax.servlet package

 There are many classes in javax.servlet package. They are as follows:


1. GenericServlet
2. ServletInputStream
3. ServletOutputStream
4. ServletRequestWrapper
5. ServletResponseWrapper
6. ServletRequestEvent
7. ServletContextEvent
8. ServletRequestAttributeEvent
9. ServletContextAttributeEvent
10. ServletException
11. UnavailableException
Interfaces in javax.servlet.http package

 There are many interfaces in javax.servlet.http package. They are as follows:


1. HttpServletRequest
2. HttpServletResponse
3. HttpSession
4. HttpSessionListener
5. HttpSessionAttributeListener
6. HttpSessionBindingListener
7. HttpSessionActivationListener
8. HttpSessionContext (deprecated now)
Classes in javax.servlet.http package
 There are many classes in javax.servlet.http package. They are as
follows:
1. HttpServlet
2. Cookie
3. HttpServletRequestWrapper
4. HttpServletResponseWrapper
5. HttpSessionEvent
6. HttpSessionBindingEvent
7. HttpUtils (deprecated now)
Servlet Interface

 Servlet interface provides common behavior to all the servlets.


 Servlet interface defines methods that all servlets must implement.
 Servlet interface needs to be implemented for creating any servlet (either
directly or indirectly).
 It provides 3 life cycle methods that are used to initialize the servlet, to service
the requests, and to destroy the servlet and 2 non-life cycle methods.
 5 Methods of Servlet interface
1. public void init(ServletConfig config)
2. public void service(ServletRequest request,ServletResponse response)
3. public void destroy()
4. public ServletConfig getServletConfig()
5. public String getServletInfo()
Servlet Interface

 5 Methods of Servlet interface


1. public void init(ServletConfig config)
 initializes the servlet. It is the life cycle method of servlet and invoked by the web container
only once.
2. public void service(ServletRequest request,ServletResponse response) :
 provides response for the incoming request. It is invoked at each request by the web
container.
3. public void destroy() :
 is invoked only once and indicates that servlet is being destroyed.
4. public ServletConfig getServletConfig()
 returns the object of ServletConfig.
5. public String getServletInfo() :
 returns information about servlet such as writer, copyright, version etc.
GenericServlet class

 GenericServlet class
implements Servlet, ServletConfig and Serializable interfaces.
 It provides the implementation of all the methods of these interfaces except
the service method.
 GenericServlet class can handle any type of request so it is protocol-
independent.
 You may create a generic servlet by inheriting the GenericServlet class and
providing the implementation of the service method.
GenericServlet : Methods

 There are many methods in GenericServlet class. They are as follows:


1. public void init(ServletConfig config) is used to initialize the servlet.
2. public abstract void service(ServletRequest request, ServletResponse response) provides service for the
incoming request. It is invoked at each time when user requests for a servlet.
3. public void destroy() is invoked only once throughout the life cycle and indicates that servlet is being destroyed.
4. public ServletConfig getServletConfig() returns the object of ServletConfig.
5. public String getServletInfo() returns information about servlet such as writer, copyright, version etc.
6. public void init() it is a convenient method for the servlet programmers, now there is no need to call
super.init(config)
7. public ServletContext getServletContext() returns the object of ServletContext.
8. public String getInitParameter(String name) returns the parameter value for the given parameter name.
9. public Enumeration getInitParameterNames() returns all the parameters defined in the web.xml file.
10. public String getServletName() returns the name of the servlet object.
11. public void log(String msg) writes the given message in the servlet log file.
12. public void log(String msg,Throwable t) writes the explanatory message in the servlet log file and a stack
trace.
HttpServlet class
 The HttpServlet class extends the GenericServlet class and
implements Serializable interface.
 It provides http specific methods such as doGet, doPost, doHead,
doTrace etc.
HttpServlet : Methods

 There are many methods in HttpServlet class. They are as follows:


1. public void service(ServletRequest req,ServletResponse res)
 dispatches the request to the protected service method by converting the request and
response object into http type.
2. protected void service(HttpServletRequest req, HttpServletResponse res)
 receives the request from the service method, and dispatches the request to the doXXX()
method depending on the incoming http request type.
3. protected void doGet(HttpServletRequest req, HttpServletResponse res)
 handles the GET request. It is invoked by the web container.
4. protected void doPost(HttpServletRequest req, HttpServletResponse res)
 handles the POST request. It is invoked by the web container.
5. protected void doHead(HttpServletRequest req, HttpServletResponse res)
 handles the HEAD request. It is invoked by the web container.
HttpServlet class
6. protected void doOptions(HttpServletRequest req,
HttpServletResponse res)
 handles the OPTIONS request. It is invoked by the web container.
7. protected void doPut(HttpServletRequest req, HttpServletResponse
res)
 handles the PUT request. It is invoked by the web container.
8. protected void doTrace(HttpServletRequest req,
HttpServletResponse res)
 handles the TRACE request. It is invoked by the web container.
9. protected void doDelete(HttpServletRequest req,
HttpServletResponse res)
 handles the DELETE request. It is invoked by the web container.
10. protected long getLastModified(HttpServletRequest req)
 returns the time when HttpServletRequest was last modified since midnight January
1, 1970 GMT.
Reading Servlet Parameters
 Reading servlet parameters is essential for handling HTTP requests in
Java Servlet applications. Here are some key points to consider when
working with servlet parameters:
 Types of Parameters:
 Request Parameters: Passed from the client to the server as part of the
request, typically from HTML forms or query strings.
 Context Parameters: Application-wide parameters defined in the web.xml
file, available to all servlets
 Initialization Parameters: Servlet-specific parameters, also defined in
web.xml, but only available to a specific servlet.
Reading Request
Parameters:
Use HttpServletRequest methods to read parameter
 String paramValue = request.getParameter("paramName");
 This retrieves the value of a single parameter. If the parameter is missing, it returns null.
 To read multiple values for the same parameter (e.g., from checkboxes):
 String[] values = request.getParameterValues("paramName");
 Request Parameter Handling:
 Parameters are passed as String, even if they represent numbers or other types.
Conversion may be necessary:
 int number = Integer.parseInt(request.getParameter("numberParam"));
 Handling Missing or Invalid Parameters:
 Always check for null to avoid NullPointerException
 Validate input for type and format correctness (e.g., check for numeric values if
expected).
Reading Request Parameters:
 Context Parameters (Application-wide):
 Defined in web.xml using <context-param> tag
<context-param>
<param-name>configParam</param-name>
<param-value>value</param-value>
</context-param>
 Accessed via the ServletContext object
 String value = getServletContext().getInitParameter("configParam");
Reading Request Parameters:
 Initialization Parameters (Servlet-specific):
 Defined in web.xml using <init-param> tag under a specific servlet definition
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.MyServlet</servlet-class>
<init-param>
<param-name>initParam</param-name>
<param-value>value</param-value>
</init-param>
</servlet>
 Accessed using the ServletConfig object
 String value = getServletConfig().getInitParameter("initParam");
Reading Request Parameters:
 Handling Multi-valued Parameters:
 When multiple values for the same parameter exist, like from multiple checkboxes,
use
 String[] paramValues = request.getParameterValues("paramName");
 Query String Parameters:
 If parameters are passed via a query string (e.g., ?key1=value1&key2=value2), they
can also be read using getParameter() methods.
 Form Encoding:
 Ensure correct encoding is set to avoid issues with special characters, particularly for
non-ASCII input:
 request.setCharacterEncoding("UTF-8");
 Request Parameter Map:
 Retrieve all parameters as a Map of parameter names and values
 Map<String, String[]> parameterMap = request.getParameterMap();
Handling HTTP Requests and Responses

 Creating a simple Hello World Servlet


 Creating a servlet that reads and forms data and processes
https://www.geeksforgeeks.org/servlet-form-data/
 Enhancing the second task to save data into DB and retrieving the
data back from DB and sending back the response
Difference between Servlet, Generic Servlet and
HttpServlet
Slno Servlet GenericServlet HttpServlet
1 An interface An abstract class An abstract class

Defines life cycles methods of Servlet.


Also defines 2 more methods to
retrieve servlet Config and context
2 objects Implements servlet interface Inherits GenericServlet

Provides implemetation details


init(ServletConfig config) , for life cycle methods
destroy(), init(),destroy() and
service(ServletRequest req, getServletConfig() and
ServletResponse res), getServletInfo() method and
getServletConfig(), service method is marked as Provides implementation
3 getServletInfo() abstract for Service method
4 Interface Base class Child class
Difference between Servlet, Generic Servlet and
HttpServlet
Slno Servlet GenericServlet HttpServlet
This is protocol
5 This is protocol independent servlet This is Http specific servlet independent servlet
To create a custom http
servlet, one has to inherit
HttpServlet class and
To create a custom protocol provide implemention for
independent servlet , one has to any of the below methods
To create a custom protocol inherit GenericServlet clas and based on the request type.
independent servlet , one has to inherit provide implementatiom for doGet() ,
GenericServlet clas and provide service() method doPost(),doDelete() ,
6 implementatiom for service() method doPut() etc

The service method in this class The service method in this


The service method in this class is takes ServletRequest and class is abstract and takes
abstract and takes in input parameters ServletResponse object and in input parameters as
as ServletRequest and ServletResponse dispatches client requests to the ServletRequest and
7 objects protected service method ServletResponse objects
Difference between Servlet, Generic Servlet and HttpServlet

Slno Servlet GenericServlet HttpServlet


public abstract void
service(ServletRequest public void service(ServletRequest
req,ServletResponse res) throws req,ServletResponse res)throws
8 ServletException,java.io.IOException ServletException,java.io.IOException
This class defines another service method
which is protected and takes in
HttpServletRequest and HttpServletResponse as
input which receives standard HTTP requests
from the public service method and dispatches
them to the doXXX methods defined in this
9 class
protected void service(HttpServletRequest req,
HttpServletResponse resp) throws
10 ServletException,java.io.IOException
Difference between ServletRequest vs HttpServletRequest
ServletRequest HttpServletRequest
This is an Interface This is also an Interface but extends ServletRequest

Defines an object to provide client request Defines an object to provide client request
information to a servlet information to a servlet
The servlet container creates The servlet container creates
a ServletRequest object and passes it as an an HttpServletRequest object and passes it as an
argument to the servlet's service method. argument to the servlet's service methods
(doGet, doPost, etc).
A ServletRequest object provides data including A HttpServletRequest object also provides data
parameter name and values, attributes, and an including parameter name and values, attributes,
input stream and an input stream

Interfaces that extend ServletRequest can provide Extends the ServletRequest interface to provide
additional protocol-specific data request information for HTTP servlets
No session management api’s defined Session management api are defined in this class
like cookie , HttpSession
Difference between ServletResponse vs
HttpServletResponse
ServletResponse HttpServletResponse
This is an Interface This is also an Interface but extends
ServletResponse
Defines an object to assist a servlet in sending a Extends the ServletResponse interface to provide
response to the client. HTTP-specific functionality in sending a response.
For example, it has methods to access HTTP
headers and cookies.
The servlet container creates The servlet container creates
a ServletResponse object and passes it as an an HttpServletResponse object and passes it as an
argument to the servlet's service method argument to the servlet's service methods
(doGet, doPost, etc)
Difference between ServletConfig vs ServletContext
ServletConfig ServletContext
This is an Interface interface
A servlet configuration object used by a servlet Defines a set of methods that a servlet uses to
container to pass information to a servlet during communicate with its servlet container, for
initialization example, to get the MIME type of a file, dispatch
requests, or write to a log file.
ServletConfig is servlet specific There is one context per "web application" per
Java Virtual Machine
The ServletConfig object is contained within the The ServletContext object is contained within
ServletRequest object which the Web server the ServletConfig object, which the Web server
provides the servlet when the client request is provides the servlet when the servlet is initialized
recieved
Parameters of servletConfig are present as name- Parameters of servletContext are present as name-
value pair in <init-param> inside <servlet>. value pair in <context-param> which is outside of
<servlet> and inside <web-app>
ServletConfig object is obtained by ServletContext object is obtained by
getServletConfig() method getServletContext() method.
Use ServletConfig when only one servlet needs Use ServletContext when whole application needs
information shared by it. information shared by it
LAB Assignment 2
1. Create student signup page and Servlet, where student registers using
username ,password , First name , lastname , emailId and USN and saves
data in the database
2. Create a login page and servlet where the user login using username and
password and servlet will validate the input data and retrieves the user
information saved in step 1 and sends back as a response

Note :
3. Create 1 tables User(USN, FN ,LN, emailId, password)
4. Use MySql DB
5. Referencce -> https://www.geeksforgeeks.org/servlet-form-data/
RequestDispatcher
 The RequestDispatcher interface provides the facility of dispatching the request
to another resource it may be html, servlet or jsp.
 This interface can also be used to include the content of another resource also.
 It is one of the way of servlet collaboration.
 There are two methods defined in the RequestDispatcher interface.
 public void forward(ServletRequest request,ServletResponse
response)throws ServletException,java.io.IOException:
 Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the
server.
 public void include(ServletRequest request,ServletResponse
response)throws ServletException,java.io.IOException:
 Includes the content of a resource (servlet, JSP page, or HTML file) in the response.
RequestDispatcher - forward

As you see in the above figure, response of second servlet is sent to the client.
Response of the first servlet is not displayed to the user.
RequestDispatcher - include

As you can see in the above figure, response of second servlet is included in the response
of the first servlet that is being sent to the client.
How to get the object of
 RequestDispatcher
The getRequestDispatcher() method of ServletRequest interface returns the object
of RequestDispatcher

RequestDispatcher rd=request.getRequestDispatcher("servlet2");
servlet2 is the url-pattern of the second servlet
 rd.forward(request, response);
 rd.include(request, response);
Example of RequestDispatcher interface

https://www.javatpoint.com/requestdispatcher-in-servlet
sendRedirect
 The sendRedirect() method of HttpServletResponse interface can be used to
redirect response to another resource, it may be servlet, jsp or html file.
 It accepts relative as well as absolute URL
 It works at client side because it uses the url bar of the browser to make another
request. So, it can work inside and outside the server.
 Example of sendRedirect() method
 response.sendRedirect("https://kletech.ac.in ");
Difference between forward() and
sendRedirect() method

https://www.javatpoint.com/sendRedirect()-method

You might also like