Web Based Applications, Tomcat and Servlets - Lab 3
Total Page:16
File Type:pdf, Size:1020Kb
CMPUT 391 Database Management Systems Web based Applications, Tomcat and Servlets - Lab 3 - CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta The Basic Web Server Lab 3 CMPUT 391 – Database Management Systems 2 Tomcat and Servlets 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) Lab 3 CMPUT 391 – Database Management Systems 3 Tomcat and Servlets CGI: Illustration Browser Browser Browser Browser GET Response Form.cgi Web Server Web Server Web Server Web Server Spawn an Read environment external program variables Response Start form.cgi CGfoI rPmr.ocggria m CGI Program CGI Program CteGrIm Pinroagterasm time Lab 3 CMPUT 391 – Database Management Systems 4 Tomcat and Servlets 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 Lab 3 CMPUT 391 – Database Management Systems 5 Tomcat and Servlets 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 Lab 3 CMPUT 391 – Database Management Systems 6 Tomcat and Servlets Servlets: Illustration Browser Browser Browser Browser GET Response /Form Java Web Server Java Web Server Java Web Server Java Web Server Request Response Create an Form Instance of Form Form Servlet the servlet if it Servlet Servlet is not loaded Remains loaded time Lab 3 CMPUT 391 – Database Management Systems 7 Tomcat and Servlets 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 Lab 3 CMPUT 391 – Database Management Systems 8 Tomcat and Servlets Typical Servlet Engine Implementation Servlet Engine Web Server Servlet 1 HTTP Custom TCP/IP traffic Protocol Browser API dispatcher Servlet 2 Servlet N Tomcat A Servlet Engine for Apache Web Server Lab 3 CMPUT 391 – Database Management Systems 9 Tomcat and Servlets 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 Lab 3 CMPUT 391 – Database Management Systems 10 Tomcat and Servlets 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 Lab 3 CMPUT 391 – Database Management Systems 11 Tomcat and Servlets 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 Lab 3 CMPUT 391 – Database Management Systems 12 Tomcat and Servlets 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) Lab 3 CMPUT 391 – Database Management Systems 13 Tomcat and Servlets 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 Lab 3 CMPUT 391 – Database Management Systems 14 Tomcat and Servlets Mapping URL to Machine Directory Put your plain HTML Catalina files here Webapps http://ui???.cs.ualberta.ca:portNum/proj1 proj1 <form name=“FirstServlet" method="GET" action=“firstservlet"> WEB-INF Put your servlets here classes http://ui???.cs.ualberta.ca:portNum/proj1/servlet Lab 3 CMPUT 391 – Database Management Systems 15 Tomcat and Servlets 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 Lab 3 CMPUT 391 – Database Management Systems 16 Tomcat and Servlets 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 Lab 3 CMPUT 391 – Database Management Systems 17 Tomcat and Servlets What Does Your First Servlet Look Like? Lab 3 CMPUT 391 – Database Management Systems 18 Tomcat and Servlets The FirstServlet In-Depth import java.io.*; The javax.servlet and javax.servlet.http import javax.servlet.*; packages provide interfaces and classes import javax.servlet.http.*; for writing servlets. 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>"); } } Lab 3 CMPUT 391 – Database Management Systems 19 Tomcat and Servlets The FirstServlet In-Depth import java.io.*; import javax.servlet.*; Implement the Servlet interface: subclassing the existing import javax.servlet.http.*; GenericServlet class that implements Servlet 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>"); } } Lab 3 CMPUT 391 – Database Management Systems 20 Tomcat and Servlets The FirstServlet In-Depth import java.io.*; import javax.servlet.*; Invoke the servlet’s import javax.servlet.http.*; service method public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { The service method takes two arguments: • ServletRequest: containing info about // Tell the Web browser that the response is HTML the request from the browser. response.setContentType("text/html"); • ServletRespone: containing info about the response going back. // 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>"); } } Lab 3 CMPUT 391 – Database Management Systems 21 Tomcat and Servlets 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"); Tell the Web browser what will // Get the Printwriter for writing out the response be returned is HTML. Required! 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>"); } } Lab 3 CMPUT 391 – Database Management Systems 22 Tomcat and Servlets 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 { The response object has the methods necessary to get an output stream for // Tell the Web browser that the response is HTML writing a response. response.setContentType("text/html"); A general rule of thumb: // Get the Printwriter for writing out the response • Writing out text: use a PrintWriter object PrintWriter out = response.getWriter(); by calling getWriter() • Sending binary data to the browser: use // Write the HTML back to the browser a ServletOutputStream by calling out.println("<html>"); getOutputStream() out.println("<body>"); out.println("<h1>Welcome to CMPUT391