28
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications, Tomcat and Servlets - Lab 3 -

Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

CMPUT 391 – Database Management SystemsDepartment of Computing Science

University of Alberta

CMPUT 391Database Management Systems

Web based Applications, Tomcat and Servlets

- Lab 3 -

Page 2: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 2

The Basic Web Server

Page 3: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 3

CGI

CGI – Common Gateway InterfaceProcessing 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)

Page 4: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 4

CGI: Illustration

CGI Program

Browser

Web Server

CGI Program

Browser

Web Server

Start form.cgi

Browser

Web Server

CGI Program

Browser

Web Server

CGI Programform.cgi

terminates

GETForm.cgi

Spawn anexternal program

Read environment variables Response

Response

time

Page 5: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 5

ASP

ASP – Active Server PagesMicrosoft’s answers to CGI programmingASP page - a Web page with code embedded inside, interpreted by the Web serverUsing special HTML tags, VBScript/JavaScript code can be inserted

Page 6: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 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

Page 7: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 7

Servlets: Illustration

Browser Browser Browser Browser

GET/servlet/Form

Create anInstance of

the servlet if itis not loaded

Java Web Server Java Web Server

FormServlet

Request

Java Web Server

FormServlet

Response

Response

FormServlet

Java Web Server

Remains loaded

time

Page 8: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 8

Servlets Support in Existing Servers

Apache Web Server& some other Java-enabled Web serversServlet engine

Usually a separate process, not within the same process at the Web server

e.g. Tomcat

Page 9: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 9

JSP

JSP – Java Server Pages - Java Version of ASP- A Web page with Java code embeddedinside it and running on the Web serverJava’s answer to both CGI and ASPCompiled into servletsJSP & servlets can easily interact

Page 10: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 10

Tomcat

Servlet &JSP engine for Apache Web ServerAn open-source package

part of the Apache Software Foundation’s Jakarta projectThe Tomcat Web site: – http://jakarta.apache.org/tomcat/index.html

Page 11: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 11

Installing Tomcat

Setting the CLASSPATH: the same classpath settings as your JVMChange the CLASSPATH environment variable from your login shell– Follow the instructions in:

http://ugweb.cs.ualberta.ca/~c391/tutorial/softwInstall.html

Test your installation

Page 12: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 12

Installation Tips

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

Page 13: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 13

URL Mapping

Unlike ASP, JSP, etc., servlets have no extension:

<form name=“FirstServlet" method="GET" action="servlet/FirstServlet">

A special URL mapping for the servletdirectory, which signals that you want to run a servlet.In our lab, the special URL is like:

http://<ui???>.cs.ualberta.ca:<Port1>/proj1/servletMachine

Name …/proj1/servlet/FirstServlet

Page 14: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 14

Mapping URL to Machine Directory

classes

WEB-INF

proj1

Webapps

Catalina

http://csu???.cs.ualberta.ca:portNum/proj1

http://csu???.cs.ualberta.ca:portNum/proj1/servlet

Put your plain HTML files here

Put your servletshere

<form name=“FirstServlet" method="GET" action="servlet/FirstServlet">

Page 15: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 15

Your First Servletimport 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 responsePrintWriter out = response.getWriter();

// Write the HTML back to the browserout.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

Page 16: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 16

Compiling the Servlet

Edit the file “FirstServlet.java”javac FirstServlet.javaStart your Tomcat

Launch a browser, input the URL like:http://ui00.cs.ualberta.ca:16410/proj1/servlet/FirstServlet

Your Port 1

Page 17: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 17

What Does Your First Servlet Look Like?

Page 18: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 18

The FirstServlet In-Depthimport 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 HTMLresponse.setContentType("text/html");

// Get the Printwriter for writing out the responsePrintWriter out = response.getWriter();

// Write the HTML back to the browserout.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.httppackages provide interfaces and classes for writing servlets.

Page 19: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 19

The FirstServlet In-Depthimport 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 HTMLresponse.setContentType("text/html");

// Get the Printwriter for writing out the responsePrintWriter out = response.getWriter();

// Write the HTML back to the browserout.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

Page 20: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 20

The FirstServlet In-Depthimport 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 HTMLresponse.setContentType("text/html");

// Get the Printwriter for writing out the responsePrintWriter out = response.getWriter();

// Write the HTML back to the browserout.println("<html>"); out.println("<body>");out.println("<h1>Welcome to CMPUT391 Lab!</h1>");out.println("</body>");out.println("</html>");}

}

Invoke the servlet’sservice method

The service method takes two arguments:• ServletRequest: containing info about the request from the browser.• ServletRespone: containing info about the response going back.

Page 21: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 21

The FirstServlet In-Depthimport 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 HTMLresponse.setContentType("text/html");

// Get the Printwriter for writing out the responsePrintWriter out = response.getWriter();

// Write the HTML back to the browserout.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!

Page 22: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 22

The FirstServlet In-Depthimport 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 HTMLresponse.setContentType("text/html");

// Get the Printwriter for writing out the responsePrintWriter out = response.getWriter();

// Write the HTML back to the browserout.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.

Page 23: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 23

The FirstServlet In-Depthimport 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 HTMLresponse.setContentType("text/html");

// Get the Printwriter for writing out the responsePrintWriter out = response.getWriter();

// Write the HTML back to the browserout.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.

Page 24: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 24

The HttpServlet Class

It has extra methods and special request-and-response objects that are geared toward the HTTP protocolThe 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;

Page 25: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 25

The HttpServlet Class

service

doGet

doPost

doPut

doDelete

Web Browser

GetPost

PutDelete

• 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.

HTTP request

Page 26: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 26

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

Page 27: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 27

Servlet Resources

Mark Wutka, Using Java Server Pages and Servlets, 2000, Que.http://java.sun.com/developer/onlineTraining/Servlets/Fundamentals/contents.htmlhttp://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets.htmlOther resources:– http://ugweb.cs.ualberta.ca/~c391

Page 28: Web based Applications, Tomcat and Servlets - Lab 3ugweb.cs.ualberta.ca/~c391/F06/resources/LabSlides/L3-391-F06.pdf · Web based Applications, Tomcat and Servlets - Lab 3 - Lab 3

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 28

Servlet Exercise

create and populate a table called “CMPUT” in your Oracle account– At least 2 columns(CourseID and CourseNAME) & – 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 “CMPUT”;)– 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”)