100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Servlets and Java Server pages $7.99   Add to cart

Class notes

Servlets and Java Server pages

 1 view  0 purchase
  • Course
  • Institution

Servlets and Java Server

Preview 4 out of 33  pages

  • March 14, 2024
  • 33
  • 2023/2024
  • Class notes
  • Santosh bhatt
  • All classes
avatar-seller
Unit 7: Servlets and Java Server pages (8 Hrs.)
Introduction to Servlets:
Servlets are small programs that execute on the server side of a Web connection and dynamically
extend the functionality of a Web server just as applets.

 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 Architecture
The architecture, is the communication interface, protocol used, requirements of client and
server, the programming with the languages and software involved. Basically, it performs the
below-mentioned tasks.

1. The clients send the request to the webserver.
2. The web server receives the request.
3. The web server passes the request to the corresponding servlet.
4. The servlet processes the request and generates the response in the form of output.
5. The servlet sends the response back to the webserver.
6. The web server sends the response back to the client and the client browser displays it on
the screen.




Notes by Santosh Bhatt Page 1

,Servlets more closely resemble Common Gateway Interface (CGI) scripts or programs than
applets in terms of functionality. As in CGI programs, servlets can respond to user events from
an HTML request, and then dynamically construct an HTML response that is sent back to the
client.



Servlet Lifecycle:

The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the
servlet:

1. Servlet class is loaded.

2. Servlet instance is created.

3. init method is invoked.

4. service method is invoked.

5. destroy method is invoked.




As displayed in the above diagram, there are three states of a servlet: new, ready and end. The
servlet is in new state if servlet instance is created. After invoking the init() method, Servlet
comes in the ready state. In the ready state, servlet performs all the tasks. When the web
container invokes the destroy() method, it shifts to the end state.


Notes by Santosh Bhatt Page 2

,1) Servlet class is loaded

The classloader is responsible to load the servlet class. The servlet class is loaded when the first
request for the servlet is received by the web container.

2) Servlet instance is created

The web container creates the instance of a servlet after loading the servlet class. The servlet
instance is created only once in the servlet life cycle.

3) init method is invoked
The web container calls the init method only once after creating the servlet instance. The init method is us
servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given belo

public void init(ServletConfig config) throws ServletException


4) service method is invoked

The web container calls the service method each time when request for the servlet is received. If
servlet is not initialized, it follows the first three steps as described above then calls the service
method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only
once. The syntax of the service method of the Servlet interface is given below:

public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException


5) destroy method is invoked

The web container calls the destroy method before removing the servlet instance from the
service. It gives the servlet an opportunity to clean up any resource for example memory, thread
etc. The syntax of the destroy method of the Servlet interface is given below:

public void destroy()


The servlet APIs:
Servlets are Java-based server-side components that extend the capabilities of a server. They
provide a way to generate dynamic content, handle HTTP requests, and interact with web clients.
Servlets are part of the Java Platform, Enterprise Edition (Java EE) and are commonly used for
developing web applications.

Notes by Santosh Bhatt Page 3

, The Servlet API in Java is a set of classes and interfaces that define the contract between a
servlet class and the runtime environment provided by the web container. Here's an overview of
some key classes and interfaces in the Servlet API:

1. javax.servlet.Servlet interface:
 All servlets must implement this interface.
 It defines the service() method, which handles requests from clients.
2. javax.servlet.http.HttpServlet class:
 This is an abstract class that extends Servlet.
 It provides HTTP-specific functionality and simplifies the development of web
applications.
 Developers usually extend this class to create their servlets.
3. javax.servlet.ServletRequest interface:
 Represents the request made by the client to the servlet.
 Provides methods to access information such as parameters, headers, and input
streams.
4. javax.servlet.ServletResponse interface:
 Represents the response sent by the servlet to the client.
 Provides methods to set response headers and write data to the response stream.
5. javax.servlet.http.HttpServletRequest interface:
 Extends ServletRequest and provides additional methods for HTTP-specific
functionality.
 Provides methods to get information about the request, such as the HTTP method,
headers, and parameters.
6. javax.servlet.http.HttpServletResponse interface:
 Extends ServletResponse and provides additional methods for HTTP-specific
functionality.
 Allows setting HTTP response status codes and headers.

Now, let's look at a simple example of a servlet that responds to a client request:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


Notes by Santosh Bhatt Page 4

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller santoshbhatt. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $7.99. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

78677 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$7.99
  • (0)
  Add to cart