SlideShare a Scribd company logo
1 of 48
UNDERSTANDING JAVA
SERVLET
by Tanmoy Barman
Discussions
 Client-Server Architecture.
 HTTP protocol.
 Introduction to Servlet.
 J2ee container and architecture.
 Servlet Life Cycle.
 Functions of servlet container.
 Servlet API.
 Example of servlet.
 Session Management Techniques.
 Servlet Collaboration.
 Comparing Servlet with CGI.
Client server architecture
Client server architecture
 Server: typically a application for large
computers which can handle multiple request
simultaneously, can process much faster. The
application of server is written in such a way
that it cannot be access by the user directly
but it waits for another program to connect and
then it process the request as need by the
user.
Client server architecture
 Client: an application that runs on simple
desktop computers. It has an attractive user
interface but itself does not have the data to
manipulate instead it takes input from the
user, connects the server and the server
responds to the request as need by the client
using this application.
HTTP protocol
 HTTP(Hyper Text Transfer Protocol ) is a
communication protocol for displaying HTML
pages inside web browser.
 HTTP are stateless protocol because it treats
each request independently and have no
knowledge of where the request have came
from.
 A stateless protocol treats each request and
response pair as an independent transaction.
 Default port no of HTTP is 80.
HTTP protocol
 The communication between the web browser
and the server are usually done using two
methods:-
1. GET.
2. POST.
 GET method is used to request data from a
specified resource.
 POST method is used to submit data from a
specified resource.
HTTP protocol
 GET methods are cached but POST method
cannot be cached.
 GET methods can be bookmarked but POST
methods cannot be bookmarked.
 GET methods have length restrictions where as
POST methods have no length restrictions.
 GET methods remains in history of the web
browser whereas the POST methods are not
present in the history of the web browser.
 GET methods are idempotent but POST methods
are non-idempotent.
HTTP protocol
 The other HTTP methods are:-
1. Trace
2. Put
3. Options
4. Head
5. Delete
Introduction to Servlet
 Servlet are Java class files which resides on
the web server which produces dynamic
output web pages when a client request for the
particular Servlet.
 So to execute a Servlet we need a compiler
and a JVM machine inside a web server same
as when we need to compile and run a simple
java program inside our desktop.
Introduction to Servlet
 To deploy our servlet inside the web server we
need a servlet container.
 Servlet container is a java virtual machine
inside web server which provides compilation
and run time execution of servlet.
 “Apache tomcat” is an example of popular
servlet container.
J2ee container and architecture
J2ee container and architecture
 Containers are used for deploying and
execution service provided by the component.
 The various types of container in J2EE:-
A. Applet container
B. Application client container
C. EJB container
D. Servlet container
Servlet Life Cycle
 When the client request for the servlet through
the web browser the Servlet container loads
the servlet for the first time, it calls the init()
method. Before the servlet can respond to any
request the init() method need to be finished or
the servlet need to be initialize. After the init()
method the servlet container marked the
servlet as available.
Servlet Life Cycle
 For each request form the client the servlet
container calls the service method on the
servlet which is going to process the request.
The service method can use all the resources
which are marked as available in the init()
method.
 The destroy() method is called to clean up the
resources when the server shuts down or
there is a lack of resources. It also calls the
save the current state information of the
servlet which will be used when the servlet
again called for the next time.
Functions of servlet container
 Manage servlet life cycles.
 Register a servlet for one or more URLS.
 Encode MIME base response.
 Decode MIME base request.
 Handles HTTP protocols along with other
protocols(ftp,pop,smtp,telnet,etc).
 Provide network services with request and
responses cycles.
Servlet API
 The java servlet API consist of two Packages
which helps in implementation of the servlet:-
1. javax.servlet
2. javax.servlet.http
Servlet API
 javax.servlet:- It provides the core functionality
of the servlet, this API consist of classes and
interfaces which is generic and protocol
independent.
 javax.servlet.http:- This API consist of classes
and interfaces which are HTTP protocol
specific.
SERVLET API
SERVLET API
 There are two types of servlet available by default
which has been defined in servlet API
1. GenericServlet
2. HttpServlet
 GenericServlet : it defines the classes and
