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

Exploring the Java Servlet Technology

  • 1.
    Riza Muhammad Nurman4SC 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 Nurman4SC Click to edit Master title styleContent • Introduce servlet • Implement servlets
  • 3.
    Riza Muhammad Nurman4SC 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 Nurman4SC 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 Nurman4SC 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 Nurman4SC 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 Nurman4SC 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 Nurman4SC 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 Nurman4SC 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 Nurman4SC 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 Nurman4SC 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 Nurman4SC Click to edit Master title styleThe Web Container Architecture
  • 13.
    Riza Muhammad Nurman4SC Click to edit Master title styleThe Web Container Architecture - 2
  • 14.
    Riza Muhammad Nurman4SC Click to edit Master title styleThe Web Container Architecture - 3
  • 15.
    Riza Muhammad Nurman4SC Click to edit Master title styleThe Web Container Architecture - 4
  • 16.
    Riza Muhammad Nurman4SC Click to edit Master title styleThe Web Container Architecture - 5
  • 17.
    Riza Muhammad Nurman4SC 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 Nurman4SC Click to edit Master title styleImplementing Servlets – Create a New project Pilih kategori Java Web Pilih Projects Web Application
  • 19.
    Riza Muhammad Nurman4SC 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 Nurman4SC 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 Nurman4SC Click to edit Master title styleCreating a Servlet • Packages : myservlet • Servlet Name : CurrentData
  • 22.
    Riza Muhammad Nurman4SC 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 Nurman4SC 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 Nurman4SC Click to edit Master title styleServlet CurrentDate
  • 25.
    Riza Muhammad Nurman4SC 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 Nurman4SC 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 Nurman4SC 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 Nurman4SC Click to edit Master title style