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.
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.