interfaces which are protocol independent and
generic which is define on the javax.servlet
package. It defines simple life cycle methods
such as init, service, destroy. To write generic
servlet we simply have to override the abstract
service() method.
SERVLET API
 Signature of GenericServlet:-
 public abstract class GenericServlet extends
java.io.lang implements
Servlet, ServletConfig, java.io.seriliziable
SERVLET API
 HttpServlet :- it defines classes and interfaces
which are http protocol specific. It extends the
GenericServlet class therfore it inherits the
GenericServlet methods.
 Signature of HttpServlet:-
 Public abstract class HttpServlet extends
GenericServlet implements java.io.sereliziable
Example of servlet
 To write our own servlet we have to extend the
HttpServlet class.
 Public class our_servlet extends HttpServlet {
Public void doGet(HttpServletRequest req,HttpServletResponse
resp)throws IOException,ServletException{
//code for servlet
//the logic of the application
}
}
Example of servlet
 teddy_test.java
Public void display extends HttpServlet{
Public void doGet(HttpServlet response, HttpSrvlet request)throws
IOException,ServletException{
out.println(“<HTML>”);
out.println(“< BODY>”);
response.setContentType(“text/html”);
PrintWriter out=resp.getWriter();
out.println(“<p>Hello World</p>”);
out.println(“</ BODY>”);
out.println(“< /HTML>”);
}
}
Example of servlet
 Reading Form Data using Servlet:-
 Servlets handles form data parsing automatically using the following
methods depending on the situation:
 getParameter(): -You 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.
Example of servlet
 To get information from an html page
 Let assume there are two text box present in
html page name “teddy_test.html”
<form ACTION=“teddy_servlet.java”>
Name:<input type=“text” name=“nme”>
Age:<input type=“text” name=“age”>
<input type=“submit”>
Example of servlet
 teddy_servlet.java
Public void display extends HttpServlet{
Public void doGet(HttpServlet response, HttpSrvlet request)throws
IOException,ServletException{
out.println(“<HTML>”);
out.println(“< BODY>”);
response.setContentType(“text/html”);
Printwriter out=resp.getWriter();
String name=req.getParameter(“name”);
String age=req.getParameter(“age”);
out.println(“<p>name:-”+name+”age:-”+age+”</p>”);
out.println(“</ BODY>”);
out.println(“< /HTML>”);
}
}
Session Management
Techniques
 As HTTP is a stateless protocol it cannot
remember the same user over multiple page
request and treats the same user as different
user.
 So, if a user request for a page the server
responds when the same user request for the
another page the server thinks as another
user.
 The value or the state of the page need to be
retained when we purchasing goods from
merchant sites or keep us out of daily login to
a particular sites.
Session Management
Techniques
 The are methods to retain this value over
many pages:-
1. Cookies.
2. Hidden from fields
3. Url rewriting
4. Http Session
Session Management
Techniques
 Cookies are textual information which are
stored on the client file system.
 Cookies are send by the web server to the
web browser.
 Cookies contain cookie name and cookie
value.
 Advantages:-
1. They are simple to maintain
2. They are maintained by the client no over
head of the server.
Session Management
Techniques
 Disadvantage:-
1. It will not work if it is disabled by the web
browser.
2. Easy to tamper and one person can
impersonate as another user.
 Syntax:-
Cookie ck_object=new
Cookie(“cookie_name”,cookie_value);
Session Management
Techniques
 Hidden from fields, in HTML we have to type
an extra field called “hidden” which will not be
shown to the user by the web browser but it
will pass information to the web server. It
contains value from the previous request
which will going to process the future request.
Session Management
Techniques
 Advantage:-
1. To process it we do not require any special
type of server.
2. It will work where cookies will be disable by
the web browser.
 Disadvantage:-
1. If an error occurred before the values are
saved then the values are lost.
Session Management
Techniques
 Syntax:-
<input type=“hidden” name=“” values=“”>
This field will not be shown to the user but it will
pass information to the app server.
Session Management
Techniques
 Url rewriting, it a technique of appending
values to the end of the url, so that when the
user clicks on the link the values of the current
page will be passed through the url.
 Advantage:-
1. No extra form submission is required as in
hidden from fields.
Session Management
Techniques
 Disadvantage:-
1. It can only pass textual information.
2. Work with link only.
 Syntax:-
