1 / 31

Web based Applications, Tomcat and Servlets - Lab 3 -

Web based Applications, Tomcat and Servlets - Lab 3 -. The Basic Web Server. CGI. CGI – Common Gateway Interface Processing forms & Generating dynamic content (early solution ) Spawning an external program, pass the data from the HTML form to the program

Download Presentation

Web based Applications, Tomcat and Servlets - Lab 3 -

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. Web based Applications, Tomcat and Servlets - Lab 3 -

  2. The Basic Web Server

  3. CGI • CGI – Common Gateway Interface • Processing forms & Generating dynamic content (early solution ) • Spawning an external program, pass the data from the HTML form to the program • Limitation: An expensive process (Spawning an external program)

  4. CGI: Illustration Browser Browser Browser Browser GET Form.cgi Response Web Server Web Server Web Server Web Server Spawn an external program Read environment variables Response form.cgi terminates Start form.cgi CGI Program CGI Program CGI Program CGI Program time

  5. ASP • ASP – Active Server Pages • Microsoft’s answers to CGI programming • ASP page - a Web page with code embedded inside, interpreted by the Web server • Using special HTML tags, VBScript/JavaScript code can be inserted

  6. Servlets • Java technology's answer to CGI programming • A Java class handling forms on Java Web servers • Applet -- a little piece of program (client side) • Servlet -- a little piece of program (server side) • A servlet can do everything that a CGI program can • Running inside the JVM along with the Web server itself

  7. Servlets: Illustration Browser Browser Browser Browser GET /servlet/Form Response Java Web Server Java Web Server Java Web Server Java Web Server Response Request Create an Instance of the servlet if it is not loaded Form Servlet Form Servlet Form Servlet Remains loaded time

  8. Servlets Support in Existing Servers • Apache Web Server & some other Java-enabled Web servers • Servlet engine Usually a separate process, not within the same process at the Web server e.g. Tomcat

  9. Typical Servlet Engine Implementation Servlet Engine Web Server Servlet 1 Browser HTTP traffic Custom TCP/IP Protocol dispatcher API Servlet 2 Servlet N • Tomcat A Servlet Engine for Apache Web Server

  10. JSP • JSP – Java Server Pages - Java Version of ASP - A Web page with Java code embedded inside it and running on the Web server • Java’s answer to both CGI and ASP • Compiled into servlets • JSP & servlets can easily interact

  11. Tomcat • Servlet &JSP engine for Apache Web Server • An open-source package part of the Apache Software Foundation’s Jakarta project • The Tomcat Web site: • http://jakarta.apache.org/tomcat/index.html

  12. Installing Tomcat • Setting the CLASSPATH: the same classpath settings as your JVM • Change the CLASSPATH environment variable from your login shell • Follow the instructions in: http://ugweb.cs.ualberta.ca/~c391/tutorial/softwInstall.html • Test your installation

  13. Installation Tips • Use “echo $SHELL” to double check your shell • When editing catalina/conf/server.xml, <Server … port=…> is Port 2 and <Connector … port=…> is Port 1 • Use “Port 1” when you input the URL • Take advantage of the alias to start and stop Tomcat (commands are the same in Bourne shell and C shell)

  14. URL Mapping • Unlike ASP, JSP, etc., servlets have no extension: <form name=“FirstServlet" method="GET" action=“firstservlet"> • A special URL mapping is needed for each servlet you create. These mappings are defined in web.xml file. • In our lab, the special URL is like: http://<ui???>.cs.ualberta.ca:<Port1>/proj1/servlet Machine Name …/proj1/firstservlet

  15. Mapping URL to Machine Directory Put your plain HTML files here Catalina Webapps http://ui???.cs.ualberta.ca:portNum/proj1 <form name=“FirstServlet" method="GET" action=“firstservlet"> proj1 WEB-INF Put your servlets here classes http://ui???.cs.ualberta.ca:portNum/proj1/servlet

  16. Your First Servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web server that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println("<html>"); out.println("<body>"); out.println("<h1>Welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } } http://ugweb.cs.ualberta.ca/~c391/tutorial/examples/FirstServlet.java

  17. Compiling the Servlet • Edit the file “FirstServlet.java” • javac FirstServlet.java • Start your Tomcat • Launch a browser, input the URL like: http://ui00.cs.ualberta.ca:16410/proj1/firstservlet

  18. What Does Your First Servlet Look Like?

  19. The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println("<html>"); out.println("<body>"); out.println("<h1>Welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } } The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets.

  20. The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println("<html>"); out.println("<body>"); out.println("<h1>Welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } } Implement the Servlet interface: subclassing the existing GenericServlet class that implements Servlet

  21. The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println("<html>"); out.println("<body>"); out.println("<h1>Welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } } Invoke the servlet’s service method The service method takes two arguments: • ServletRequest: containing info about the request from the browser. • ServletRespone: containing info about the response going back.

  22. The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println("<html>"); out.println("<body>"); out.println("<h1>Welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } } Tell the Web browser what will be returned is HTML. Required!

  23. The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println("<html>"); out.println("<body>"); out.println("<h1>Welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } } The response object has the methods necessary to get an output stream for writing a response. A general rule of thumb: • Writing out text: use a PrintWriter object by calling getWriter() • Sending binary data to the browser: use a ServletOutputStream by calling getOutputStream() Some content types require a slightly different character set, and the PrintWriter object automatically adjusts the character set based on the content type.

  24. The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println("<html>"); out.println("<body>"); out.println("<h1>Welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } } You don’t need to worry about closing the output stream when you have done.

  25. Servlet Life Cycle Loads the servlet (if it has not yet been loaded) Initializes the servlet instance -run init method Servlets accept request from Clients and return data back - run service method If the engine needs to remove the servlet, it finalizes the servlet - run destroy method

  26. The Servlet Interface Hierarchy Definition of The Servlet Interface Package javax.servlet; Public interface Servlet { public void destroy(); public ServletConfig getServletConfig(); public String getServletInfo(); public void init (ServletConfig config) throws ServletException; public void service (ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException; } Servlet Interface GenericServlet Class HttpServlet Class Your servlet class

  27. The HttpServlet Class • It has extra methods and special request-and-response objects that are geared toward the HTTP protocol • The HttpServlet provides separate methods for handling the different type of HTTP requests. The two most common ones are GET and POST: • public void doGet(HTTPServletRequest request, HTTPServletResponse response) throws ServletException, java.io.IOException; • public void doPost(HTTPServletRequest request, HTTPServletResponse response) throws ServletException, java.io.IOException;

  28. The HttpServlet Class doGet Get Post Web Browser doPost HTTP request service doPut Put Delete doDelete • The doGet, doPost, doPut, doDelete methods are of the same general form as the service method. • The service method of HttpServlet looks at the type of the HTTP request and then calls the appropriate handler methods.

  29. The Second Servlet Example • Refer to Asn2Sample.java and Asn2Sample.html in http://ugweb.cs.ualberta.ca/~c391/tutorial/servletbasics.html • Acces servlet using the HTML form • “Subclass” the HttpServlet class rather than the GenericServlet class

  30. Servlet Resources • Mark Wutka, Using Java Server Pages and Servlets, 2000, Que. • http://java.sun.com/developer/onlineTraining/Servlets/Fundamentals/contents.html • http://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets.html • Other resources: • http://ugweb.cs.ualberta.ca/~c391

  31. Servlet Exercise • create and populate a table in your Oracle account (At least 2 columns & 4 records) • extend Example 3.3 in servletbasics.html • servlet should connect to Oracle database • execute the SQLStatement received from the browser (e.g. select * from “YourTable”;) • display ResultSet on the browser(Tip: use ResultSetMetaData presented in JDBC tutorial) • display an error if the SQL statement was not executed correctly in Oracle (e.g. select * from “Wrong table”)

More Related