SlideShare a Scribd company logo
1 of 31
Download to read offline
IT2253 Web Essentials
Unit V – Servlets and Database Connectivity
Kaviya.P
Kamaraj College of Engineering & Technology
Unit IV – Servlets and Database Connectivity
Servlets: Java Servlet Architecture – Servlet Life cycle
– Form GET and POST actions – Sessions – Cookies –
Database connectivity – JDBC – Creation of simple
interactive applications – Simple database applications
Servlets – Introduction
• Servlet technology is used to create a web application (resides at server side and
generates a dynamic web page).
• 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 webserver, process the
request, produce the response, then send a response back to the webserver.
• Properties:
• Servlets work on the server-side.
• Servlets are capable of handling complex requests obtained from the
webserver.
Java Servlet Architecture
Execution of Servlets basically involves six basic steps:
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.
Java Servlet Architecture
Java Servlet Architecture
• Client: It sends HTTP requests over to the web server and again processing
the response it gets back from the server.
• Web Server: Primary job of a web server is to process the requests and
responses that a user sends over time and maintain how a web user would
be able to access the files that has been hosted over the server. The server is
a software which manages access to a centralized resource or service in a
network.. There are precisely two types of webservers:
– Static web server
– Dynamic web server
Java Servlet Architecture
• Web Container: A component in servlet architecture which is responsible
for communicating with the servlets. Two prime tasks of a web container
are:
– Managing the servlet lifecycle
– URL mapping
Web container sits at the server-side managing and handling all the requests
that are coming in either from the servlets or from some JSP pages or
potentially any other file system.
Java Servlet Architecture
Types of Servlets:
• Generic Servlets
• HTTP Servlets
To create a Servlet:
1. Implementing Servlet Interface
2. Extending Generic Servlet
3. Extending HTTP Servlet
Java Servlet Architecture
CGI (Common Gateway Interface)
• CGI technology enables the web server to call an external program and pass HTTP request
information to the external program to process the request.
• For each request, it starts a new process.
Disadvantages
• If the number of clients increases, it takes more time for sending the response.
• For each request, it starts a process, and the web server is limited to start processes.
• It uses platform dependent language e.g. C, C++, perl.
Java Servlet Architecture
Advantages of Servlet
• The web container creates threads for handling the multiple requests to the Servlet.
• Threads have many benefits over the Processes such as they share a common
memory area, lightweight, cost of communication between the threads are low.
• Advantages: Better performance, Portability, Robust, Secure.
Java Servlet Architecture
Website: static vs
dynamic
A collection of related web pages that may contain text, images,
audio and video.
HTTP
The data communication protocol used to establish
communication between client and server.
HTTP Requests
The request send by the computer to a web server that contains
all sorts of potentially interesting information.
Get vs Post It gives the difference between GET and POST request.
Container
Used in java for dynamically generating the web pages on the
server side.
Server: Web vs
Application
Used to manage the network resources and for running the
program or software that provides services.
Content Type
HTTP header that provides the description about what are you
sending to the browser.
Servlet Life Cycle
• The entire life cycle of a Servlet is managed by the Servlet container which uses
the javax.servlet.
• Stages of the Servlet Life Cycle:
• Loading a Servlet: Loading and initializing the Servlet by the Servlet container
• Initializing the Servlet: After the Servlet is instantiated successfully, the
Servlet container initializes the instantiated Servlet object. The container
initializes the Servlet object by invoking the Servlet.init(ServletConfig) method
• Request handling: It creates the ServletRequest and ServletResponse objects.
• Destroying the Servlet: When a Servlet container decides to destroy the Servlet
Servlet Life Cycle
Servlet Life Cycle
Servlet Life Cycle Methods
• init(): The Servlet.init() method is called by the Servlet container to
indicate that this Servlet instance is instantiated successfully and is about to
put into service.
• service(): It is invoked to inform the Servlet about the client requests.
• destroy(): It runs only once during the lifetime of a Servlet and signals the
end of the Servlet instance.
Servlet Life Cycle
Servlet Life Cycle Methods
Form GET and POST Actions
GET Method
• The GET method sends the encoded user information appended to the page request.
• The page and the encoded information are separated by the ? (question mark)
symbol.
• The GET method is the default method to pass information from browser to web
server.
• The GET method has size limitation: only 1024 characters can be used in a request
string.
• Servlet handles this type of requests using doGet() method.
• Example
http://www.test.com/hello?key1 = value1&key2 = value2
Form GET and POST Actions
POST Method
• More reliable method of passing information to a backend program is the POST
method.
• This packages the information in exactly the same way as GET method, but instead
of sending it as a text string after a ? (question mark) in the URL it sends it as a
separate message.
• Servlet handles this type of requests using doPost() method.
Form GET and POST Actions
Reading Form Data Using Servlet
• getParameter() − Call request.getParameter() method to get the value of a form
parameter.
• getParameterValues() − Call this method if the parameter appears more than once
and returns multiple values, for example checkbox.
• getParameterNames() − Call this method if you want a complete list of all
parameters in the current request.
Sessions
• Session Tracking is a way to maintain state (data) of an user. It is also
known as session management in servlet.
• Http protocol is a stateless so it is needed to maintain state using session
tracking techniques.
• Each time user requests to the server, server treats the request as the new
request.
• So we need to maintain the state of an user to recognize to particular user.
• HTTP is stateless that means each request is considered as the new request.
Sessions
Sessions
Session Tracking Techniques: There are four techniques used in Session
tracking:
1. Cookies
2. Hidden Form Field
3. URL Rewriting
4. HttpSession
Cookies
• A cookie is a small piece of information that is persisted between the
multiple client requests.
• A cookie has a name, a single value, and optional attributes such as a
comment, path and domain qualifiers, a maximum age, and a version
number.
Types of Cookie
• Non-persistent cookie: It is valid for single session only. It is removed
each time when user closes the browser.
• Persistent cookie: It is valid for multiple session . It is not removed each
time when user closes the browser. It is removed only if user logout or
signout.
Cookies
Advantage of Cookies
1. Simplest technique of maintaining the state.
2. Cookies are maintained at client side.
Disadvantage of Cookies
1. It will not work if cookie is disabled from the browser.
2. Only textual information can be set in Cookie object.
Cookies
Cookie class
• javax.servlet.http.Cookie class provides the functionality of using
cookies. It provides a lot of useful methods for cookies.
Constructor of Cookie class
Constructor Description
Cookie() constructs a cookie.
Cookie(String name, String value) constructs a cookie with a
specified name and value.
Cookies
Methods of Cookie class
Method Description
public void setMaxAge(int
expiry)
Sets the maximum age of the cookie in
seconds.
public String getName() Returns the name of the cookie. The
name cannot be changed after creation.
public String getValue() Returns the value of the cookie.
public void setName(String name) changes the name of the cookie.
public void setValue(String value) changes the value of the cookie.
Database Connectivity - JDBC
• JDBC stands for Java Database Connectivity.
• JDBC is a Java API to connect and execute the query with the database.
Database Connectivity - JDBC
• The java.sql package contains classes and interfaces for JDBC API.
• A list of popular interfaces of JDBC API are given below:
• Driver interface
• Connection interface
• Statement interface
• PreparedStatement interface
• CallableStatement interface
• ResultSet interface
• ResultSetMetaData interface
• DatabaseMetaData interface
• RowSet interface
Database Connectivity - JDBC
• A list of popular classes of JDBC API are given below:
• DriverManager class
• Blob class
• Clob class
• Types class
• One can use JDBC API to handle database using Java program and can
perform the following activities:
1. Connect to the database
2. Execute queries and update statements to the database
3. Retrieve the result received from the database.
Database Connectivity - JDBC
Database Connectivity - JDBC
Steps in JDBC
• Register the Driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
• Create connection
Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:
1521:xe","system","password");
• Create statement
Statement stmt=con.createStatement();
Database Connectivity - JDBC
Steps in JDBC
• Execute queries
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
• Close connection
con.close();

