1 / 35

Chapter #2 JAVA SERVLET PRGRAMMING

Chapter #2 JAVA SERVLET PRGRAMMING. Objective. Basic concepts Why? What is java servlet? Servlet container Servlet life circle Servlet API, servlet interface … Build, deploy, run a java servlet. References.

kylene
Download Presentation

Chapter #2 JAVA SERVLET PRGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. @2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING

  2. @2008 Huynh Ngoc Tin Objective Basic concepts Why? What is java servlet? Servlet container Servlet life circle Servlet API, servlet interface … Build, deploy, run a java servlet

  3. @2008 Huynh Ngoc Tin References [1] Chapter 3. Prentice Hall Ptr Java(Tm) Ee 5 Tutorial, The (3Rd Edition) (The Java Series) [2] Marty Hall. Core Servlet and Java Server Page. Sun Micro System. Prentice Hall PTR; 1 edition 2000. [3] http://java.sun.com/products/servlet

  4. @2008 Huynh Ngoc Tin Basic concepts Why? What is java servlet? What is servlet container? Servets on J2EE/JEE Platform Servlet Interface Servlet Life Circle Servlet context Request/Response

  5. Why? • The need for dynamic content. • Applets, one of the earliest attempts toward this goal (the client side) • At the same time, (CGI) scripts were the main technology used to generate dynamic content • Limitation: Lack of scalability, Java Servlet technology was created as a portable way to provide dynamic, user-oriented content.

  6. @2008 Huynh Ngoc Tin What is java Servlet? One of technologies of J2EE/JEE Platform. Java Servlet is Java component that runs on web container. Java Servlets “talks” to Web client base on request/ response that is managed by Servlet Container.

  7. @2008 Huynh Ngoc Tin What is servlet container (Web container)? A Web container is essentially the component of a Web server that interacts with the servlet. The Web container is responsible for managing the life cycle of servlets Mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights.

  8. What is servlet container (Web container)?

  9. @2008 Huynh Ngoc Tin Java Servlet on JEE Platform

  10. Servlet API • The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. • The Java Servlet APIallows a software developer to add dynamic content to a Web server using the Java platform. The generated content is commonly HTML, but may be other data such as XML. • The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.

  11. @2008 Huynh Ngoc Tin Servlet API history Packages: javax.servlet & javax.servlet.http

  12. @2008 Huynh Ngoc Tin Servlet Interface

  13. @2008 Huynh Ngoc Tin Servlet Interface Contains methods which are implemented by your servlets Define methods to: Init a java servlet. Handle requests from the client side. Remove servlet from the server. Your Servlets can implements Servlet Interface or extends from a other class that implemented Servlet Interface such as GenericServlet & HttpServlet.

  14. @2008 Huynh Ngoc Tin Servlet Interface • public abstract class GenericServlet extends Object implements Servlet, ServletConfig, Serializable • public abstract class HttpServlet extends GenericServlet implements Serializable • GenericServlet định nghĩa 1 servlet độc lập protocol, để định nghĩa 1 servlet dùng trên Web ta có thể cho kế thừa lớp HttpServlet.

  15. @2008 Huynh Ngoc Tin Servlet Interface Handle request service method: servlet Interface define service method to handle requests from the client side. Servlet Container will call service method to handle the request from the client side.

  16. @2008 Huynh Ngoc Tin Servlet Interface HttpRequest methods HttpServlet have some more methods to handle HttpRequest: doGet for handling HTTP GET requests doPost for handling HTTP POST requests … The above methods can are called automatically by service method

  17. @2008 Huynh Ngoc Tin Servlet life circle Servlet container have only one instance for each servlet. Instance is handle by init, service, destroy in Servlet Interface (JEE API). Servlet Container load and init servlets. Servlet Container have init servlets before the request come from the client. Servlet Container have to choose a servlet for the request and make decision what servlet will be removed from the server.

  18. @2008 Huynh Ngoc Tin Request & Reponse

  19. @2008 Huynh Ngoc Tin Request & Reponse Request Package all information that comes from the client. Request from Client -> Server follows the HTTP protocol HttpServletRequest methods can summarize, read the request’s information.

  20. @2008 Huynh Ngoc Tin Request & Response Response Package all information that will response for the client. Information in response object are sent to client by HTTT protocol HttpServletReponse methods can summarize, package information into response object.

  21. Request & Response • Hypertext Transfer Protocol reference to: • http://djce.org.uk/dumprequest • http://www.w3.org/Protocols/rfc2616/rfc2616.html • http://www.faqs.org/rfcs/rfc2616.html

  22. @2008 Huynh Ngoc Tin Example – Login Servlet Step by step Define a class name LoginServlet extends from GenericServlet or HttpServlet class Implement/override the following methods init() service() destroy() Compile and deploy on webserver. Call to run by URL from the client side

  23. @2008 Huynh Ngoc Tin Example – Login Servlet import javax.servlet.http.*; import java.io.*; import java.sql.*; public class LoginServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws IOException { // …. } }

  24. @2008 Huynh Ngoc Tin Example – Login Servlet // inside the service method … response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); String strUsername = request.getParameter("Username"); String strPassword = request.getParameter("Password"); …

  25. @2008 Huynh Ngoc Tin Example – Login Servlet Deploy the servlet on Apache Tomcat Web Server Web Server Apache Tomcat (read the document of other web servers to know how to deploy a servlet) Step 1: Copy servlet file class into WEB-INF\classes of your web application. Step 2: Add information about your servlet into web.xml file to notify with the web server or application server. WEB-INF\web.xml

  26. @2008 Huynh Ngoc Tin Example – Login Servlet web.xml file (configuration) <web-app> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/servlets/LoginServlet</url-pattern> </servlet-mapping> </web-app>

  27. @2008 Huynh Ngoc Tin Example – Login Servlet Call a servlet from client side Call a servlet when user clicks submit or login button from the LoginForm/client side … <Form action="../../servlets/LoginServlet" method="post"> …

  28. Connecting to Database • Directly • Using the connection pool

  29. Connecting to Database

  30. Connecting to Database

  31. Connecting to Database

  32. Connecting to Database

  33. Connecting to Database

  34. @2008 Huynh Ngoc Tin Deploy a web app on Tomcat webserver

  35. @2008 Huynh Ngoc Tin Exercises Study by yourself client side programming such as HTML, Java Script. Build, deploy and run servlets which do the below functions using NetBean 6.1 (include JBoss, GlassFish Application Server) Registration (RegisterServlet) Login (LoginServlet) Change password, registered information (UpdateUserInformationServlet) Read more about JBoss, GlassFish, Tomcat document to deploy a java servlet

More Related