SlideShare a Scribd company logo
UNIT 3- SERVER SIDE PROGRAMMING
1
CS8651 - INTERNET
PROGRAMMING
• Servlets: Java Servlet Architecture - Servlet Life Cycle - Form
GET and POST actions
• Session Handling - Understanding Cookies - Installing and
Configuring Apache Tomcat Web Server -
• DATABASE CONNECTIVITY: JDBC perspectives, JDBC
program example
• JSP: Understanding Java Server Pages - JSP Standard Tag Library
(JSTL)-Creating HTML forms by embedding JSP code. 2
UNIT III: SYLLABUS
3
SERVLETS - JAVASERVLET ARCHITECTURE-SERVLET
LIFE CYCLE
4
SERVLETS
• 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.
5
SERVLETS
6
SERVLETS - WORKING
• What is CGI?
– Common Gateway Interface (CGI) is a standard for writing programs that
can interact through a Web server with a client running on Web browser.
7
CGI vs. SERVLETS
8
CGI vs. SERVLETS
Servlet
CGI(Common Gateway
Interface)
Servlets are portable and efficient. CGI is not portable
In Servlets, sharing of data is possible. In CGI, sharing of data is not possible.
Servlets can directly communicate with the
web server.
CGI cannot directly communicate with the
web server.
Servlets are less expensive than CGI. CGI are more expensive than Servlets.
Servlets can handle the cookies. CGI cannot handle the cookies.
• The components of Servlet Architecture are
– Client
– Web Server
– Web Container: Component in the webserver it interacts with Java servlets
which is responsible for managing the lifecycle of servlets and it also
performs the URL mapping task.
9
SERVLETS - ARCHITECTURE
• In General,
• But when a page requested by a client is different or may be build
at the time of request (Not in html format) or the server is not
having the file in the requested format(Dynamic Request), the
server need to build the page in the format requested.
• That process is done by the helper application called “Web
Container” which contains the Servlets.
• Servlet is basically a Java file which can take the request from the
client on the internet and it can process the request and provide the
response in the form of HTML.
10
SERVLETS - WORKING
• The client send the request to the server.
• The Web Server receives the request from the client.
• The webserver passes the request to correspondent Servlet in the
Web Container.
• The servlet process the request and generate the response in the
form of output.
• The servlet send the response back to the webserver and back to
client.
11
SERVLETS - WORKING
• The web container maintains the life cycle of a servlet instance.
• The life cycle of the servlet are as follows:
– Servlet Class is Loaded
– Servlet Instance is Created
– Init Method is Invoked
– Service Method is Invoked
– Destroy Method is Invoked
12
SERVLETS - LIFE CYCLE
Start
End
Ready
1. Load Servlet
Class
2. Create
Servlet
Instance
3. Call Init
Method
4. Call the
Service
Method
4. Call the
Destroy
Method
13
SERVLETS - LIFE CYCLE
Create Initialize
Available for
Service
Servicing
Requests
Un available for
Services
Destroy
Unload
Initialization
Failed
Unavailable
- Exception
Throw
Calls
Calls
14
SERVLETS - METHODS
Loading Servlet - Initialization - init()
Process the Request - Service() - toGet(), toPost()
Unloading the Servlet - destroy()
• Servlet Class is Loaded
– The class loader 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.
• 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.
• init method is invoked
– The web container calls the init method only once after creating the servlet
instance.
– The init method is used to initialize the Servlet 15
SERVLETS - METHODS
– Init is available in Javax.Servlet.Servlet.Interface.
public void init(servletconfig config) throws ServletException
{
}
• Servlet Method is Invoked
– The web container calls the service method each time when the request for
servlet is received.
– If the servlet is initialized , it calls the servlet method.
Public void service (SerletRequest request, Servlet Response response) throws
ServletException, IOException
• Destroy Method is Invoked
– The web container calls the destroy method before removing the servlet
instance from the service. 16
SERVLETS - METHODS
• Tomcat is a Web Server that can run Java Web Applications like
Java Servlets, Java Server Pages (JSP), Java Expression Language
and Java Web socket.
• Tomcat is a powerful production-ready server that is used by many
small, medium and big companies.
• https://tomcat.apache.org/download-80.cgi - Link to download the
Apache Tomcat Web Server.
• Binary Distributions -> 32-bit/64-bit Windows Service
Installer (pgp, sha512)
17
APACHE TOMCAT INSTALLATION
18
APCHE TOMCAT INSTALLATION
• Before start installation, need to setup JAVA_HOME or
JRE_HOME and CATALINA_HOME so that you can do
some maintenance operations on the Tomcat server like
Starting and Stopping the Server.
19
APCHE TOMCAT INSTALLATION
• The parameters are the way in which a client or user can send
information to the Http Server.
• For example, in a login screen, we need to send to the server,
the user and the password so that it validates them.
20
PARAMETERS, GET AND POST METHODS
• The first thing we are going to do is to create in our site a page
"login.html" with the following content:
• Then, we create a Servlet which receives the request in /login ,
which is the indicated direction in the action attribute of the
tag <form> of login.html 21
PARAMETERS, GET AND POST METHODS
• Then Start the Webserver.
• Once the server starts, type http://localhost:8080/first-
servlet/login.html
• Then, we create a Servlet which receives the request in /login ,
which is the indicated direction in the action attribute of the
tag <form> of login.html 22
PARAMETERS, GET AND POST METHODS
• Session simply means a particular interval of time.
• 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 we need 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.
23
SESSION HANDLING
• Session Tracking is used to recognize the particular user and
their data.
• Session Tracking Techniques
– There are four techniques used in Session tracking:
• Cookies
• Hidden Form Field
• URL Rewriting
• HttpSession
24
SESSION HANDLING
• A cookie is a small piece of information that is persisted
between the multiple client requests.
• Cookies are text files stored on the client computer and they
are kept for various information tracking purpose.
• Java Servlets transparently supports HTTP cookies.
• Cookies can be used by a server to indicate session IDs,
shopping cart contents, login credentials, user preferences,
and more.
25
COOKIES IN SERVLET
• Server script sends a set of cookies to the browser. For
example name, age, or identification number etc.
• Browser stores this information on local machine for future
use.
• When next time browser sends any request to web server then
it sends those cookies information to the server and server uses
that information to identify the user. 26
COOKIES IN SERVLET
• There are 2 types of cookies in servlets.
• 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 sign-out.
27
TYPES OF COOKIE
Advantage of Cookies
• Simplest technique of maintaining the state.
• Cookies are maintained at client side.
Disadvantage of Cookies
• It will not work if cookie is disabled from the
browser.
• Only textual information can be set in Cookie object.
28
COOKIES
Cookie class
• javax.servlet.http.Cookie class provides the functionality of
using cookies.
How to create Cookie?
How to delete Cookie?
29
COOKIES
Cookie ck=new Cookie("user","sonoo jaiswal");//creating cookie object
response.addCookie(ck);//adding cookie in the response
Cookie ck=new Cookie("user","");//deleting value of cookie
ck.setMaxAge(0);//changing the maximum age to 0 seconds
response.addCookie(ck);//adding cookie in the response
• JDBC stands for Java Database Connectivity.
• JDBC is a Java API to connect and execute the query with
the database.
• JDBC API uses JDBC drivers to connect with the database.
• There are four types of JDBC drivers:
– JDBC-ODBC Bridge Driver,
– Native Driver,
– Network Protocol Driver, and
– Thin Driver
30
DATABASE CONNECTIVITY - JAVA JDBC
• JDBC API is a Java API that can access any kind of tabular
data, especially data stored in a Relational Database.
• JDBC works with Java on a variety of platforms, such as
Windows, Mac OS, and the various versions of UNIX.
Why Should We Use JDBC?
• Before JDBC, ODBC API was the database API to connect
and execute the query with the database.
• But, ODBC API uses ODBC driver which is written in C
language (i.e. platform dependent and unsecured).
31
DATABASE CONNECTIVITY - JAVA JDBC
32
JDBC VS ODBC
• JDBC Driver is a software component that enables java
application to interact with the database.
• There are 4 types of JDBC drivers:
– JDBC-ODBC bridge driver
– Native-API driver (partially java driver)
– Network Protocol driver (fully java driver)
– Thin driver (fully java driver)
33
JDBC - DRIVERS
34
JDBC-ODBC bridge driver
35
Native-API driver
36
Native-API driver
37
Thin driver
• There are 5 steps to connect any java application with the
database using JDBC. These steps are as follows:
• Register the Driver class
• Create connection
• Create statement
• Execute queries
• Close connection
38
DATABASE CONNECTIVITY
• Java Server Pages (JSP) is a technology for developing
Webpages that supports dynamic content.
• This helps developers insert java code in HTML pages by
making use of special JSP tags, most of which start with <%
and end with %>.
• A Java Server Pages component is a type of Java servlet that is
designed to fulfil the role of a user interface for a Java web
application.
39
UNDERSTANDING JSP
• Web developers write JSPs as text files that combine HTML
or XHTML code, XML elements, and embedded JSP actions
and commands.
• Using JSP, we can collect input from users through Webpage
forms, present records from a database or another source, and
create Webpages dynamically.
• JSP tags can be used for a variety of purposes, such as
retrieving information from a database or registering user
preferences, accessing JavaBeans components, passing
control between pages, and sharing information between
requests, pages etc. 40
UNDERSTANDING JSP
• Java Server Pages often serve the same purpose as programs
implemented using the Common Gateway Interface (CGI).
• But JSP offers several advantages in comparison with the CGI:
– JSP are always compiled before they are processed by the
server unlike CGI/Perl which requires the server to load
an interpreter and the target script each time the page is
requested.
– Java Server Pages are built on top of the Java Servlets API,
so like Servlets, JSP also has access to all the powerful
Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP,
etc. 41
WHY USE JSP?
• The JSP pages follow these phases:
– Translation of JSP Page
– Compilation of JSP Page
– Classloading (the classloader loads class file)
– Instantiation (Object of the Generated Servlet is created).
– Initialization ( the container invokes jspInit() method).
– Request processing ( the container invokes _jspService()
method).
– Destroy ( the container invokes jspDestroy() method).
42
THE LIFECYCLE OF A JSP PAGE
43
THE LIFECYCLE OF A JSP PAGE
• JSP page is translated into Servlet by the help of JSP
translator.
• The JSP translator is a part of the web server which is
responsible for translating the JSP page into Servlet.
• After that, Servlet page is compiled by the compiler and gets
converted into the class file.
• Moreover, all the processes that happen in Servlet are
performed on JSP later like initialization, committing response
to the browser and destroy.
44
THE LIFECYCLE OF A JSP PAGE
hello.jsp
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
Put it in a folder and paste the folder in the web-apps directory
in apache tomcat to run the JSP page.
45
CREATING A SIMPLE JSP PAGE
• As with a normal page, your browser sends an HTTP request
to the web server.
• The web server recognizes that the HTTP request is for a JSP
page and forwards it to a JSP engine.
• This is done by using the URL
(http://localhost:8888/myapplication/hello.jsp) or JSP page
which ends with .jsp instead of .html.
• The JSP engine loads the JSP page from disk and converts it
into a servlet content.
46
JSP PROCESSING
• This conversion is very simple in which all template text is
converted to println( ) statements and all JSP elements are
converted to Java code.
• This code implements the corresponding dynamic behavior of
the page.
• The JSP engine compiles the servlet into an executable class
and forwards the original request to a servlet engine.
• A part of the web server called the servlet engine loads the
Servlet class and executes it.
47
JSP PROCESSING
• During execution, the servlet produces an output in HTML
format.
• The output is further passed on to the web server by the servlet
engine inside an HTTP response.
• The web server forwards the HTTP response to your browser
in terms of static HTML content.
• Finally, the web browser handles the dynamically generated
HTML page inside the HTTP response exactly as if it were a
static page
48
JSP PROCESSING
49
JSP PROCESSING
• The Java Server Pages Standard Tag Library (JSTL) is a
collection of useful JSP tags which encapsulates the core
functionality common to many JSP applications.
• JSTL has support for common, structural tasks such as
iteration and conditionals, tags for manipulating XML
documents, and SQL tags, Etc.,
• It also provides a framework for integrating the existing
custom tags with the JSTL tags.
50
JSTL
• Fast Development: JSTL provides many tags that simplify the
JSP.
• Code Reusability: We can use the JSTL tags on various pages.
• No need to use scriptlet tag: It avoids the use of scriptlet tag.
51
ADVANTAGE OF JSTL

More Related Content

What's hot

Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
Dhruvin Nakrani
 
Bootstrap
BootstrapBootstrap
Bootstrap
Jadson Santos
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
ASP.NET Page Life Cycle
ASP.NET Page Life CycleASP.NET Page Life Cycle
ASP.NET Page Life Cycle
Abhishek Sur
 
Servlets
ServletsServlets
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
NexThoughts Technologies
 
Introduction to ajax
Introduction  to  ajaxIntroduction  to  ajax
Introduction to ajax
Pihu Goel
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Javascript operators
Javascript operatorsJavascript operators
Javascript operatorsMohit Rana
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Ajax ppt
Ajax pptAjax ppt
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
Taha Malampatti
 
Common language runtime clr
Common language runtime clrCommon language runtime clr
Common language runtime clr
SanSan149
 
Struts
StrutsStruts
Struts
s4al_com
 
Lecture 7: Server side programming
Lecture 7: Server side programmingLecture 7: Server side programming
Lecture 7: Server side programming
Artificial Intelligence Institute at UofSC
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 

What's hot (20)

Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Bootstrap
BootstrapBootstrap
Bootstrap
 
Xml http request
Xml http requestXml http request
Xml http request
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
ASP.NET Page Life Cycle
ASP.NET Page Life CycleASP.NET Page Life Cycle
ASP.NET Page Life Cycle
 
Js ppt
Js pptJs ppt
Js ppt
 
Servlets
ServletsServlets
Servlets
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
Introduction to ajax
Introduction  to  ajaxIntroduction  to  ajax
Introduction to ajax
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Javascript operators
Javascript operatorsJavascript operators
Javascript operators
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Ajax ppt
Ajax pptAjax ppt
Ajax ppt
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Common language runtime clr
Common language runtime clrCommon language runtime clr
Common language runtime clr
 
Struts
StrutsStruts
Struts
 
Lecture 7: Server side programming
Lecture 7: Server side programmingLecture 7: Server side programming
Lecture 7: Server side programming
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 

Similar to CS8651 IP Unit 3.pptx

IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
pkaviya
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptx
ssuser92282c
 
Ppt for Online music store
Ppt for Online music storePpt for Online music store
Ppt for Online music store
ADEEBANADEEM
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptx
karthiksmart21
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
Senthil Kumar
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
SenthilKumar571813
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
Fahad Golra
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
Gary Yeh
 
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
sindhu991994
 
18CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-318CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-3
sivakumarmcs
 
session and cookies.ppt
session and cookies.pptsession and cookies.ppt
session and cookies.ppt
Jayaprasanna4
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
ssbd6985
 
Presentation on java servlets
Presentation on java servletsPresentation on java servlets
Presentation on java servlets
Aamir Sohail
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
SachinSingh217687
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
ramya marichamy
 
BITM3730Week12.pptx
BITM3730Week12.pptxBITM3730Week12.pptx
BITM3730Week12.pptx
MattMarino13
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jspJafar Nesargi
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
deepak kumar
 

Similar to CS8651 IP Unit 3.pptx (20)

IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptx
 
Ppt for Online music store
Ppt for Online music storePpt for Online music store
Ppt for Online music store
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
 
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptx
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
 
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
 
18CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-318CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-3
 
session and cookies.ppt
session and cookies.pptsession and cookies.ppt
session and cookies.ppt
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
Presentation on java servlets
Presentation on java servletsPresentation on java servlets
Presentation on java servlets
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
 
BITM3730Week12.pptx
BITM3730Week12.pptxBITM3730Week12.pptx
BITM3730Week12.pptx
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
 

Recently uploaded

Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 

Recently uploaded (20)

Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 

CS8651 IP Unit 3.pptx

  • 1. UNIT 3- SERVER SIDE PROGRAMMING 1 CS8651 - INTERNET PROGRAMMING
  • 2. • Servlets: Java Servlet Architecture - Servlet Life Cycle - Form GET and POST actions • Session Handling - Understanding Cookies - Installing and Configuring Apache Tomcat Web Server - • DATABASE CONNECTIVITY: JDBC perspectives, JDBC program example • JSP: Understanding Java Server Pages - JSP Standard Tag Library (JSTL)-Creating HTML forms by embedding JSP code. 2 UNIT III: SYLLABUS
  • 3. 3 SERVLETS - JAVASERVLET ARCHITECTURE-SERVLET LIFE CYCLE
  • 5. • 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. 5 SERVLETS
  • 7. • What is CGI? – Common Gateway Interface (CGI) is a standard for writing programs that can interact through a Web server with a client running on Web browser. 7 CGI vs. SERVLETS
  • 8. 8 CGI vs. SERVLETS Servlet CGI(Common Gateway Interface) Servlets are portable and efficient. CGI is not portable In Servlets, sharing of data is possible. In CGI, sharing of data is not possible. Servlets can directly communicate with the web server. CGI cannot directly communicate with the web server. Servlets are less expensive than CGI. CGI are more expensive than Servlets. Servlets can handle the cookies. CGI cannot handle the cookies.
  • 9. • The components of Servlet Architecture are – Client – Web Server – Web Container: Component in the webserver it interacts with Java servlets which is responsible for managing the lifecycle of servlets and it also performs the URL mapping task. 9 SERVLETS - ARCHITECTURE
  • 10. • In General, • But when a page requested by a client is different or may be build at the time of request (Not in html format) or the server is not having the file in the requested format(Dynamic Request), the server need to build the page in the format requested. • That process is done by the helper application called “Web Container” which contains the Servlets. • Servlet is basically a Java file which can take the request from the client on the internet and it can process the request and provide the response in the form of HTML. 10 SERVLETS - WORKING
  • 11. • The client send the request to the server. • The Web Server receives the request from the client. • The webserver passes the request to correspondent Servlet in the Web Container. • The servlet process the request and generate the response in the form of output. • The servlet send the response back to the webserver and back to client. 11 SERVLETS - WORKING
  • 12. • The web container maintains the life cycle of a servlet instance. • The life cycle of the servlet are as follows: – Servlet Class is Loaded – Servlet Instance is Created – Init Method is Invoked – Service Method is Invoked – Destroy Method is Invoked 12 SERVLETS - LIFE CYCLE Start End Ready 1. Load Servlet Class 2. Create Servlet Instance 3. Call Init Method 4. Call the Service Method 4. Call the Destroy Method
  • 13. 13 SERVLETS - LIFE CYCLE Create Initialize Available for Service Servicing Requests Un available for Services Destroy Unload Initialization Failed Unavailable - Exception Throw
  • 14. Calls Calls 14 SERVLETS - METHODS Loading Servlet - Initialization - init() Process the Request - Service() - toGet(), toPost() Unloading the Servlet - destroy()
  • 15. • Servlet Class is Loaded – The class loader 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. • 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. • init method is invoked – The web container calls the init method only once after creating the servlet instance. – The init method is used to initialize the Servlet 15 SERVLETS - METHODS
  • 16. – Init is available in Javax.Servlet.Servlet.Interface. public void init(servletconfig config) throws ServletException { } • Servlet Method is Invoked – The web container calls the service method each time when the request for servlet is received. – If the servlet is initialized , it calls the servlet method. Public void service (SerletRequest request, Servlet Response response) throws ServletException, IOException • Destroy Method is Invoked – The web container calls the destroy method before removing the servlet instance from the service. 16 SERVLETS - METHODS
  • 17. • Tomcat is a Web Server that can run Java Web Applications like Java Servlets, Java Server Pages (JSP), Java Expression Language and Java Web socket. • Tomcat is a powerful production-ready server that is used by many small, medium and big companies. • https://tomcat.apache.org/download-80.cgi - Link to download the Apache Tomcat Web Server. • Binary Distributions -> 32-bit/64-bit Windows Service Installer (pgp, sha512) 17 APACHE TOMCAT INSTALLATION
  • 19. • Before start installation, need to setup JAVA_HOME or JRE_HOME and CATALINA_HOME so that you can do some maintenance operations on the Tomcat server like Starting and Stopping the Server. 19 APCHE TOMCAT INSTALLATION
  • 20. • The parameters are the way in which a client or user can send information to the Http Server. • For example, in a login screen, we need to send to the server, the user and the password so that it validates them. 20 PARAMETERS, GET AND POST METHODS
  • 21. • The first thing we are going to do is to create in our site a page "login.html" with the following content: • Then, we create a Servlet which receives the request in /login , which is the indicated direction in the action attribute of the tag <form> of login.html 21 PARAMETERS, GET AND POST METHODS
  • 22. • Then Start the Webserver. • Once the server starts, type http://localhost:8080/first- servlet/login.html • Then, we create a Servlet which receives the request in /login , which is the indicated direction in the action attribute of the tag <form> of login.html 22 PARAMETERS, GET AND POST METHODS
  • 23. • Session simply means a particular interval of time. • 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 we need 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. 23 SESSION HANDLING
  • 24. • Session Tracking is used to recognize the particular user and their data. • Session Tracking Techniques – There are four techniques used in Session tracking: • Cookies • Hidden Form Field • URL Rewriting • HttpSession 24 SESSION HANDLING
  • 25. • A cookie is a small piece of information that is persisted between the multiple client requests. • Cookies are text files stored on the client computer and they are kept for various information tracking purpose. • Java Servlets transparently supports HTTP cookies. • Cookies can be used by a server to indicate session IDs, shopping cart contents, login credentials, user preferences, and more. 25 COOKIES IN SERVLET
  • 26. • Server script sends a set of cookies to the browser. For example name, age, or identification number etc. • Browser stores this information on local machine for future use. • When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user. 26 COOKIES IN SERVLET
  • 27. • There are 2 types of cookies in servlets. • 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 sign-out. 27 TYPES OF COOKIE
  • 28. Advantage of Cookies • Simplest technique of maintaining the state. • Cookies are maintained at client side. Disadvantage of Cookies • It will not work if cookie is disabled from the browser. • Only textual information can be set in Cookie object. 28 COOKIES
  • 29. Cookie class • javax.servlet.http.Cookie class provides the functionality of using cookies. How to create Cookie? How to delete Cookie? 29 COOKIES Cookie ck=new Cookie("user","sonoo jaiswal");//creating cookie object response.addCookie(ck);//adding cookie in the response Cookie ck=new Cookie("user","");//deleting value of cookie ck.setMaxAge(0);//changing the maximum age to 0 seconds response.addCookie(ck);//adding cookie in the response
  • 30. • JDBC stands for Java Database Connectivity. • JDBC is a Java API to connect and execute the query with the database. • JDBC API uses JDBC drivers to connect with the database. • There are four types of JDBC drivers: – JDBC-ODBC Bridge Driver, – Native Driver, – Network Protocol Driver, and – Thin Driver 30 DATABASE CONNECTIVITY - JAVA JDBC
  • 31. • JDBC API is a Java API that can access any kind of tabular data, especially data stored in a Relational Database. • JDBC works with Java on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. Why Should We Use JDBC? • Before JDBC, ODBC API was the database API to connect and execute the query with the database. • But, ODBC API uses ODBC driver which is written in C language (i.e. platform dependent and unsecured). 31 DATABASE CONNECTIVITY - JAVA JDBC
  • 33. • JDBC Driver is a software component that enables java application to interact with the database. • There are 4 types of JDBC drivers: – JDBC-ODBC bridge driver – Native-API driver (partially java driver) – Network Protocol driver (fully java driver) – Thin driver (fully java driver) 33 JDBC - DRIVERS
  • 38. • There are 5 steps to connect any java application with the database using JDBC. These steps are as follows: • Register the Driver class • Create connection • Create statement • Execute queries • Close connection 38 DATABASE CONNECTIVITY
  • 39. • Java Server Pages (JSP) is a technology for developing Webpages that supports dynamic content. • This helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>. • A Java Server Pages component is a type of Java servlet that is designed to fulfil the role of a user interface for a Java web application. 39 UNDERSTANDING JSP
  • 40. • Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands. • Using JSP, we can collect input from users through Webpage forms, present records from a database or another source, and create Webpages dynamically. • JSP tags can be used for a variety of purposes, such as retrieving information from a database or registering user preferences, accessing JavaBeans components, passing control between pages, and sharing information between requests, pages etc. 40 UNDERSTANDING JSP
  • 41. • Java Server Pages often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). • But JSP offers several advantages in comparison with the CGI: – JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested. – Java Server Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc. 41 WHY USE JSP?
  • 42. • The JSP pages follow these phases: – Translation of JSP Page – Compilation of JSP Page – Classloading (the classloader loads class file) – Instantiation (Object of the Generated Servlet is created). – Initialization ( the container invokes jspInit() method). – Request processing ( the container invokes _jspService() method). – Destroy ( the container invokes jspDestroy() method). 42 THE LIFECYCLE OF A JSP PAGE
  • 43. 43 THE LIFECYCLE OF A JSP PAGE
  • 44. • JSP page is translated into Servlet by the help of JSP translator. • The JSP translator is a part of the web server which is responsible for translating the JSP page into Servlet. • After that, Servlet page is compiled by the compiler and gets converted into the class file. • Moreover, all the processes that happen in Servlet are performed on JSP later like initialization, committing response to the browser and destroy. 44 THE LIFECYCLE OF A JSP PAGE
  • 45. hello.jsp <html> <body> <% out.print(2*5); %> </body> </html> Put it in a folder and paste the folder in the web-apps directory in apache tomcat to run the JSP page. 45 CREATING A SIMPLE JSP PAGE
  • 46. • As with a normal page, your browser sends an HTTP request to the web server. • The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. • This is done by using the URL (http://localhost:8888/myapplication/hello.jsp) or JSP page which ends with .jsp instead of .html. • The JSP engine loads the JSP page from disk and converts it into a servlet content. 46 JSP PROCESSING
  • 47. • This conversion is very simple in which all template text is converted to println( ) statements and all JSP elements are converted to Java code. • This code implements the corresponding dynamic behavior of the page. • The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine. • A part of the web server called the servlet engine loads the Servlet class and executes it. 47 JSP PROCESSING
  • 48. • During execution, the servlet produces an output in HTML format. • The output is further passed on to the web server by the servlet engine inside an HTTP response. • The web server forwards the HTTP response to your browser in terms of static HTML content. • Finally, the web browser handles the dynamically generated HTML page inside the HTTP response exactly as if it were a static page 48 JSP PROCESSING
  • 50. • The Java Server Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates the core functionality common to many JSP applications. • JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, and SQL tags, Etc., • It also provides a framework for integrating the existing custom tags with the JSTL tags. 50 JSTL
  • 51. • Fast Development: JSTL provides many tags that simplify the JSP. • Code Reusability: We can use the JSTL tags on various pages. • No need to use scriptlet tag: It avoids the use of scriptlet tag. 51 ADVANTAGE OF JSTL