More Related Content

What's hot

Module (10) NAT for IPV4.pptx
Module (10) NAT for IPV4.pptxModule (10) NAT for IPV4.pptx
Module (10) NAT for IPV4.pptx
GeorgeThoreJr
 
Basics Of Networking (Overview)
Basics Of Networking (Overview)Basics Of Networking (Overview)
Basics Of Networking (Overview)
ashiesh0007
 

What's hot (20)

CS8651 Internet Programming - Basics of HTML, HTML5, CSS
CS8651   Internet Programming - Basics of HTML, HTML5, CSSCS8651   Internet Programming - Basics of HTML, HTML5, CSS
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
 
Mcse 2012
Mcse 2012Mcse 2012
Mcse 2012
 
Simple Mail Transfer Protocol
Simple Mail Transfer ProtocolSimple Mail Transfer Protocol
Simple Mail Transfer Protocol
 
IIS
IISIIS
IIS
 
Networking basics PPT
Networking basics PPTNetworking basics PPT
Networking basics PPT
 
Module: Welcome to Web 3.0
Module: Welcome to Web 3.0Module: Welcome to Web 3.0
Module: Welcome to Web 3.0
 
Routers and Routing Configuration
Routers and Routing ConfigurationRouters and Routing Configuration
Routers and Routing Configuration
 
Network layer
Network layerNetwork layer
Network layer
 
Chapter 11 - Network Address Translation for IPv4
Chapter 11 - Network Address Translation for IPv4Chapter 11 - Network Address Translation for IPv4
Chapter 11 - Network Address Translation for IPv4
 
Cisco's Three-tier Hierarchical Network Model
Cisco's Three-tier Hierarchical Network ModelCisco's Three-tier Hierarchical Network Model
Cisco's Three-tier Hierarchical Network Model
 
Apache ppt
Apache pptApache ppt
Apache ppt
 
Module (10) NAT for IPV4.pptx
Module (10) NAT for IPV4.pptxModule (10) NAT for IPV4.pptx
Module (10) NAT for IPV4.pptx
 
CCNA 2 Routing and Switching v5.0 Chapter 2
CCNA 2 Routing and Switching v5.0 Chapter 2CCNA 2 Routing and Switching v5.0 Chapter 2
CCNA 2 Routing and Switching v5.0 Chapter 2
 
7 slaac-rick graziani
7 slaac-rick graziani7 slaac-rick graziani
7 slaac-rick graziani
 
Basics Of Networking (Overview)
Basics Of Networking (Overview)Basics Of Networking (Overview)
Basics Of Networking (Overview)
 
Client & server side scripting
Client & server side scriptingClient & server side scripting
Client & server side scripting
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
Internet and search engine
Internet and search engineInternet and search engine
Internet and search engine
 
Hypertext transfer protocol (http)
Hypertext transfer protocol (http)Hypertext transfer protocol (http)
Hypertext transfer protocol (http)
 
Sub Netting
Sub NettingSub Netting
Sub Netting
 

Similar to IT2255 Web Essentials - Unit V Servlets and Database Connectivity

Similar to IT2255 Web Essentials - Unit V Servlets and Database Connectivity (20)

WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptx
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptx
 
CS8651 IP Unit 3.pptx
CS8651 IP Unit 3.pptxCS8651 IP Unit 3.pptx
CS8651 IP Unit 3.pptx
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
 
session and cookies.ppt
session and cookies.pptsession and cookies.ppt
session and cookies.ppt
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
 
Servlets
ServletsServlets
Servlets
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet1.ppt
Servlet1.pptServlet1.ppt
Servlet1.ppt
 
Ppt for Online music store
Ppt for Online music storePpt for Online music store
Ppt for Online music store
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
BITM3730Week12.pptx
BITM3730Week12.pptxBITM3730Week12.pptx
BITM3730Week12.pptx
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
 
Liit tyit sem 5 enterprise java unit 1 notes 2018
Liit tyit sem 5 enterprise java  unit 1 notes 2018 Liit tyit sem 5 enterprise java  unit 1 notes 2018
Liit tyit sem 5 enterprise java unit 1 notes 2018
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
Adv java unit 4 M.Sc CS.pdf
Adv java unit 4 M.Sc CS.pdfAdv java unit 4 M.Sc CS.pdf
Adv java unit 4 M.Sc CS.pdf
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 

More from pkaviya

BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdfBT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf
pkaviya
 
OIT552 Cloud Computing - Question Bank
OIT552 Cloud Computing - Question BankOIT552 Cloud Computing - Question Bank
OIT552 Cloud Computing - Question Bank
pkaviya
 
CS8791 Cloud Computing - Question Bank
CS8791 Cloud Computing - Question BankCS8791 Cloud Computing - Question Bank
CS8791 Cloud Computing - Question Bank
pkaviya
 
CS8592 Object Oriented Analysis & Design - UNIT V
CS8592 Object Oriented Analysis & Design - UNIT V CS8592 Object Oriented Analysis & Design - UNIT V
CS8592 Object Oriented Analysis & Design - UNIT V
pkaviya
 
CS8591 Computer Networks - Unit III
CS8591 Computer Networks - Unit IIICS8591 Computer Networks - Unit III
CS8591 Computer Networks - Unit III
pkaviya
 

More from pkaviya (20)

IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
 
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdfBT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf
 
OIT552 Cloud Computing Material
OIT552 Cloud Computing MaterialOIT552 Cloud Computing Material
OIT552 Cloud Computing Material
 
OIT552 Cloud Computing - Question Bank
OIT552 Cloud Computing - Question BankOIT552 Cloud Computing - Question Bank
OIT552 Cloud Computing - Question Bank
 
CS8791 Cloud Computing - Question Bank
CS8791 Cloud Computing - Question BankCS8791 Cloud Computing - Question Bank
CS8791 Cloud Computing - Question Bank
 
CS8592 Object Oriented Analysis & Design - UNIT V
CS8592 Object Oriented Analysis & Design - UNIT V CS8592 Object Oriented Analysis & Design - UNIT V
CS8592 Object Oriented Analysis & Design - UNIT V
 
CS8592 Object Oriented Analysis & Design - UNIT IV
CS8592 Object Oriented Analysis & Design - UNIT IV CS8592 Object Oriented Analysis & Design - UNIT IV
CS8592 Object Oriented Analysis & Design - UNIT IV
 
CS8592 Object Oriented Analysis & Design - UNIT III
CS8592 Object Oriented Analysis & Design - UNIT III CS8592 Object Oriented Analysis & Design - UNIT III
CS8592 Object Oriented Analysis & Design - UNIT III
 
CS8592 Object Oriented Analysis & Design - UNIT II
CS8592 Object Oriented Analysis & Design - UNIT IICS8592 Object Oriented Analysis & Design - UNIT II
CS8592 Object Oriented Analysis & Design - UNIT II
 
CS8592 Object Oriented Analysis & Design - UNIT I
CS8592 Object Oriented Analysis & Design - UNIT ICS8592 Object Oriented Analysis & Design - UNIT I
CS8592 Object Oriented Analysis & Design - UNIT I
 
Cs8591 Computer Networks - UNIT V
Cs8591 Computer Networks - UNIT VCs8591 Computer Networks - UNIT V
Cs8591 Computer Networks - UNIT V
 
CS8591 Computer Networks - Unit IV
CS8591 Computer Networks - Unit IVCS8591 Computer Networks - Unit IV
CS8591 Computer Networks - Unit IV
 
CS8591 Computer Networks - Unit III
CS8591 Computer Networks - Unit IIICS8591 Computer Networks - Unit III
CS8591 Computer Networks - Unit III
 
CS8591 Computer Networks - Unit II
CS8591 Computer Networks - Unit II CS8591 Computer Networks - Unit II
CS8591 Computer Networks - Unit II
 
CS8591 Computer Networks - Unit I
CS8591 Computer Networks - Unit ICS8591 Computer Networks - Unit I
CS8591 Computer Networks - Unit I
 
IT8602 Mobile Communication - Unit V
IT8602 Mobile Communication - Unit V IT8602 Mobile Communication - Unit V
IT8602 Mobile Communication - Unit V
 
IT8602 - Mobile Communication Unit IV
IT8602 - Mobile Communication   Unit IV IT8602 - Mobile Communication   Unit IV
IT8602 - Mobile Communication Unit IV
 
IT8602 Mobile Communication - Unit III
IT8602 Mobile Communication  - Unit IIIIT8602 Mobile Communication  - Unit III
IT8602 Mobile Communication - Unit III
 