http://url? name1=value1&name2=value2
 Appends value on the end of the url where
parameters are separated by „&‟ and
name, value pairs are separated by „=„.
Session Management
Techniques
 HttpSession object is completely maintained
and created by the Web server. To achieve this
there is HttpRequest.getSession() method.
This method returns true if there the session is
created or creates a session before returning.
It is used to identify the user across multiple
pages and they can live up to specific time
bound by the server.
Session Management
Techniques
 Syntax:-
HttpSession ses_object=request.getSession();
ses_object.setAttribute(“ses_name”,ses_value);
//to set the session on the web server.
ses_object.getAttribute(“ses_name”);
// to get the value of the session from the web server.
Servlet Collaboration
 Servlet collaboration is a technique of sharing
information between the servlets.
 In servlet collaboration on servlet pass the
information directly to the another servlet.
 To pass the information directly one servlet
need to know about the other.
Servlet Collaboration
 The servlet collaboration can be achieved by
following methods :-
1. forward
2. include
3. sendRedirect
Servlet Collaboration
 Forward and include is the method of
RequestDispatcher interface.
 Syntax:-
RequestDispatcher
rd=response.getRequestDispatcher(“url”);
rd.forward(request,reponse);
 sendRedirect is the method of
HttpServletResponse interface
 Syntax:-
response.sendRedirect(“url of the redirect
servlet”);
Servlet Collaboration
 Difference between sendRedirect and forward:-
 The sendRedirect method is used to send the
forwarded address to resources in a new domain
or server where as the forward method is used to
send the forwarded address to resources inside
the server.
 In case of forward method the servlet container
takes cares of everything, the client and browser
is not involved, whereas in case of sendRedirect
method the forwarded address is sent to the
browser of the client.
Servlet Collaboration
 In forward, the old request and response is
send along with the forwarded address, the old
request is going to process the new request. In
case of sendRedirect the old request and
response is lost and a new response is
created by the web server and treated as a
new request by the browser.
Servlet Collaboration
 In forward the forwarded address in not seen
by the client; its is transparent whereas in
sendRedirect the url is present in the url bar of
the web browser.
 The forward method is fast than the
sendRedirect as it does not require an extra
round trip.
CGI(Common Gateway Interface) allows the application server to
call an external program which is going to process the client http
request and respond to it. It creates a new process for each
request made by the client.
What is CGI?
Servlet Vs. CGI
 CGI(Common Gateway Interface) scripts
creates a separate process for each request
made by the client where as servlet is initialize
only once and creates threads for each
request, so CGI scripts are lead to overhead
when there is rise in user.
 CGI are more prone to attacks than servlet.
 CGI scripts are executed to native to operating
system where as servlet are compiled to java
byte code which can run anywhere.
Servlet Vs. CGI
 CGI are platform dependent whereas servlet
are platform independent.
 Servlet handling of request us much faster
than CGI.
Thank you

More Related Content

What's hot

What's hot (20)

Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Php technical presentation
Php technical presentationPhp technical presentation
Php technical presentation
 
Servlets
ServletsServlets
Servlets
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
 
Page layout with css
Page layout with cssPage layout with css
Page layout with css
 
Expressjs
ExpressjsExpressjs
Expressjs
 
MVC Architecture
MVC ArchitectureMVC Architecture
MVC Architecture
 
Javascript
JavascriptJavascript
Javascript
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
(Fast) Introduction to HTML & CSS
(Fast) Introduction to HTML & CSS (Fast) Introduction to HTML & CSS
(Fast) Introduction to HTML & CSS
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
Web Development with HTML5, CSS3 & JavaScript
Web Development with HTML5, CSS3 & JavaScriptWeb Development with HTML5, CSS3 & JavaScript
Web Development with HTML5, CSS3 & JavaScript
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 

Similar to java Servlet technology

Similar to java Servlet technology (20)

Servlet by Rj
Servlet by RjServlet by Rj
Servlet by Rj
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
Server side programming
Server side programming Server side programming
Server side programming
 
TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Servlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIServlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, API
 
Servlet
Servlet Servlet
Servlet
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
Servlets
ServletsServlets
Servlets
 
SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMING
 
