1 / 27

Chapter 4 Servlets

Chapter 4 Servlets. Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML <form> tag with GET request HTML <form> tag with POST request Server-side Includes Servlet Chaining Example of Using Servlet with JDBC-ODBC

dalia
Télécharger la présentation

Chapter 4 Servlets

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. Chapter 4 Servlets • Concept of Servlets (What, Why, and How) • Servlet API • Third-party tools to run servlets • Examples of Using Servlets • HTML <form> tag with GET request • HTML <form> tag with POST request • Server-side Includes • Servlet Chaining • Example of Using Servlet with JDBC-ODBC • Session Tracking • Lab Exercises

  2. Concept of Servlets • What is a servlet? - a server-side software component, written in Java, that dynamically extends the functionality of a server - servlets execute on a java-enabled server - servlet API from Sun • Why servlets? • client/server computing • dynamic Web page updating • Multithreading • Easy use in HTML and browsers • How servlet works - Servlet lifecycle, see: servlet lifecycle and API.doc

  3. Servlet API • javax.servlet package (in zipped file: servlet lifecycle and API.doc) • javax.servlet.http package (in zipped file: HttpServlet chart and API.doc) • Download servlet API - http://java.sun.com/products/servlet/index.html • Configuration of servlet API - unzip the API package to the following directory: JDK_HOME/jre/lib/ext (JDK_HOME is the directory in which the JDK is in the execution path in your computer)

  4. Servlet API (continue) • Set up the classpath - method 1: (temporary) at DOS prompt, enter: set classpath=JDK_HOME\jre\lib\ext\ - method 2: (permanent) add: classpath=JDK_HOME\jre\lib\ext\ into autoexec.bat file and save the file (JDK_HOME is the directory in which the JDK is in the execution path in your computer)

  5. Third-party tools to run servlets • Servlets must be executed by a server, usually a Web server • There many third-party tools providing Web servers for free to use • JRun is used throughout all examples in the text • The CD-ROM with the textbook has free copy of JRun 3.0 • Tomcat server is getting popularity and it’s free to download: http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release • Configuration of Tomcat: http://www.cecs.csulb.edu/~artg/internet/tomcat.txt

  6. Third-party tools to run servlets (continue) • Execute JRun default server and admin server - click on start, programs, JRun 3.0, and select the server to run • Login to the admin server - username: admin - password: admin • More functionalities in JRun will be discussed later

  7. Examples of Using Servlets • A simple example using GenericServlet - (textbook) p. 162 – Welcome.java • Compile the program to create a class file - javac Welcome.java • Execute the servlet - method 1: run the class file by a client from a browser a). Copy the class file into the following directory in JRun: JRUN_HOME/servers/default/default-app/web-inf/classes b). Enter the following statement as URL in a browser: http://IP_address:8100/servlet/file_name (JRUN_HOME is the directory in which the JRun is installed in your computer; IP_address is the computer where the class file is copied to, enter localhost if the same machine is used for testing)

  8. Examples of Using Servlets (continue) • Execute the servlet (continue) - method 2: run the class file by a client from a HTML using hyperlink (p. 163) a). Copy and paste the class file into the following directory in JRun: JRUN_HOME/servers/default/default-app/web-inf/classes b). Create a HTML file using hyperlink as: <a herf=http://IP_address:8100/servlet/file_name>Try a servlet</a> c). Open the HTML file from a browser

  9. Examples of Using Servlets (continue) • Execute the servlet (continue) - method 3: run a HTML file stored in JRun to execute the servlet from a client computer a). Copy the HTML to the following directory: JRUN_HOME/servers/default/default-app b). Copy the servlet class file into the following directory: JRUN_HOME/servers/default/default-app/web-inf/classes c). Enter the following URL in a client browser: http://IP_address:8100/file_name.html

  10. HTML <form> tag with GET request • <form> tag allows the client to submit the request or/and data to the server • The submission then can be handled by doGet() method in HttpServlet • Example of <form> tag with GET: . <form action=“http://IP_address:8100/servlet/file_name” method=GET> . </form> .

  11. HTML <form> tag with GET request (continue) • Example of doGet() method of HttpServlet: . public void doGet(HttpServletRequest req, HttpServletRequest resp) throws ServletException, IOException { . String message = req.getParameter(“Order”); PrintWriter out=resp.setContentType(“text/html”); out.println(“<strong>display ”+message+“</strong>”); . } . • Complete example (textbook) p. 165-166

  12. HTML <form> tag with POST request • We may use either GET request or POST request to submit request or/and data to a server • GET request is handled by doGet() of HttpServlet • GET request allows browser to include the data in the URL window • POST request is handled by doPost() of HttpServlet • POST does not allow browser to include the data in the URL window • POST is more secure than GET and can handle more data

  13. HTML <form> tag with POST request(continue) • Example of <form> tag with POST: . <form action=“http://IP_address:8100/servlet/file_name” method=POST> . </form> .

  14. HTML <form> tag with POST request(continue) • Example of doGet() method of HttpServlet: public void doPost(HttpServletRequest req, HttpServletRequest resp) throws ServletException, IOException { . String message = req.getParameter(“Order”); PrintWriter out=resp.setContentType(“text/html”); out.println(“<strong>display ”+message+“</strong>”); . } • Complete example (textbook) p. 168-170 • More reading in HTTPServlet, see Servlet Chart and Methods.doc

  15. Server-side Includes • What is server-side includes? - SSIs allow servlet calls to be embedded in a HTML file using a special <servlet> tag - by convention, the file uses the .shtml file extension - JRun uses the .shtml file extension • Why server-side includes? - Embed the response to the client’s Web page - focus on the content by the servlet

  16. Server-side Includes (continue) • Example of using <servlet> tag . <servlet code= classFile_name codebase=“http://IP_address:8100/servlet”> <param name=data value=9> . • The response from the servlet will be embedded into the HTML file and displayed on the Web page • Complete example (textbook) p. 172-174

  17. Servlet Chaining • What is servlet chaining? - is the process of passing the output of one servlet to the input of another - Chart of servlet chaining (in zipped file: servlet chaining.doc) • Why servlet chaining? - to filter/alter a servlet’s output - pipelining process

  18. Servlet Chaining (continue) • Two different methods to implement servlet chaining: - chain the servlets in the URL - use MIME filter (Multipurpose Internet Mail Extensions) • Example of chaining the servlets in the URL (textbook) p. 175 http://IP_address:8100/servlet/LuckyWelcome, LuckTag

  19. Servlet Chaining (continue) • Example of using MIME filter in JRun - Configuration steps: a). Make sure JRun admin server is running b). If login is needed, use admin as both username and password c). Click on JRun Default Server d). Click on Web Applications e). Click on Default User Application f). Click on MIME Chaining g). Click on Edit(use of textbook example as follows) h). Enter custom/lucky in MIME Type i). Enter LuckyTag in Servlet Invoked j). Stop and start the Default Server • Complete example (textbook) p. 175-178 • Servlet filtering using Tomcat: http://www.cecs.csulb.edu/~artg/internet/ch4changes.txt

  20. Example of Using Servlet with JDBC-ODBC • Concept of three-tied architecture using servlet • Complete example (textbook) p. 180-182 JDBC-ODBC request response Client Server Database

  21. Session Tracking • What is a session? - is a persistent network connection b/w a client and a server that facilitates the exchange of data What is a HTTP session? - Since HTTP is connectionless and stateless protocol, a HTTP session is a virtual session - a virtual session is a series of associate requests in which the client can be uniquely identified by a session ID/data returned to the server by the client • Why session tracking? - to keep track of clients from one connection to another

  22. Session Tracking (continue) • Implementations of session tracking - method 1: using cookie (Set-Cookie in HTTP Header from a client and method setHeader() in HttpServletResponse from the server), will be discussed later - method 2: using HttpSession object • Methods in HttpSession Interface see: HttpSeesion API.doc • Complete example (textbook) p. 183-186 • Author’s updates: http://www.cecs.csulb.edu/~artg/internet/advanced.html

  23. Concept of JSP • What is JSP? - stands for JavaServer Pages - is a simple Java-based specification for building dynamic Web sites - is built on top of Java servlets. All JSP pages are automatically compiled into a servlet by the JSP container • Why JSP? - Simplify the use of servlets - Client/server applications - Integration ability

  24. Using JSP Tags in HTML • We use comment tag <!-- comment statements--> to write the comments • We use JSP page directive tag <%@ page import=“package_name”%> to import a Java package in HTML • We can use JSP expression tag <% = any Java expression %> to write any Java expression in HTML • We use JSP statement tag <% any Java statement(s) %> to write Java statements in HTML • So, we can handle all kind of requests from a client in HTML

  25. Simple Example of JSP with HTML GET method • (p. 188) Example 4.10 GetOrder.jsp - Comparing JSP version with servlet version (p. 188) GetOrder.jsp and (p.166) GetOrder.java - Comparing HTML files in these two versions (p. 188) GetJspOrder.html and (p. 165) GetOrder.html • Steps to execute the application: 1. Copy GetJspOrder.html and GetOrder.jsp into the following JRun directory: JRUN_HOME\servers\default\default-app 2. Browser the URL: http://IP_address:8100/GetJspOrder.html

  26. Using JSP Handling HTML POST Method • Example handling POST method in HTML • (p. 190) Example 4.11 – PostOrder.jsp • (p. 190) PostJspOrder.html • Steps to execute the application: 1. Copy PostJspOrder.html and PostOrder.jsp into the following JRun directory: JRUN_HOME\servers\default\default-app 2. Browser the URL: http://IP_address:8100/PostJspOrder.html

  27. In-class Exercises - Do TRY-IT-YOURSELF 3, 7, 9, 11,13, and 15 - textbook (p. 195) 3, 5, 7, and 9

More Related