IT8602 Mobile Communication Unit II
IT8602 Mobile Communication   Unit II IT8602 Mobile Communication   Unit II
IT8602 Mobile Communication Unit II
 
IT8602 Mobile Communication Question Bank
IT8602 Mobile Communication Question BankIT8602 Mobile Communication Question Bank
IT8602 Mobile Communication Question Bank
 

Recently uploaded

Recently uploaded (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

IT2255 Web Essentials - Unit V Servlets and Database Connectivity

  • 1. IT2253 Web Essentials Unit V – Servlets and Database Connectivity Kaviya.P Kamaraj College of Engineering & Technology
  • 2. Unit IV – Servlets and Database Connectivity Servlets: Java Servlet Architecture – Servlet Life cycle – Form GET and POST actions – Sessions – Cookies – Database connectivity – JDBC – Creation of simple interactive applications – Simple database applications
  • 3. Servlets – Introduction • Servlet technology is used to create a web application (resides at server side and generates a dynamic web page). • 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 webserver, process the request, produce the response, then send a response back to the webserver. • Properties: • Servlets work on the server-side. • Servlets are capable of handling complex requests obtained from the webserver.
  • 4. Java Servlet Architecture Execution of Servlets basically involves six basic steps: 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.
  • 6. Java Servlet Architecture • Client: It sends HTTP requests over to the web server and again processing the response it gets back from the server. • Web Server: Primary job of a web server is to process the requests and responses that a user sends over time and maintain how a web user would be able to access the files that has been hosted over the server. The server is a software which manages access to a centralized resource or service in a network.. There are precisely two types of webservers: – Static web server – Dynamic web server
  • 7. Java Servlet Architecture • Web Container: A component in servlet architecture which is responsible for communicating with the servlets. Two prime tasks of a web container are: – Managing the servlet lifecycle – URL mapping Web container sits at the server-side managing and handling all the requests that are coming in either from the servlets or from some JSP pages or potentially any other file system.
  • 8. Java Servlet Architecture Types of Servlets: • Generic Servlets • HTTP Servlets To create a Servlet: 1. Implementing Servlet Interface 2. Extending Generic Servlet 3. Extending HTTP Servlet
  • 9. Java Servlet Architecture CGI (Common Gateway Interface) • CGI technology enables the web server to call an external program and pass HTTP request information to the external program to process the request. • For each request, it starts a new process. Disadvantages • If the number of clients increases, it takes more time for sending the response. • For each request, it starts a process, and the web server is limited to start processes. • It uses platform dependent language e.g. C, C++, perl.
  • 10. Java Servlet Architecture Advantages of Servlet • The web container creates threads for handling the multiple requests to the Servlet. • Threads have many benefits over the Processes such as they share a common memory area, lightweight, cost of communication between the threads are low. • Advantages: Better performance, Portability, Robust, Secure.
  • 11. Java Servlet Architecture Website: static vs dynamic A collection of related web pages that may contain text, images, audio and video. HTTP The data communication protocol used to establish communication between client and server. HTTP Requests The request send by the computer to a web server that contains all sorts of potentially interesting information. Get vs Post It gives the difference between GET and POST request. Container Used in java for dynamically generating the web pages on the server side. Server: Web vs Application Used to manage the network resources and for running the program or software that provides services. Content Type HTTP header that provides the description about what are you sending to the browser.
  • 12. Servlet Life Cycle • The entire life cycle of a Servlet is managed by the Servlet container which uses the javax.servlet. • Stages of the Servlet Life Cycle: • Loading a Servlet: Loading and initializing the Servlet by the Servlet container • Initializing the Servlet: After the Servlet is instantiated successfully, the Servlet container initializes the instantiated Servlet object. The container initializes the Servlet object by invoking the Servlet.init(ServletConfig) method • Request handling: It creates the ServletRequest and ServletResponse objects. • Destroying the Servlet: When a Servlet container decides to destroy the Servlet
  • 14. Servlet Life Cycle Servlet Life Cycle Methods • init(): The Servlet.init() method is called by the Servlet container to indicate that this Servlet instance is instantiated successfully and is about to put into service. • service(): It is invoked to inform the Servlet about the client requests. • destroy(): It runs only once during the lifetime of a Servlet and signals the end of the Servlet instance.
  • 15. Servlet Life Cycle Servlet Life Cycle Methods
  • 16. Form GET and POST Actions GET Method • The GET method sends the encoded user information appended to the page request. • The page and the encoded information are separated by the ? (question mark) symbol. • The GET method is the default method to pass information from browser to web server. • The GET method has size limitation: only 1024 characters can be used in a request string. • Servlet handles this type of requests using doGet() method. • Example http://www.test.com/hello?key1 = value1&key2 = value2
  • 17. Form GET and POST Actions POST Method • More reliable method of passing information to a backend program is the POST method. • This packages the information in exactly the same way as GET method, but instead of sending it as a text string after a ? (question mark) in the URL it sends it as a separate message. • Servlet handles this type of requests using doPost() method.
  • 18. Form GET and POST Actions Reading Form Data Using Servlet • getParameter() − Call request.getParameter() method to get the value of a form parameter. • getParameterValues() − Call this method if the parameter appears more than once and returns multiple values, for example checkbox. • getParameterNames() − Call this method if you want a complete list of all parameters in the current request.
  • 19. Sessions • Session Tracking is a way to maintain state (data) of an user. It is also known as session management in servlet. • Http protocol is a stateless so it is needed to maintain state using session tracking techniques. • Each time user requests to the server, server treats the request as the new request. • So we need to maintain the state of an user to recognize to particular user. • HTTP is stateless that means each request is considered as the new request.
  • 21. Sessions Session Tracking Techniques: There are four techniques used in Session tracking: 1. Cookies 2. Hidden Form Field 3. URL Rewriting 4. HttpSession
  • 22. Cookies • A cookie is a small piece of information that is persisted between the multiple client requests. • A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. Types of Cookie • Non-persistent cookie: It is valid for single session only. It is removed each time when user closes the browser. • Persistent cookie: It is valid for multiple session . It is not removed each time when user closes the browser. It is removed only if user logout or signout.
  • 23. Cookies Advantage of Cookies 1. Simplest technique of maintaining the state. 2. Cookies are maintained at client side. Disadvantage of Cookies 1. It will not work if cookie is disabled from the browser. 2. Only textual information can be set in Cookie object.
  • 24. Cookies Cookie class • javax.servlet.http.Cookie class provides the functionality of using cookies. It provides a lot of useful methods for cookies. Constructor of Cookie class Constructor Description Cookie() constructs a cookie. Cookie(String name, String value) constructs a cookie with a specified name and value.
  • 25. Cookies Methods of Cookie class Method Description public void setMaxAge(int expiry) Sets the maximum age of the cookie in seconds. public String getName() Returns the name of the cookie. The name cannot be changed after creation. public String getValue() Returns the value of the cookie. public void setName(String name) changes the name of the cookie. public void setValue(String value) changes the value of the cookie.
  • 26. Database Connectivity - JDBC • JDBC stands for Java Database Connectivity. • JDBC is a Java API to connect and execute the query with the database.
  • 27. Database Connectivity - JDBC • The java.sql package contains classes and interfaces for JDBC API. • A list of popular interfaces of JDBC API are given below: • Driver interface • Connection interface • Statement interface • PreparedStatement interface • CallableStatement interface • ResultSet interface • ResultSetMetaData interface • DatabaseMetaData interface • RowSet interface
  • 28. Database Connectivity - JDBC • A list of popular classes of JDBC API are given below: • DriverManager class • Blob class • Clob class • Types class • One can use JDBC API to handle database using Java program and can perform the following activities: 1. Connect to the database 2. Execute queries and update statements to the database 3. Retrieve the result received from the database.
  • 30. Database Connectivity - JDBC Steps in JDBC • Register the Driver class Class.forName("oracle.jdbc.driver.OracleDriver"); • Create connection Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost: 1521:xe","system","password"); • Create statement Statement stmt=con.createStatement();
  • 31. Database Connectivity - JDBC Steps in JDBC • Execute queries ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()){ System.out.println(rs.getInt(1)+" "+rs.getString(2)); } • Close connection con.close();