Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary servlets

Rating
-
Sold
-
Pages
7
Uploaded on
21-10-2022
Written in
2020/2021

servlets are modules of java code that run in a server side to respond to client requests

Institution
Course

Content preview

J a v a a n d J 2EE 10I S 753

UNIT -6: SERVLETS

Background

Definition : Servlets are modules of Java code that run in a server application (hence
the name "Servlets", similar to "Applets" on the client side) to answer client requests.

• Servlets are not tied to a specific client-server protocol but they are most commonly
used with HTTP and the word "Servlet" is often used in the meaning of "HTTP
Servlet".

• Servlets make use of the Java standard extension classes in the packages

javax. servlet (the basic Servlet framework) and javax. servlet .http

• Typical uses for HTTP Servlets include:

o Processing and/or storing data submitted by an HTML form.

o Providing dynamic content, e.g. returning the results of a database query to the
client.

o Managing state information on top of the stateless HTTP, e.g. for an online
shopping cart system which manages shopping carts for many concurrent
customers and maps every request to the right customer.

1. Servlet Life Cycle




The life cycle of a servlet is controlled by the container in which the servlet has been
deployed. When a request is mapped to a servlet, the container performs the following
steps:

1. If an instance of the servlet does not exist, the web container:

a. Loads the servlet class

b. Creates an instance of the servlet class

c. Initializes the servlet instance by calling the init method. Initialization is covered
in Initializing a Servlet

Dept. of ISE, SJBIT Page 54

, J a v a a n d J 2EE 10I S 753


2. Invokes the service method, passing a request and response object.

3. If the container needs to remove the servlet, it finalizes the servlet by calling the
servlet’s destroy method.

1.1 A servlet example

import java.io.*; import javax.servlet.*; import javax.servlet.http. *;

public class HelloClientServlet extends HttpServlet

{

protected void doGet(HttpServletRequest req,

HttpServletResponse res)

throws ServletException, IOException

{

res.setContentType("text/html"); PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>Hello Client !</TITLE>"+

"</HEAD><BODY>Hello Client !</BODY></HTML>");

out.close();

}

public String getServletInfo()

{

return "HelloClientServlet 1.0 by Stefan Zeiger";

}

}

1.2. Servlet API

• Two packages contain the classes and interfaces that are required to build servlets.
They are javax.servlet and javax.servlet.http.

• The javax.servlet and javax.servlet.http packages provide interfaces and classes for
writing servlets. All servlets must implement the Servlet interface, which defines
life cycle methods.

1.3 The servlet packages :

Dept. of ISE, SJBIT Page 55

Written for

Institution
Course

Document information

Uploaded on
October 21, 2022
Number of pages
7
Written in
2020/2021
Type
SUMMARY

Subjects

$3.79
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
neethumolthomson

Get to know the seller

Seller avatar
neethumolthomson Srinivas institute of technology
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
4
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions