IT2255 Web Essentials - Unit V Servlets and Database Connectivity

P
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();
1 de 31

Recomendados

IT2255 Web Essentials - Unit II Web Designing por
IT2255 Web Essentials - Unit II  Web DesigningIT2255 Web Essentials - Unit II  Web Designing
IT2255 Web Essentials - Unit II Web Designingpkaviya
84 vistas36 diapositivas
IT2255 Web Essentials - Unit I Website Basics por
IT2255 Web Essentials - Unit I  Website BasicsIT2255 Web Essentials - Unit I  Website Basics
IT2255 Web Essentials - Unit I Website Basicspkaviya
261 vistas57 diapositivas
Network management por
Network managementNetwork management
Network managementAksha Radhanpuri
441 vistas11 diapositivas
Server vs client por
Server vs clientServer vs client
Server vs clientMuhammed M. Mekki
1.8K vistas15 diapositivas
Client server architecture por
Client server architectureClient server architecture
Client server architectureWhitireia New Zealand
28.9K vistas16 diapositivas
message communication protocols in IoT por
message communication protocols in IoTmessage communication protocols in IoT
message communication protocols in IoTFabMinds
1.6K vistas28 diapositivas

Más contenido relacionado

La actualidad más candente

Communications is distributed systems por
Communications is distributed systemsCommunications is distributed systems
Communications is distributed systemsSHATHAN
25K vistas49 diapositivas
Network Management System and Protocol por
Network Management System and Protocol Network Management System and Protocol
Network Management System and Protocol Hamdamboy (함담보이)
4.1K vistas48 diapositivas
Client server architecture por
Client server architectureClient server architecture
Client server architectureBhargav Amin
30.5K vistas12 diapositivas
CNIT 40: 1: The Importance of DNS Security por
CNIT 40: 1: The Importance of DNS SecurityCNIT 40: 1: The Importance of DNS Security
CNIT 40: 1: The Importance of DNS SecuritySam Bowne
1.9K vistas47 diapositivas
HCI 3e - Ch 13: Socio-organizational issues and stakeholder requirements por
HCI 3e - Ch 13:  Socio-organizational issues and stakeholder requirementsHCI 3e - Ch 13:  Socio-organizational issues and stakeholder requirements
HCI 3e - Ch 13: Socio-organizational issues and stakeholder requirementsAlan Dix
17.7K vistas25 diapositivas
Lecture 08 distributed dbms por
Lecture 08 distributed dbmsLecture 08 distributed dbms
Lecture 08 distributed dbmsemailharmeet
6.1K vistas39 diapositivas

La actualidad más candente(20)

Communications is distributed systems por SHATHAN
Communications is distributed systemsCommunications is distributed systems
Communications is distributed systems
SHATHAN25K vistas
Client server architecture por Bhargav Amin
Client server architectureClient server architecture
Client server architecture
Bhargav Amin30.5K vistas
CNIT 40: 1: The Importance of DNS Security por Sam Bowne
CNIT 40: 1: The Importance of DNS SecurityCNIT 40: 1: The Importance of DNS Security
CNIT 40: 1: The Importance of DNS Security
Sam Bowne1.9K vistas
HCI 3e - Ch 13: Socio-organizational issues and stakeholder requirements por Alan Dix
HCI 3e - Ch 13:  Socio-organizational issues and stakeholder requirementsHCI 3e - Ch 13:  Socio-organizational issues and stakeholder requirements
HCI 3e - Ch 13: Socio-organizational issues and stakeholder requirements
Alan Dix17.7K vistas
Lecture 08 distributed dbms por emailharmeet
Lecture 08 distributed dbmsLecture 08 distributed dbms
Lecture 08 distributed dbms
emailharmeet6.1K vistas
Design rules and usability requirements por Andres Baravalle
Design rules and usability requirementsDesign rules and usability requirements
Design rules and usability requirements
Andres Baravalle5K vistas
Tcpip services and applications por Online
Tcpip services and applicationsTcpip services and applications
Tcpip services and applications
Online 5.1K vistas
HTTP Protocol Basic por Chuong Mai
HTTP Protocol BasicHTTP Protocol Basic
HTTP Protocol Basic
Chuong Mai8.1K vistas
Network management por Mohd Arif
Network managementNetwork management
Network management
Mohd Arif41.3K vistas
Chapter13 designing forms and reports por Dhani Ahmad
Chapter13 designing forms and reportsChapter13 designing forms and reports
Chapter13 designing forms and reports
Dhani Ahmad17.3K vistas
Circuit and packet_switching por hoanv
Circuit and packet_switchingCircuit and packet_switching
Circuit and packet_switching
hoanv12.4K vistas

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

