CS 639 Class 7
Return pa1a
Note (2/15): notes for pa1b schema testing now available, linked with pa1 on the class web page.
Another note on XSL: the example last time assumed browsers would process the XSL stylesheet. But of course Java can do it too. See $cs639/xsl with build.xml and tiny Java program that uses the XSLT processor of JDK 1.6, if you’re interested. We are not officially covering XSL.
SOAP
· uses SOAP Envelope around XML messages designed for the particular service
· XML messages are typically described by XML Schema
· Is platform-neutral: can have JEE server, .NET client or vice versa (sometimes problems, though)
· WSDL (Web Services Description Language) is an XML document used for describing SOAP services
· In particular, there are tools that read WSDL and create client app skeletons, for both JEE and .NET
SOAP over HTTP
· uses POST only
· ignores headers except possibly Content-length
· typically uses a single URL “service endpoint” at a particular server, no query string
SOAP web services took over (2000-2005 say) from older technologies that were much less open and portable, a big step forward. Now REST is more popular.
Quick Intro to
REST
The idea of REST is to use HTTP directly, rather than reducing it to a carrier of SOAP messages. With REST, we use multiple HTTP verbs:
· GET for reading data (no changes!)
· POST for creating new data items
· PUT for updating old data items
· DELETE for deleting old data items
Flip through Chap. 4 to see these actions in diagrams.
How do we specify the data item to work on?
In REST, we use individual URLs for data items, i.e. “resources”.
Chap. 4 example: pg. 57:
GET /order/1234 read order # 1234 (in XML)
POST /order add a new order (server determines new id).
(also PUT and DELETE)
How does the client find out the new id after a POST?
The HTTP Location header in the response gives this. See pg. 60 for full response: HTTP headers + XML body
So we see that REST uses HTTP directly, including its headers. So we don’t have a “REST protocol” or “REST envelope” in the usual sense, although there are strong conventions on how to use HTTP for REST.
SOAP and REST both need a HTTP server, i.e., application server, to do the necessary actions.
Now we see the basic idea of SOAP and REST. We need to turn to the execution environment for web services under J2EE, i.e., servlets, so we’ll talk about tomcat, and you will be able to install it on Linux sf08 and on your home PC.
Tomcat is a web server, and thus runs a server process, listening on a certain TCP port. Though web servers usually listen on port 80, tomcat normally runs on another port, often 8080, to avoid conflicting with the “real” web server.
For example, eoneil's tomcat runs on port 11600 on sf08.cs.umb.edu.
Browsing to http://sf08.cs.umb.edu:11600 will show the tomcat default home page with the tomcat icon
You will need a specific port to run your own copy of tomcat on Linux. At home or in the weblab, you can use the “normal” port 8080.
Actually you need 2 ports, one for the service and one to command tomcat to stop.
TCP ports at CS.UMB (UNIX/Linux systems)
· port 80 is reserved for site webservers, like our whole CS department
· low-numbered ports are “well known” ports (see /etc/services on any Unix machine) with definite assignments, like ssh
· port numbers above 1023 are non-priviledged on Unix (any user can run a server on them, but should be authorized locally)
· at UMB we have static allocation of port numbers by professor. numbers 51250 – 51499 are reserved for eoneil
· each class member will get 5 port numbers, good on any Unix/Linux machine at our site. You'll run Tomcat on these ports.
Note the new Resource links to a tutorial on servlets if you have time before next class to get started.
Handout: Example servlets that come with tomcat. You can find them via links from the Tomcat home page.
Tomcat
· Tomcat is a webserver and a servlet container
· the Tomcat home page has links to JSP examples and servlet examples
· eoneil’s tomcat is running on sf08.cs.umb.edu on port 11600: browse to http://sf08.cs.umb.edu:11600
· read the servlet tutorials linked under Resources on the class web page
JSP vs. servlets
· JSP is a server-side scripting language usually used for producing HTML, but also can produce XML.
· We won't be using it in this course, since we will be using the underlying technology, servlets, directly
· JSP is compiled into a servlet, so servlets are more basic
· PHP is another server-side scripting language, also ASP for Microsoft .NET
· Also there are client-side scripting languages, notably Javascript
Look at the HelloWorld servlet, which can be found by browsing to http://sf08.cs.umb.edu:11600 and following the link to Example Servlets
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest
request, HttpServletResponse response)
throws IOException,
ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
· The HttpServlet object provides a framework for constructing servlets and provides many services
· In the HelloWorld servlet, the code overrides the the doGet method for handling GET requests
· The HttpServletRequest and HttpServletResponse provided as arguments to doGet are important objects, ready to use
· The first thing to do is to set the content type. This is a form of typing for the output message.
· This content type sets encoding character set for response object's Writer: Latin-1 for text/html, UTF-8 for text/xml.
· Here we see text/html, so the Latin1 character set (ISO 8859) is used here.
Execution:
The user browses to http://sf08.cs.umb.edu:11600/examples/servlets/servlet/HelloWorldExample,
The browser will connect to sf08.cs.umb.edu on port 11600 and send the following:
GET /examples/servlets/servlet/HelloWorldExample HTTP/1.1
(header)
and receives back from this servlet:
(header)
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
+ the second example is Request Info. Here doPost calls doGet, which is often done in servlets, as explained below
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RequestInfo extends HttpServlet {
public void
doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException,
ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Request Information
Example</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3>Request Information Example</h3>");
out.println("Method: " + request.getMethod());
out.println("Request URI: " + request.getRequestURI());
out.println("Protocol: " + request.getProtocol());
out.println("PathInfo: " + request.getPathInfo());
out.println("Remote Address: " + request.getRemoteAddr());
out.println("</body>");
out.println("</html>");
}
/**
* We are going to perform
the same operations for POST requests
* as for GET methods, so
this method just sends the request to
* the doGet method.
*/
public void
doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException,
ServletException
{
doGet(request, response);
}
}
doPost can call doGet because the query string appears after the ? in the URI in a GET
- is contained in the body of a POST
- both are encoded the same way
- the web container parses the query string and puts the results in the request object as parameters
- this enables both doGet and doPost to access the parameters in the same way
For REST, we’ll want to use doDelete and doPut as well—follow the “JEE API” link on the class web page for details.
Using telnet for HTTP
telnet is the bare-bones way for a user to use a TCP/IP stream connection. You fire telnet up and then what you type goes out on the stream connection, and what comes back is shown on the screen. You can talk directly to various servers.
You don't need a browser to send an HTTP request. you can use telnet:
telnet sf08.cs.umb.edu 11600
GET / HTTP/1.0
(blank line to signify the end of the header)
Note: use HTTP/1.0 to avoid having to fill in anything real in the header. Browsers use HTTP/1.1.
CATALINA_HOME is an environment variable that specifies the top level directory for Tomcat
we can get Tomcat from $cs639/tomcat.zip for home use, directions to come.
the Tomcat directory structure looks like this:
$CATALINA_HOME or %CATALINA_HOME%
bin - for Tomcat itself: we use startup.bat, shutdown.bat, startup.sh, shutdown.sh
conf - contains server.xml, which we'll need to edit to put in the port numbers
logs - log files. look here when in trouble
webapps - web applications go here
… (ignore for now)