BITM3730Week12.pptx
BITM3730Week12.pptxBITM3730Week12.pptx
BITM3730Week12.pptx
 
J2ee servlet
J2ee servletJ2ee servlet
J2ee servlet
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083
 
Servlets
ServletsServlets
Servlets
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and Servlets
 
Advanced java+JDBC+Servlet
Advanced java+JDBC+ServletAdvanced java+JDBC+Servlet
Advanced java+JDBC+Servlet
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet
 
Ecom 1
Ecom 1Ecom 1
Ecom 1
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 

More from Tanmoy Barman

Web apps architecture
Web apps architectureWeb apps architecture
Web apps architectureTanmoy Barman
 
introduction to channel borrowing scheme in cellular networks
introduction to channel borrowing scheme in cellular networksintroduction to channel borrowing scheme in cellular networks
introduction to channel borrowing scheme in cellular networksTanmoy Barman
 
INTRODUCTION TO CLOUD COMPUTING
INTRODUCTION TO CLOUD COMPUTINGINTRODUCTION TO CLOUD COMPUTING
INTRODUCTION TO CLOUD COMPUTINGTanmoy Barman
 

More from Tanmoy Barman (6)

Java rmi
Java rmiJava rmi
Java rmi
 
Jini
JiniJini
Jini
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Web apps architecture
Web apps architectureWeb apps architecture
Web apps architecture
 
introduction to channel borrowing scheme in cellular networks
introduction to channel borrowing scheme in cellular networksintroduction to channel borrowing scheme in cellular networks
introduction to channel borrowing scheme in cellular networks
 
INTRODUCTION TO CLOUD COMPUTING
INTRODUCTION TO CLOUD COMPUTINGINTRODUCTION TO CLOUD COMPUTING
INTRODUCTION TO CLOUD COMPUTING
 

Recently uploaded

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