WEB TECHNOLOGY Unit-3.pptx por
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxkarthiksmart21
23 vistas122 diapositivas
IP UNIT III PPT.pptx por
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptxssuser92282c
7 vistas79 diapositivas
CS8651 IP Unit 3.pptx por
CS8651 IP Unit 3.pptxCS8651 IP Unit 3.pptx
CS8651 IP Unit 3.pptxVigneshkumar Ponnusamy
440 vistas51 diapositivas
Servlet.pptx por
Servlet.pptxServlet.pptx
Servlet.pptxSenthil Kumar
10 vistas34 diapositivas
Servlet.pptx por
Servlet.pptxServlet.pptx
Servlet.pptxSenthilKumar571813
5 vistas34 diapositivas
session and cookies.ppt por
session and cookies.pptsession and cookies.ppt
session and cookies.pptJayaprasanna4
6 vistas53 diapositivas

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

Lecture 2: Servlets por Fahad Golra
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
Fahad Golra1.4K vistas
Servlet.ppt por MouDhara1
Servlet.pptServlet.ppt
Servlet.ppt
MouDhara124 vistas
Servlet.ppt por kstalin2
Servlet.pptServlet.ppt
Servlet.ppt
kstalin23 vistas
Ppt for Online music store por ADEEBANADEEM
Ppt for Online music storePpt for Online music store
Ppt for Online music store
ADEEBANADEEM3.7K vistas
java Servlet technology por Tanmoy Barman
java Servlet technologyjava Servlet technology
java Servlet technology
Tanmoy Barman8.6K vistas
Servlet and JSP por Gary Yeh
Servlet and JSPServlet and JSP
Servlet and JSP
Gary Yeh798 vistas
Liit tyit sem 5 enterprise java unit 1 notes 2018 por tanujaparihar
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
tanujaparihar3.3K vistas
Bt0083 server side programing por Techglyphs
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
Techglyphs79 vistas
Adv java unit 4 M.Sc CS.pdf por KALAISELVI P
Adv java unit 4 M.Sc CS.pdfAdv java unit 4 M.Sc CS.pdf
Adv java unit 4 M.Sc CS.pdf
KALAISELVI P108 vistas
UNIT-3 Servlet por ssbd6985
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
ssbd698547 vistas

Más de pkaviya

IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf por
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.pdfpkaviya
39 vistas45 diapositivas
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting por
IT2255 Web Essentials - Unit III Client-Side Processing and ScriptingIT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and Scriptingpkaviya
47 vistas82 diapositivas
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf por
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdfBT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdfpkaviya
148 vistas58 diapositivas
OIT552 Cloud Computing Material por
OIT552 Cloud Computing MaterialOIT552 Cloud Computing Material
OIT552 Cloud Computing Materialpkaviya
803 vistas105 diapositivas
OIT552 Cloud Computing - Question Bank por
OIT552 Cloud Computing - Question BankOIT552 Cloud Computing - Question Bank
OIT552 Cloud Computing - Question Bankpkaviya
2.3K vistas27 diapositivas
CS8791 Cloud Computing - Question Bank por
CS8791 Cloud Computing - Question BankCS8791 Cloud Computing - Question Bank
CS8791 Cloud Computing - Question Bankpkaviya
7.9K vistas32 diapositivas

Más de pkaviya(20)

IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf por pkaviya
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
pkaviya39 vistas
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting por pkaviya
IT2255 Web Essentials - Unit III Client-Side Processing and ScriptingIT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
pkaviya47 vistas
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf por pkaviya
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdfBT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf
BT2252 - ETBT - UNIT 3 - Enzyme Immobilization.pdf
pkaviya148 vistas
OIT552 Cloud Computing Material por pkaviya
OIT552 Cloud Computing MaterialOIT552 Cloud Computing Material
OIT552 Cloud Computing Material
pkaviya803 vistas
OIT552 Cloud Computing - Question Bank por pkaviya
OIT552 Cloud Computing - Question BankOIT552 Cloud Computing - Question Bank
OIT552 Cloud Computing - Question Bank
pkaviya2.3K vistas
CS8791 Cloud Computing - Question Bank por pkaviya
CS8791 Cloud Computing - Question BankCS8791 Cloud Computing - Question Bank
CS8791 Cloud Computing - Question Bank
pkaviya7.9K vistas
CS8592 Object Oriented Analysis & Design - UNIT V por pkaviya
CS8592 Object Oriented Analysis & Design - UNIT V CS8592 Object Oriented Analysis & Design - UNIT V
CS8592 Object Oriented Analysis & Design - UNIT V
pkaviya525 vistas
CS8592 Object Oriented Analysis & Design - UNIT IV por pkaviya
CS8592 Object Oriented Analysis & Design - UNIT IV CS8592 Object Oriented Analysis & Design - UNIT IV
CS8592 Object Oriented Analysis & Design - UNIT IV
pkaviya413 vistas
CS8592 Object Oriented Analysis & Design - UNIT III por pkaviya
CS8592 Object Oriented Analysis & Design - UNIT III CS8592 Object Oriented Analysis & Design - UNIT III
CS8592 Object Oriented Analysis & Design - UNIT III
pkaviya889 vistas
CS8592 Object Oriented Analysis & Design - UNIT II por pkaviya
CS8592 Object Oriented Analysis & Design - UNIT IICS8592 Object Oriented Analysis & Design - UNIT II
CS8592 Object Oriented Analysis & Design - UNIT II
pkaviya572 vistas
CS8592 Object Oriented Analysis & Design - UNIT I por pkaviya
CS8592 Object Oriented Analysis & Design - UNIT ICS8592 Object Oriented Analysis & Design - UNIT I
CS8592 Object Oriented Analysis & Design - UNIT I
pkaviya972 vistas
Cs8591 Computer Networks - UNIT V por pkaviya
Cs8591 Computer Networks - UNIT VCs8591 Computer Networks - UNIT V
Cs8591 Computer Networks - UNIT V
pkaviya767 vistas
CS8591 Computer Networks - Unit IV por pkaviya
CS8591 Computer Networks - Unit IVCS8591 Computer Networks - Unit IV
CS8591 Computer Networks - Unit IV
pkaviya339 vistas
CS8591 Computer Networks - Unit III por pkaviya
CS8591 Computer Networks - Unit IIICS8591 Computer Networks - Unit III
CS8591 Computer Networks - Unit III
pkaviya465 vistas
CS8591 Computer Networks - Unit II por pkaviya
CS8591 Computer Networks - Unit II CS8591 Computer Networks - Unit II
CS8591 Computer Networks - Unit II
pkaviya196 vistas
CS8591 Computer Networks - Unit I por pkaviya
CS8591 Computer Networks - Unit ICS8591 Computer Networks - Unit I
CS8591 Computer Networks - Unit I
pkaviya280 vistas
IT8602 Mobile Communication - Unit V por pkaviya
IT8602 Mobile Communication - Unit V IT8602 Mobile Communication - Unit V
IT8602 Mobile Communication - Unit V
pkaviya1.2K vistas
IT8602 - Mobile Communication Unit IV por pkaviya
IT8602 - Mobile Communication   Unit IV IT8602 - Mobile Communication   Unit IV
IT8602 - Mobile Communication Unit IV
pkaviya1.1K vistas
IT8602 Mobile Communication - Unit III por pkaviya
IT8602 Mobile Communication  - Unit IIIIT8602 Mobile Communication  - Unit III
IT8602 Mobile Communication - Unit III
pkaviya749 vistas
IT8602 Mobile Communication Unit II por pkaviya
IT8602 Mobile Communication   Unit II IT8602 Mobile Communication   Unit II
IT8602 Mobile Communication Unit II
pkaviya803 vistas

Último

The Accursed House by Émile Gaboriau por
The Accursed House  by Émile GaboriauThe Accursed House  by Émile Gaboriau
The Accursed House by Émile GaboriauDivyaSheta
212 vistas15 diapositivas
Create a Structure in VBNet.pptx por
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptxBreach_P
75 vistas8 diapositivas
CWP_23995_2013_17_11_2023_FINAL_ORDER.pdf por
CWP_23995_2013_17_11_2023_FINAL_ORDER.pdfCWP_23995_2013_17_11_2023_FINAL_ORDER.pdf
CWP_23995_2013_17_11_2023_FINAL_ORDER.pdfSukhwinderSingh895865
536 vistas6 diapositivas
Computer Introduction-Lecture06 por
Computer Introduction-Lecture06Computer Introduction-Lecture06
Computer Introduction-Lecture06Dr. Mazin Mohamed alkathiri
102 vistas12 diapositivas
Women from Hackney’s History: Stoke Newington by Sue Doe por
Women from Hackney’s History: Stoke Newington by Sue DoeWomen from Hackney’s History: Stoke Newington by Sue Doe
Women from Hackney’s History: Stoke Newington by Sue DoeHistory of Stoke Newington
157 vistas21 diapositivas
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB... por
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
73 vistas113 diapositivas

Último(20)

The Accursed House by Émile Gaboriau por DivyaSheta
The Accursed House  by Émile GaboriauThe Accursed House  by Émile Gaboriau
The Accursed House by Émile Gaboriau
DivyaSheta212 vistas
Create a Structure in VBNet.pptx por Breach_P
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptx
Breach_P75 vistas
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB... por Nguyen Thanh Tu Collection
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
Psychology KS5 por WestHatch
Psychology KS5Psychology KS5
Psychology KS5
WestHatch103 vistas
Drama KS5 Breakdown por WestHatch
Drama KS5 BreakdownDrama KS5 Breakdown
Drama KS5 Breakdown
WestHatch87 vistas
How to empty an One2many field in Odoo por Celine George
How to empty an One2many field in OdooHow to empty an One2many field in Odoo
How to empty an One2many field in Odoo
Celine George72 vistas
On Killing a Tree.pptx por AncyTEnglish
On Killing a Tree.pptxOn Killing a Tree.pptx
On Killing a Tree.pptx
AncyTEnglish66 vistas
CUNY IT Picciano.pptx por apicciano
CUNY IT Picciano.pptxCUNY IT Picciano.pptx
CUNY IT Picciano.pptx
apicciano54 vistas
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant... por Ms. Pooja Bhandare
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...
Pharmaceutical Inorganic Chemistry Unit IVMiscellaneous compounds Expectorant...
Ms. Pooja Bhandare109 vistas
The basics - information, data, technology and systems.pdf por JonathanCovena1
The basics - information, data, technology and systems.pdfThe basics - information, data, technology and systems.pdf
The basics - information, data, technology and systems.pdf
JonathanCovena1126 vistas
When Sex Gets Complicated: Porn, Affairs, & Cybersex por Marlene Maheu
When Sex Gets Complicated: Porn, Affairs, & CybersexWhen Sex Gets Complicated: Porn, Affairs, & Cybersex
When Sex Gets Complicated: Porn, Affairs, & Cybersex
Marlene Maheu73 vistas
Dance KS5 Breakdown por WestHatch
Dance KS5 BreakdownDance KS5 Breakdown
Dance KS5 Breakdown
WestHatch86 vistas

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();