SlideShare una empresa de Scribd logo
1 de 28
Riza Muhammad Nurman 4SC
Click to edit Master title style
Chapter 2
on
4SC Q7M2
Chapter 2
on
4SC Q7M2
FACULTYFACULTY
Riza Muhammad NurmanRiza Muhammad Nurman
Exploring the java Servlet TechnologyExploring the java Servlet Technology
Facebook : https://facebook.com/rizamanFacebook : https://facebook.com/rizaman
Twitter : https://twitter.com/rhyzoneTwitter : https://twitter.com/rhyzone
SlideShare : https://slideshare.net/rizamanSlideShare : https://slideshare.net/rizaman
Riza Muhammad Nurman 4SC
Click to edit Master title styleContent
• Introduce servlet
• Implement servlets
Riza Muhammad Nurman 4SC
Click to edit Master title styleIntroduction to Servlets
• Web  used as a medium entertainment, business,
and socializing
• Ex: Google website
• Servlet is a Java program that runs on the server side
• Servlet features:
– Efficient :
• Servlets provide faster response to the client requests as it is loaded
only once in the Web server memory
– Asynchronous support :
• Servlet supports asynchronous programming that enables it to serve
multiple client requests and generate responses simultaneously
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe Servlet API
• To create and manage
servlets, various interfaces,
classes, and methods are
defined in the Servlet API
• Classes and interafaces of
the Servlet API are available
in the following packages:
– javax.servlet
– javax.servlet.http
Servlet API
javax.servlet javax.servlet.http
GenericServlet
ServletException
Servlet
RequestDispatcher
ServletConfig
ServletRequest
ServletResponse
ServletContext
AsyncContext
HttpServlet
Cookie
HttpServletRequest
HttpServletResponse
HttpSession
Packages
Classes
Interfaces
The Servlet API Hierarchy
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe javax.servlet Package
Interface Description
Servlet It provides the methods that all servlets must implement
ServletConfig It provides information to the servlets during the servlet initialization
ServletContext It provides methods, which is used by the servlets to communicate
with the Web container
ServletRequest It provides the client request information to the servlet
ServletResponse It helps the servlet in sending the response to the client
RequestDispatcher It receives a request from a client and sends it to any other resource,
such as a servlet, an HTML page, or a JSP page
AsyncContext It provides the methods to handle multiple client requests
asynchronously
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe Methods of the Servlet Interface
Interface Description
void init(ServletConfig config) throws
ServletException
It is called by the servlet container to indicate to a
servlet that the servlet is being placed into service
ServletConfig getServletConfig() Returns a ServletConfig object, which contains
initialization and startup parameters for this servlet
String getServletInfo() Returns information about the servlet, such as author,
version, and copyright
void service(ServletRequest request,
ServletResponse response) throws
ServletException, IOException
It is Called by the servlet container to allow the servlet
to respond to a request
void destroy() Called by the servlet container to indicate to a servlet
that the servlet is being taken out of service
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe Methods of the ServletRequest Interface
Interface Description
String getParameter(String
paramName)
Returns a String object that specifies the value of a
particular request parameter
public String[]
getParameterValues(String
paramName)
Returns an array of String objects that contains all the
values of the request parameter
public Enumeration
getParameterNames()
Returns an Enumeration object containing all the
parameter names as String objects that `request
contains
public String getRemoteHost() Returns a String object that specifies the full-qualified
name of the computer from which the request is sent
public String getRemoteAddr() Returns a String object that specifies the IP address of
the computer from which the request is sent
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe Methods of the ServletResponse Interface
Interface Description
public ServletOutputStream
getOutputStream() throws
IOException
Returns an object of the ServletOutputStream class
that represents an output stream to send binary data
as response
public PrintWriter getWriter() throws
IOException
Returns an object of the PrintWriter class that the
servlet uses to send character data as response
public void setContentType(String
type)
Sets the MIME type for a servlet response. Some of the
MIME types are text/lain, image/jpeg, and text/html.
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe Interfaces of the javax.servlet.http Package
Interface Description
HttpServletRequest It extends the ServletRequest interface to represent
the request information being sent to the HTTP
servlets by an HTTP client
HttpServletResponse It extends the ServletResponse interface and provides
methods to handle response, status codes, and
response headers of the servlets that communicate
using HTTP
HttpSession It provides mechanism that helps in identifying a client
across multiple requests
Riza Muhammad Nurman 4SC
Click to edit Master title styleCode Snippet “MyServlet”
1. import javax.servlet.http.HttpServlet;
2. import javax.servlet.http.HttpServletRequest;
3. import javax.servlet.http.HttpServletResponse;
4. public class MyServlet extends HttpServlet {
5. protected void doGet(HttpServletRequest request, HttpServletResponse
response)
6. throws ServletException, IOException {
7.
8. }
9. protected void doPost(HttpServletRequest request, HttpServletResponse
response)
10. throws ServletException, IOException {
11.
12. }
13. }
14. }
Riza Muhammad Nurman 4SC
Click to edit Master title styleUnderstanding the Web Container
• Web Container is a component of the Web server which
provides the run-time environment for executing the servlets
• Web container implements the Servlet APIs
– Communication Support : The Web container provides communication
support that a servlet can communicate with the Web server
– Lifecycle Management : The Web container manages the lifecycle of the
servlets.
– Multithreading Support : The Web container provides multithreading
support for every servlet
– Declarative Security : The Web container defines the security
constraints that specify that only the authorized users can access the
deployed servlets
– JSP Support : The Web container compiles and translates the JSP pages
into the servlet class files, which are then used to process the requests
of a client
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe Web Container Architecture
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe Web Container Architecture - 2
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe Web Container Architecture - 3
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe Web Container Architecture - 4
Riza Muhammad Nurman 4SC
Click to edit Master title styleThe Web Container Architecture - 5
Riza Muhammad Nurman 4SC
Click to edit Master title styleLife Cycle of a Servlet
• init()
– The method is called during initialization phase of the servlet life cycle
• service()
– To process client request
• destroy()
– The method marks the end of the life cycle of a servlet
public void init(ServletConfig config) throws ServletException {
...
}
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
...
}
public void destroy() {
...
}
Riza Muhammad Nurman 4SC
Click to edit Master title styleImplementing Servlets – Create a New project
Pilih kategori
Java Web
Pilih Projects
Web Application
Riza Muhammad Nurman 4SC
Click to edit Master title styleWeb Application Structure
• The root directory
– Contains static resources : HTML files, JSP
files, and image files
• The WEB-INF directory inside the root
directory
– Contains the application deployment
descriptor file, web.xml, which stores
various configurations of a Web
application
• The classes directory
– Contains the class files of the application
• The lib directory
– Contains Java Archive (JAR) files of
libraries
Riza Muhammad Nurman 4SC
Click to edit Master title styleindex.html
1. <!DOCTYPE html>
2. <!--
3. To change this license header, choose License Headers in Project Properties.
4. To change this template file, choose Tools | Templates
5. and open the template in the editor.
6. -->
7. <html>
8. <head>
9. <title>TODO supply a title</title>
10. <meta charset="UTF-8">
11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
12. </head>
13. <body>
14. <div>TODO write content</div>
15. </body>
16. </html>
Riza Muhammad Nurman 4SC
Click to edit Master title styleCreating a Servlet
• Packages : myservlet
• Servlet Name : CurrentData
Riza Muhammad Nurman 4SC
Click to edit Master title styleConfiguring a Servlet Using DD File
1. <?xml version="1.0" encoding="UTF-8"?>
2. <web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
3. <servlet>
4. <servlet-name>CurrentDate</servlet-name>
5. <servlet-class>myservlet.CurrentDate</servlet-class>
6. </servlet>
7. <servlet-mapping>
8. <servlet-name>CurrentDate</servlet-name>
9. <url-pattern>/CurrentDate</url-pattern>
10. </servlet-mapping>
11. <session-config>
12. <session-timeout>
13. 30
14. </session-timeout>
15. </session-config>
16. </web-app>
Riza Muhammad Nurman 4SC
Click to edit Master title styleConfiguring a Servlet Using Annotations
1. @WebServlet(name =
"CurrentDate", urlPatterns =
{"/CurrentDate"})
2. public class CurrentDate
extends HttpServlet {
3. …
4. }
Riza Muhammad Nurman 4SC
Click to edit Master title styleServlet CurrentDate
Riza Muhammad Nurman 4SC
Click to edit Master title styleprocessRequest Method
1. protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
2. throws ServletException, IOException {
3. response.setContentType("text/html;charset=UTF-8");
4. try (PrintWriter out = response.getWriter()) {
5. /* TODO output your page here. You may use following sample code. */
6. out.println("<!DOCTYPE html>");
7. out.println("<html>");
8. out.println("<head>");
9. out.println("<title>Servlet CurrentDate</title>");
10. out.println("</head>");
11. out.println("<body>");
12. out.println("<h1>Servlet CurrentDate at " + request.getContextPath() +
"</h1>");
13. out.println("</body>");
14. out.println("</html>");
15. }
16. }
Riza Muhammad Nurman 4SC
Click to edit Master title styleOthers Method
1. @Override
2. protected void doGet(HttpServletRequest request, HttpServletResponse
response)
3. throws ServletException, IOException {
4. processRequest(request, response);
5. }
6. @Override
7. protected void doPost(HttpServletRequest request,
HttpServletResponse response)
8. throws ServletException, IOException {
9. processRequest(request, response);
10. }
11. @Override
12. public String getServletInfo() {
13. return "Short description";
14. }
Riza Muhammad Nurman 4SC
Click to edit Master title styleSample
1. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
2. throws ServletException, IOException {
3. response.setContentType("text/html;charset=UTF-8");
4. try (PrintWriter out = response.getWriter()) {
5. /* TODO output your page here. You may use following sample code. */
6. out.println("<!DOCTYPE html>");
7. out.println("<html>");
8. out.println("<head>");
9. out.println("<title>Hello World</title>");
10. out.println("</head>");
11. out.println("<body>");
12. out.println("<h1>"+ new java.util.Date() +"</h1>");
13. out.println("</body>");
14. out.println("</html>");
15. }
16. }
Riza Muhammad Nurman 4SC
Click to edit Master title style

Más contenido relacionado

La actualidad más candente

ServiceMix 4 -- Integrating OSGi with JBI
ServiceMix 4 -- Integrating OSGi with JBIServiceMix 4 -- Integrating OSGi with JBI
ServiceMix 4 -- Integrating OSGi with JBI
Gert Vanthienen
 
WebLogic JMS System Best Practices
WebLogic JMS System Best PracticesWebLogic JMS System Best Practices
WebLogic JMS System Best Practices
Trivadis
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
vamsi krishna
 

La actualidad más candente (20)

Aspdotnet
AspdotnetAspdotnet
Aspdotnet
 
JEE Course - The Web Tier
JEE Course - The Web TierJEE Course - The Web Tier
JEE Course - The Web Tier
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Service Oriented Integration With ServiceMix
Service Oriented Integration With ServiceMixService Oriented Integration With ServiceMix
Service Oriented Integration With ServiceMix
 
ServiceMix 4 -- Integrating OSGi with JBI
ServiceMix 4 -- Integrating OSGi with JBIServiceMix 4 -- Integrating OSGi with JBI
ServiceMix 4 -- Integrating OSGi with JBI
 
WebLogic FAQs
WebLogic FAQsWebLogic FAQs
WebLogic FAQs
 
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
 
WebLogic JMS System Best Practices
WebLogic JMS System Best PracticesWebLogic JMS System Best Practices
WebLogic JMS System Best Practices
 
Websphere interview Questions
Websphere interview QuestionsWebsphere interview Questions
Websphere interview Questions
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Servlets
ServletsServlets
Servlets
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Server-Side Programming Primer
Server-Side Programming PrimerServer-Side Programming Primer
Server-Side Programming Primer
 
Mongo db eng
Mongo db engMongo db eng
Mongo db eng
 
Apache servicemix1
Apache servicemix1Apache servicemix1
Apache servicemix1
 
An Introduction to Apache ServiceMix 4 - FUSE ESB
An Introduction to Apache ServiceMix 4 - FUSE ESBAn Introduction to Apache ServiceMix 4 - FUSE ESB
An Introduction to Apache ServiceMix 4 - FUSE ESB
 
Asif
AsifAsif
Asif
 
Service Oriented Integration with ServiceMix
Service Oriented Integration with ServiceMixService Oriented Integration with ServiceMix
Service Oriented Integration with ServiceMix
 
Servlets
ServletsServlets
Servlets
 
HTML5 Refresher
HTML5 RefresherHTML5 Refresher
HTML5 Refresher
 

Similar a Exploring the Java Servlet Technology

Similar a Exploring the Java Servlet Technology (20)

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
 
Servlets
ServletsServlets
Servlets
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
Liit tyit sem 5 enterprise java unit 1 notes 2018
Liit tyit sem 5 enterprise java  unit 1 notes 2018 Liit tyit sem 5 enterprise java  unit 1 notes 2018
Liit tyit sem 5 enterprise java unit 1 notes 2018
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java Servlet
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java Servlet
Java ServletJava Servlet
Java Servlet
 
Web container and Apache Tomcat
Web container and Apache TomcatWeb container and Apache Tomcat
Web container and Apache Tomcat
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responses
 
Unit5 servlets
Unit5 servletsUnit5 servlets
Unit5 servlets
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Servlet by Rj
Servlet by RjServlet by Rj
Servlet by Rj
 
Introduction to java servlet 3.0 api javaone 2009
Introduction to java servlet 3.0 api javaone 2009Introduction to java servlet 3.0 api javaone 2009
Introduction to java servlet 3.0 api javaone 2009
 
Basics Of Servlet
Basics Of ServletBasics Of Servlet
Basics Of Servlet
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVC
 

Más de Riza Nurman

Más de Riza Nurman (20)

TOT PHP DAY 1
TOT PHP DAY 1TOT PHP DAY 1
TOT PHP DAY 1
 
SE - Chapter 9 Pemeliharaan Perangkat Lunak
SE - Chapter 9 Pemeliharaan Perangkat LunakSE - Chapter 9 Pemeliharaan Perangkat Lunak
SE - Chapter 9 Pemeliharaan Perangkat Lunak
 
SE - Chapter 8 Strategi Pengujian Perangkat Lunak
SE - Chapter 8 Strategi Pengujian Perangkat LunakSE - Chapter 8 Strategi Pengujian Perangkat Lunak
SE - Chapter 8 Strategi Pengujian Perangkat Lunak
 
SE - Chapter 7 Teknik Pengujian Perangkat Lunak
SE - Chapter 7 Teknik Pengujian Perangkat LunakSE - Chapter 7 Teknik Pengujian Perangkat Lunak
SE - Chapter 7 Teknik Pengujian Perangkat Lunak
 
SE - Chapter 6 Tim dan Kualitas Perangkat Lunak
SE - Chapter 6 Tim dan Kualitas Perangkat LunakSE - Chapter 6 Tim dan Kualitas Perangkat Lunak
SE - Chapter 6 Tim dan Kualitas Perangkat Lunak
 
XML - Chapter 8 WEB SERVICES
XML - Chapter 8 WEB SERVICESXML - Chapter 8 WEB SERVICES
XML - Chapter 8 WEB SERVICES
 
XML - Chapter 7 XML DAN DATABASE
XML - Chapter 7 XML DAN DATABASEXML - Chapter 7 XML DAN DATABASE
XML - Chapter 7 XML DAN DATABASE
 
XML - Chapter 6 SIMPLE API FOR XML (SAX)
XML - Chapter 6 SIMPLE API FOR XML (SAX)XML - Chapter 6 SIMPLE API FOR XML (SAX)
XML - Chapter 6 SIMPLE API FOR XML (SAX)
 
XML - Chapter 5 XML DOM
XML - Chapter 5 XML DOMXML - Chapter 5 XML DOM
XML - Chapter 5 XML DOM
 
DBA BAB 5 - Keamanan Database
DBA BAB 5 - Keamanan DatabaseDBA BAB 5 - Keamanan Database
DBA BAB 5 - Keamanan Database
 
DBA BAB 4 - Recovery Data
DBA BAB 4 - Recovery DataDBA BAB 4 - Recovery Data
DBA BAB 4 - Recovery Data
 
DBA BAB 3 - Manage Database
DBA BAB 3 - Manage DatabaseDBA BAB 3 - Manage Database
DBA BAB 3 - Manage Database
 
DBA BAB 2 - INSTALASI DAN UPGRADE SQL SERVER 2005
DBA BAB 2 - INSTALASI DAN UPGRADE SQL SERVER 2005DBA BAB 2 - INSTALASI DAN UPGRADE SQL SERVER 2005
DBA BAB 2 - INSTALASI DAN UPGRADE SQL SERVER 2005
 
DBA BAB 1 - Pengenalan Database Administrator
DBA BAB 1 - Pengenalan Database AdministratorDBA BAB 1 - Pengenalan Database Administrator
DBA BAB 1 - Pengenalan Database Administrator
 
RMN - XML Source Code
RMN -  XML Source CodeRMN -  XML Source Code
RMN - XML Source Code
 
XML - Chapter 4
XML - Chapter 4XML - Chapter 4
XML - Chapter 4
 
XML - Chapter 3
XML - Chapter 3XML - Chapter 3
XML - Chapter 3
 
XML - Chapter 2
XML - Chapter 2XML - Chapter 2
XML - Chapter 2
 
XML - Chapter 1
XML - Chapter 1XML - Chapter 1
XML - Chapter 1
 
ADP - Chapter 5 Exploring JavaServer Pages Technology
ADP - Chapter 5 Exploring JavaServer Pages TechnologyADP - Chapter 5 Exploring JavaServer Pages Technology
ADP - Chapter 5 Exploring JavaServer Pages Technology
 

Último

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Último (20)

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Exploring the Java Servlet Technology

  • 1. Riza Muhammad Nurman 4SC Click to edit Master title style Chapter 2 on 4SC Q7M2 Chapter 2 on 4SC Q7M2 FACULTYFACULTY Riza Muhammad NurmanRiza Muhammad Nurman Exploring the java Servlet TechnologyExploring the java Servlet Technology Facebook : https://facebook.com/rizamanFacebook : https://facebook.com/rizaman Twitter : https://twitter.com/rhyzoneTwitter : https://twitter.com/rhyzone SlideShare : https://slideshare.net/rizamanSlideShare : https://slideshare.net/rizaman
  • 2. Riza Muhammad Nurman 4SC Click to edit Master title styleContent • Introduce servlet • Implement servlets
  • 3. Riza Muhammad Nurman 4SC Click to edit Master title styleIntroduction to Servlets • Web  used as a medium entertainment, business, and socializing • Ex: Google website • Servlet is a Java program that runs on the server side • Servlet features: – Efficient : • Servlets provide faster response to the client requests as it is loaded only once in the Web server memory – Asynchronous support : • Servlet supports asynchronous programming that enables it to serve multiple client requests and generate responses simultaneously
  • 4. Riza Muhammad Nurman 4SC Click to edit Master title styleThe Servlet API • To create and manage servlets, various interfaces, classes, and methods are defined in the Servlet API • Classes and interafaces of the Servlet API are available in the following packages: – javax.servlet – javax.servlet.http Servlet API javax.servlet javax.servlet.http GenericServlet ServletException Servlet RequestDispatcher ServletConfig ServletRequest ServletResponse ServletContext AsyncContext HttpServlet Cookie HttpServletRequest HttpServletResponse HttpSession Packages Classes Interfaces The Servlet API Hierarchy
  • 5. Riza Muhammad Nurman 4SC Click to edit Master title styleThe javax.servlet Package Interface Description Servlet It provides the methods that all servlets must implement ServletConfig It provides information to the servlets during the servlet initialization ServletContext It provides methods, which is used by the servlets to communicate with the Web container ServletRequest It provides the client request information to the servlet ServletResponse It helps the servlet in sending the response to the client RequestDispatcher It receives a request from a client and sends it to any other resource, such as a servlet, an HTML page, or a JSP page AsyncContext It provides the methods to handle multiple client requests asynchronously
  • 6. Riza Muhammad Nurman 4SC Click to edit Master title styleThe Methods of the Servlet Interface Interface Description void init(ServletConfig config) throws ServletException It is called by the servlet container to indicate to a servlet that the servlet is being placed into service ServletConfig getServletConfig() Returns a ServletConfig object, which contains initialization and startup parameters for this servlet String getServletInfo() Returns information about the servlet, such as author, version, and copyright void service(ServletRequest request, ServletResponse response) throws ServletException, IOException It is Called by the servlet container to allow the servlet to respond to a request void destroy() Called by the servlet container to indicate to a servlet that the servlet is being taken out of service
  • 7. Riza Muhammad Nurman 4SC Click to edit Master title styleThe Methods of the ServletRequest Interface Interface Description String getParameter(String paramName) Returns a String object that specifies the value of a particular request parameter public String[] getParameterValues(String paramName) Returns an array of String objects that contains all the values of the request parameter public Enumeration getParameterNames() Returns an Enumeration object containing all the parameter names as String objects that `request contains public String getRemoteHost() Returns a String object that specifies the full-qualified name of the computer from which the request is sent public String getRemoteAddr() Returns a String object that specifies the IP address of the computer from which the request is sent
  • 8. Riza Muhammad Nurman 4SC Click to edit Master title styleThe Methods of the ServletResponse Interface Interface Description public ServletOutputStream getOutputStream() throws IOException Returns an object of the ServletOutputStream class that represents an output stream to send binary data as response public PrintWriter getWriter() throws IOException Returns an object of the PrintWriter class that the servlet uses to send character data as response public void setContentType(String type) Sets the MIME type for a servlet response. Some of the MIME types are text/lain, image/jpeg, and text/html.
  • 9. Riza Muhammad Nurman 4SC Click to edit Master title styleThe Interfaces of the javax.servlet.http Package Interface Description HttpServletRequest It extends the ServletRequest interface to represent the request information being sent to the HTTP servlets by an HTTP client HttpServletResponse It extends the ServletResponse interface and provides methods to handle response, status codes, and response headers of the servlets that communicate using HTTP HttpSession It provides mechanism that helps in identifying a client across multiple requests
  • 10. Riza Muhammad Nurman 4SC Click to edit Master title styleCode Snippet “MyServlet” 1. import javax.servlet.http.HttpServlet; 2. import javax.servlet.http.HttpServletRequest; 3. import javax.servlet.http.HttpServletResponse; 4. public class MyServlet extends HttpServlet { 5. protected void doGet(HttpServletRequest request, HttpServletResponse response) 6. throws ServletException, IOException { 7. 8. } 9. protected void doPost(HttpServletRequest request, HttpServletResponse response) 10. throws ServletException, IOException { 11. 12. } 13. } 14. }
  • 11. Riza Muhammad Nurman 4SC Click to edit Master title styleUnderstanding the Web Container • Web Container is a component of the Web server which provides the run-time environment for executing the servlets • Web container implements the Servlet APIs – Communication Support : The Web container provides communication support that a servlet can communicate with the Web server – Lifecycle Management : The Web container manages the lifecycle of the servlets. – Multithreading Support : The Web container provides multithreading support for every servlet – Declarative Security : The Web container defines the security constraints that specify that only the authorized users can access the deployed servlets – JSP Support : The Web container compiles and translates the JSP pages into the servlet class files, which are then used to process the requests of a client
  • 12. Riza Muhammad Nurman 4SC Click to edit Master title styleThe Web Container Architecture
  • 13. Riza Muhammad Nurman 4SC Click to edit Master title styleThe Web Container Architecture - 2
  • 14. Riza Muhammad Nurman 4SC Click to edit Master title styleThe Web Container Architecture - 3
  • 15. Riza Muhammad Nurman 4SC Click to edit Master title styleThe Web Container Architecture - 4
  • 16. Riza Muhammad Nurman 4SC Click to edit Master title styleThe Web Container Architecture - 5
  • 17. Riza Muhammad Nurman 4SC Click to edit Master title styleLife Cycle of a Servlet • init() – The method is called during initialization phase of the servlet life cycle • service() – To process client request • destroy() – The method marks the end of the life cycle of a servlet public void init(ServletConfig config) throws ServletException { ... } protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ... } public void destroy() { ... }
  • 18. Riza Muhammad Nurman 4SC Click to edit Master title styleImplementing Servlets – Create a New project Pilih kategori Java Web Pilih Projects Web Application
  • 19. Riza Muhammad Nurman 4SC Click to edit Master title styleWeb Application Structure • The root directory – Contains static resources : HTML files, JSP files, and image files • The WEB-INF directory inside the root directory – Contains the application deployment descriptor file, web.xml, which stores various configurations of a Web application • The classes directory – Contains the class files of the application • The lib directory – Contains Java Archive (JAR) files of libraries
  • 20. Riza Muhammad Nurman 4SC Click to edit Master title styleindex.html 1. <!DOCTYPE html> 2. <!-- 3. To change this license header, choose License Headers in Project Properties. 4. To change this template file, choose Tools | Templates 5. and open the template in the editor. 6. --> 7. <html> 8. <head> 9. <title>TODO supply a title</title> 10. <meta charset="UTF-8"> 11. <meta name="viewport" content="width=device-width, initial-scale=1.0"> 12. </head> 13. <body> 14. <div>TODO write content</div> 15. </body> 16. </html>
  • 21. Riza Muhammad Nurman 4SC Click to edit Master title styleCreating a Servlet • Packages : myservlet • Servlet Name : CurrentData
  • 22. Riza Muhammad Nurman 4SC Click to edit Master title styleConfiguring a Servlet Using DD File 1. <?xml version="1.0" encoding="UTF-8"?> 2. <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 3. <servlet> 4. <servlet-name>CurrentDate</servlet-name> 5. <servlet-class>myservlet.CurrentDate</servlet-class> 6. </servlet> 7. <servlet-mapping> 8. <servlet-name>CurrentDate</servlet-name> 9. <url-pattern>/CurrentDate</url-pattern> 10. </servlet-mapping> 11. <session-config> 12. <session-timeout> 13. 30 14. </session-timeout> 15. </session-config> 16. </web-app>
  • 23. Riza Muhammad Nurman 4SC Click to edit Master title styleConfiguring a Servlet Using Annotations 1. @WebServlet(name = "CurrentDate", urlPatterns = {"/CurrentDate"}) 2. public class CurrentDate extends HttpServlet { 3. … 4. }
  • 24. Riza Muhammad Nurman 4SC Click to edit Master title styleServlet CurrentDate
  • 25. Riza Muhammad Nurman 4SC Click to edit Master title styleprocessRequest Method 1. protected void processRequest(HttpServletRequest request, HttpServletResponse response) 2. throws ServletException, IOException { 3. response.setContentType("text/html;charset=UTF-8"); 4. try (PrintWriter out = response.getWriter()) { 5. /* TODO output your page here. You may use following sample code. */ 6. out.println("<!DOCTYPE html>"); 7. out.println("<html>"); 8. out.println("<head>"); 9. out.println("<title>Servlet CurrentDate</title>"); 10. out.println("</head>"); 11. out.println("<body>"); 12. out.println("<h1>Servlet CurrentDate at " + request.getContextPath() + "</h1>"); 13. out.println("</body>"); 14. out.println("</html>"); 15. } 16. }
  • 26. Riza Muhammad Nurman 4SC Click to edit Master title styleOthers Method 1. @Override 2. protected void doGet(HttpServletRequest request, HttpServletResponse response) 3. throws ServletException, IOException { 4. processRequest(request, response); 5. } 6. @Override 7. protected void doPost(HttpServletRequest request, HttpServletResponse response) 8. throws ServletException, IOException { 9. processRequest(request, response); 10. } 11. @Override 12. public String getServletInfo() { 13. return "Short description"; 14. }
  • 27. Riza Muhammad Nurman 4SC Click to edit Master title styleSample 1. protected void processRequest(HttpServletRequest request, HttpServletResponse response) 2. throws ServletException, IOException { 3. response.setContentType("text/html;charset=UTF-8"); 4. try (PrintWriter out = response.getWriter()) { 5. /* TODO output your page here. You may use following sample code. */ 6. out.println("<!DOCTYPE html>"); 7. out.println("<html>"); 8. out.println("<head>"); 9. out.println("<title>Hello World</title>"); 10. out.println("</head>"); 11. out.println("<body>"); 12. out.println("<h1>"+ new java.util.Date() +"</h1>"); 13. out.println("</body>"); 14. out.println("</html>"); 15. } 16. }
  • 28. Riza Muhammad Nurman 4SC Click to edit Master title style