java Servlet technology

  • 2. Discussions  Client-Server Architecture.  HTTP protocol.  Introduction to Servlet.  J2ee container and architecture.  Servlet Life Cycle.  Functions of servlet container.  Servlet API.  Example of servlet.  Session Management Techniques.  Servlet Collaboration.  Comparing Servlet with CGI.
  • 4. Client server architecture  Server: typically a application for large computers which can handle multiple request simultaneously, can process much faster. The application of server is written in such a way that it cannot be access by the user directly but it waits for another program to connect and then it process the request as need by the user.
  • 5. Client server architecture  Client: an application that runs on simple desktop computers. It has an attractive user interface but itself does not have the data to manipulate instead it takes input from the user, connects the server and the server responds to the request as need by the client using this application.
  • 6. HTTP protocol  HTTP(Hyper Text Transfer Protocol ) is a communication protocol for displaying HTML pages inside web browser.  HTTP are stateless protocol because it treats each request independently and have no knowledge of where the request have came from.  A stateless protocol treats each request and response pair as an independent transaction.  Default port no of HTTP is 80.
  • 7. HTTP protocol  The communication between the web browser and the server are usually done using two methods:- 1. GET. 2. POST.  GET method is used to request data from a specified resource.  POST method is used to submit data from a specified resource.
  • 8. HTTP protocol  GET methods are cached but POST method cannot be cached.  GET methods can be bookmarked but POST methods cannot be bookmarked.  GET methods have length restrictions where as POST methods have no length restrictions.  GET methods remains in history of the web browser whereas the POST methods are not present in the history of the web browser.  GET methods are idempotent but POST methods are non-idempotent.
  • 9. HTTP protocol  The other HTTP methods are:- 1. Trace 2. Put 3. Options 4. Head 5. Delete
  • 10. Introduction to Servlet  Servlet are Java class files which resides on the web server which produces dynamic output web pages when a client request for the particular Servlet.  So to execute a Servlet we need a compiler and a JVM machine inside a web server same as when we need to compile and run a simple java program inside our desktop.
  • 11. Introduction to Servlet  To deploy our servlet inside the web server we need a servlet container.  Servlet container is a java virtual machine inside web server which provides compilation and run time execution of servlet.  “Apache tomcat” is an example of popular servlet container.
  • 12. J2ee container and architecture
  • 13. J2ee container and architecture  Containers are used for deploying and execution service provided by the component.  The various types of container in J2EE:- A. Applet container B. Application client container C. EJB container D. Servlet container
  • 14. Servlet Life Cycle  When the client request for the servlet through the web browser the Servlet container loads the servlet for the first time, it calls the init() method. Before the servlet can respond to any request the init() method need to be finished or the servlet need to be initialize. After the init() method the servlet container marked the servlet as available.
  • 15. Servlet Life Cycle  For each request form the client the servlet container calls the service method on the servlet which is going to process the request. The service method can use all the resources which are marked as available in the init() method.  The destroy() method is called to clean up the resources when the server shuts down or there is a lack of resources. It also calls the save the current state information of the servlet which will be used when the servlet again called for the next time.
  • 16. Functions of servlet container  Manage servlet life cycles.  Register a servlet for one or more URLS.  Encode MIME base response.  Decode MIME base request.  Handles HTTP protocols along with other protocols(ftp,pop,smtp,telnet,etc).  Provide network services with request and responses cycles.
  • 17. Servlet API  The java servlet API consist of two Packages which helps in implementation of the servlet:- 1. javax.servlet 2. javax.servlet.http
  • 18. Servlet API  javax.servlet:- It provides the core functionality of the servlet, this API consist of classes and interfaces which is generic and protocol independent.  javax.servlet.http:- This API consist of classes and interfaces which are HTTP protocol specific.
  • 20. SERVLET API  There are two types of servlet available by default which has been defined in servlet API 1. GenericServlet 2. HttpServlet  GenericServlet : it defines the classes and interfaces which are protocol independent and generic which is define on the javax.servlet package. It defines simple life cycle methods such as init, service, destroy. To write generic servlet we simply have to override the abstract service() method.
  • 21. SERVLET API  Signature of GenericServlet:-  public abstract class GenericServlet extends java.io.lang implements Servlet, ServletConfig, java.io.seriliziable
  • 22. SERVLET API  HttpServlet :- it defines classes and interfaces which are http protocol specific. It extends the GenericServlet class therfore it inherits the GenericServlet methods.  Signature of HttpServlet:-  Public abstract class HttpServlet extends GenericServlet implements java.io.sereliziable
  • 23. Example of servlet  To write our own servlet we have to extend the HttpServlet class.  Public class our_servlet extends HttpServlet { Public void doGet(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException{ //code for servlet //the logic of the application } }
  • 24. Example of servlet  teddy_test.java Public void display extends HttpServlet{ Public void doGet(HttpServlet response, HttpSrvlet request)throws IOException,ServletException{ out.println(“<HTML>”); out.println(“< BODY>”); response.setContentType(“text/html”); PrintWriter out=resp.getWriter(); out.println(“<p>Hello World</p>”); out.println(“</ BODY>”); out.println(“< /HTML>”); } }
  • 25. Example of servlet  Reading Form Data using Servlet:-  Servlets handles form data parsing automatically using the following methods depending on the situation:  getParameter(): -You 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.
  • 26. Example of servlet  To get information from an html page  Let assume there are two text box present in html page name “teddy_test.html” <form ACTION=“teddy_servlet.java”> Name:<input type=“text” name=“nme”> Age:<input type=“text” name=“age”> <input type=“submit”>
  • 27. Example of servlet  teddy_servlet.java Public void display extends HttpServlet{ Public void doGet(HttpServlet response, HttpSrvlet request)throws IOException,ServletException{ out.println(“<HTML>”); out.println(“< BODY>”); response.setContentType(“text/html”); Printwriter out=resp.getWriter(); String name=req.getParameter(“name”); String age=req.getParameter(“age”); out.println(“<p>name:-”+name+”age:-”+age+”</p>”); out.println(“</ BODY>”); out.println(“< /HTML>”); } }
  • 28. Session Management Techniques  As HTTP is a stateless protocol it cannot remember the same user over multiple page request and treats the same user as different user.  So, if a user request for a page the server responds when the same user request for the another page the server thinks as another user.  The value or the state of the page need to be retained when we purchasing goods from merchant sites or keep us out of daily login to a particular sites.
  • 29. Session Management Techniques  The are methods to retain this value over many pages:- 1. Cookies. 2. Hidden from fields 3. Url rewriting 4. Http Session
  • 30. Session Management Techniques  Cookies are textual information which are stored on the client file system.  Cookies are send by the web server to the web browser.  Cookies contain cookie name and cookie value.  Advantages:- 1. They are simple to maintain 2. They are maintained by the client no over head of the server.
  • 31. Session Management Techniques  Disadvantage:- 1. It will not work if it is disabled by the web browser. 2. Easy to tamper and one person can impersonate as another user.  Syntax:- Cookie ck_object=new Cookie(“cookie_name”,cookie_value);
  • 32. Session Management Techniques  Hidden from fields, in HTML we have to type an extra field called “hidden” which will not be shown to the user by the web browser but it will pass information to the web server. It contains value from the previous request which will going to process the future request.
  • 33. Session Management Techniques  Advantage:- 1. To process it we do not require any special type of server. 2. It will work where cookies will be disable by the web browser.  Disadvantage:- 1. If an error occurred before the values are saved then the values are lost.
  • 34. Session Management Techniques  Syntax:- <input type=“hidden” name=“” values=“”> This field will not be shown to the user but it will pass information to the app server.
  • 35. Session Management Techniques  Url rewriting, it a technique of appending values to the end of the url, so that when the user clicks on the link the values of the current page will be passed through the url.  Advantage:- 1. No extra form submission is required as in hidden from fields.
  • 36. Session Management Techniques  Disadvantage:- 1. It can only pass textual information. 2. Work with link only.  Syntax:- http://url? name1=value1&name2=value2  Appends value on the end of the url where parameters are separated by „&‟ and name, value pairs are separated by „=„.
  • 37. Session Management Techniques  HttpSession object is completely maintained and created by the Web server. To achieve this there is HttpRequest.getSession() method. This method returns true if there the session is created or creates a session before returning. It is used to identify the user across multiple pages and they can live up to specific time bound by the server.
  • 38. Session Management Techniques  Syntax:- HttpSession ses_object=request.getSession(); ses_object.setAttribute(“ses_name”,ses_value); //to set the session on the web server. ses_object.getAttribute(“ses_name”); // to get the value of the session from the web server.
  • 39. Servlet Collaboration  Servlet collaboration is a technique of sharing information between the servlets.  In servlet collaboration on servlet pass the information directly to the another servlet.  To pass the information directly one servlet need to know about the other.
  • 40. Servlet Collaboration  The servlet collaboration can be achieved by following methods :- 1. forward 2. include 3. sendRedirect
  • 41. Servlet Collaboration  Forward and include is the method of RequestDispatcher interface.  Syntax:- RequestDispatcher rd=response.getRequestDispatcher(“url”); rd.forward(request,reponse);  sendRedirect is the method of HttpServletResponse interface  Syntax:- response.sendRedirect(“url of the redirect servlet”);
  • 42. Servlet Collaboration  Difference between sendRedirect and forward:-  The sendRedirect method is used to send the forwarded address to resources in a new domain or server where as the forward method is used to send the forwarded address to resources inside the server.  In case of forward method the servlet container takes cares of everything, the client and browser is not involved, whereas in case of sendRedirect method the forwarded address is sent to the browser of the client.
  • 43. Servlet Collaboration  In forward, the old request and response is send along with the forwarded address, the old request is going to process the new request. In case of sendRedirect the old request and response is lost and a new response is created by the web server and treated as a new request by the browser.
  • 44. Servlet Collaboration  In forward the forwarded address in not seen by the client; its is transparent whereas in sendRedirect the url is present in the url bar of the web browser.  The forward method is fast than the sendRedirect as it does not require an extra round trip.
  • 45. CGI(Common Gateway Interface) allows the application server to call an external program which is going to process the client http request and respond to it. It creates a new process for each request made by the client. What is CGI?
  • 46. Servlet Vs. CGI  CGI(Common Gateway Interface) scripts creates a separate process for each request made by the client where as servlet is initialize only once and creates threads for each request, so CGI scripts are lead to overhead when there is rise in user.  CGI are more prone to attacks than servlet.  CGI scripts are executed to native to operating system where as servlet are compiled to java byte code which can run anywhere.
  • 47. Servlet Vs. CGI  CGI are platform dependent whereas servlet are platform independent.  Servlet handling of request us much faster than CGI.