Please send questions to
st10@humboldt.edu .
CIS 480 - Advanced Java
Week 14, April 30, 2001
* note how-to overview for servlets, along with today's examples
on the course web page;
INTRO TO SERVLETS
* interesting sources
* O'Reilly's http://servlets.com
1) http://java.sun.com/docs/glossary.html
2) Deitel and Deitel, "Java How to Program", 3rd Edition,
Chapter 19, "Servlets", pp. 935-979
3) Dale Dougherty, "Understanding Java Servlets", at
http://www.webreview.com/97/10/10/feature/colton.html
(reached from servlets.com page, list of articles)
4) Oracle Application Server Documentation, at
http://sorrel.humboldt.edu:8889/doc/oin/index_ms.htm,
likely available only from on-campus (but I am not sure).
Search for "servlet", and follow link to Tutorial...
5) http://www.webreview.com/97/10/10/feature/main.html,
Developing Java Servlets by William Crawford
6) http://www.esperanto.org.nz/jsp/jspfaq.html
(one of the FAQ's from
http://servlets.com/resources/urls/document.html)
7) http://www.faqtory.com/servlets/main.jsp
8) Java's Servlets tutorial
http://java.sun.com/docs/books/tutorial/servlets/TOC.html
* WHAT IS A SERVLET?
* from Sun's Java glossary ([1] above)
"servlet: A Java program that extends the functionality of a
Web server, generating dynamic content and interacting with
Web clients using a request-response paradigm."
* from [2]:
* "Basically, a servlet is the opposite end of an applet.
* A servlet can almost be thought of as a server-side applet.
* Servlets run inside the Web server in the way that applets
run inside the Web browser.
* The browser can submit a request to execute a servlet
directly; it can be stand-alone in terms of its actions --
as a browser can request an applet directly."
* a servlet can be an alternative to CGI scripts...
* WHAT SOFWARE IS REQUIRED?
* well, these servlets run inside a Web server --- so, you need a computer that is a Web server!
* in our case, that's sorrel;
* next: (from [3]) "To run a Java servlet, ... you need ... a Java
Virtual Machine (JVM) running on ...[your Web] server.
* It's analogous to running Java applets in a browser.
* The browser that supports applets must be running a JVM.
* So, for a server to run a servlet, it has to be running a JVM."
* and (also from [3]) --- that Web server "also has to support the
Java Servlet API.
* This API, developed by JavaSoft, defines how and when the
servlet communicates with the server.
* Essentially, the Servlet API is a well-defined set of
function calls to get and send information to and from the
server.
* The servlet needs to be able to access server-defined
variables, issue redirects, send error messages and the
like."
* from [7]: "What do I need to use servlets?"
* servlet classes --- publicly available from Sun, also
incorporated into many web servers, (such as our Oracle Web Server on
sorrel?)
* (you'll need these to compile your servlets!)
* "In addition to the servlet classes, you need a servlet
engine. There are three flavors of servlet engines:
standalone, add-on, and embeddable.
* A Standalone servlet engine is a web server with
built-in support for Servlets. An add-on servlet engine
functions as plug-in to an existing server. An embeddable
servlet engine is generally a lightweight servlet engine
that can be embedded in another application."
* SOME THINGS TO NOTE ABOUT SERVLETS...
* (from [3])
* "There's a single Java virtual machine running on the server and the
servlet is loaded once when it is called.
* It's not loaded again until the servlet changes, and a
modified servlet can be re-loaded without restarting the
server.
* The servlet stays resident in memory ....
* Static or persistent information can be shared across
multiple invocations of the servlet, allowing you to share
information between multiple users.
* For example, if I want to submit something to a database, I
can start the task in a separate Java thread and tell the
user to come back in 15 minutes."
* also in [3]: you are less dependent on whether a browser runs the
"right" version of Java --- because the servlet is running on the
server, and NOT on the client;
* you just need to make sure that your Web server is running
the right version of Java --- then, anyone who can reach the servlet
can make use of it
* (I note that I can run the servlet called from
HelloServlet.html and HelloGeorge.html just fine on poor
old Netscape 4.0 in the NHW 244 lab!!!)
* also in [3]: "One more important thing, servlets are modular; each
servlet can perform a specific task and then you can tie them
together.
* Servlets can talk to each other.
* I can do something called "servlet chaining." I can take the
output of one servlet and forward it to the next servlet
to process.
* This is very powerful because the servlets themselves don't
need to know that they are going to be used that way."
* BUT!!! Please note that servlet chaining is NOT always
supported; the Oracle Application Server Documentation for
the Oracle Web Server we are using to run our servlets on
sorrel states that the Oracle Web Server does not support
servlet chaining.
* from [4]: modifying an example from Oracle Application Server documentation:
* example st10HelloServlet.java
// notice the packages imported...
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
// notice that you extend HttpServlet...
public class st10HelloServlet extends HttpServlet
{
// [SST] "Handle the HTTP GET method by building a
// simple web page."
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out;
// [SST]"set content type and other response
// header fields first"
response.setContentType("text/html");
// [SST] "then write the data of the response"
out = response.getWriter();
out.println("<HTML><BODY>");
out.println("<H2>Hello World</H2>");
out.println("</BODY></HTML>");
} // doGet
} // HelloServlet
* BUT --- if you simply type this in on sorrel and try to
compile it, you will get complaints about not being able
to find the import classes javax.servlet and
javax.servlet.http...
* see the servlet how-to information on the course
web page, mentioned earlier;
* REMEMBER that the CLASSPATH command is
one long line!
* remember to:
* set environment variables (such as CLASSPATH!) necessary
so that javac can find the servlet classes when compiling
your servlet;
* import the javax.servlet and javax.servlet.http packages,
along with the java.io package
* once your servlet is compiled, copy the resulting
.class file to your home directory, or some directory
of your choice;
* REMEMBER --- you should start each with
your sorrel username, to head off name
collisions;
* make sure that it is world-READABLE and world-EXECUTABLE
(chmod 755 abc13YourServlet.class)
* you can either call this from a web page (look at
source for
http://www.humboldt.edu/~st10/cis480j/st10HelloServlet.html)
OR use a URL such as this one for servlet st10HelloServlet:
http://sorrel.humboldt.edu:8895/jservf/st10/st10HelloServlet
...substituting the name of YOUR servlet, of course;
* from [6]: So, what is JSP and how does THAT fit in here?
* "JSP is a dynamic scripting capability for web pages that
allows Java as well as a few special tags to be embedded
into a web file (HTML/XML, etc).
* The suffix traditionally ends with .jsp to indicate to the
web server that the file is a JSP file.
* JSP is a server side technology - you can't do any client
side validation with it.
* JSP files actually get compiled into Servlets, so what is
the point? Why not just write Servlets?
* For most people, the benefit is twofold:
* The focus is on HTML.
* Java and the JSP extensions assist in making
the HTML more functional.
* Servlets on the other hand allow outputting
of HTML but it is a tedious process.
* It is easy to make a change and then let the
JSP capability of the Web Server you are
using deal with compiling it into a
Servlet and running it.
* SOME SERVLET PACKAGE DETAIL and DISCUSSION
(from Java's servlet tutorial, starting at
http://java.sun.com/docs/books/tutorial/servlets/TOC.html)
* "The javax.servlet package provides interfaces and classes for
writing servlets. "
* "The central abstraction in the Servlet API is the Servlet
interface. All servlets implement this interface, either directly
or, more commonly, by extending a class that implements it such as
HttpServlet."
* that's what we're doing in HelloServlet.java and
HelloGeorge.java --- extending HttpServlet, which is
itself an extension of the Servlet interface...
* "The Servlet interface declares, but does not implement, methods
that manage the servlet and its communications with
clients. Servlet writers provide some or all of these methods when
developing a servlet."
* "Client Interaction"
* "When a servlet accepts a call from a client, it receives
two objects:
* A ServletRequest, which encapsulates the
communication from the client to the server.
* A ServletResponse, which encapsulates the
communication from the servlet back to the
client.
* ServletRequest and ServletResponse are interfaces defined by
the javax.servlet package. "
* note the HttpServletRequest and HttpServletResponse
parameters in the doGet() method in HelloServlet,
HelloGeorge;
* "The ServletRequest Interface"
* "The ServletRequest interface allows the servlet access to:
* Information such as the names of the parameters
passed in by the client, the protocol (scheme)
being used by the client, and the names of the
remote host that made the request and the
server that received it.
* The input stream, ServletInputStream. Servlets use
the input stream to get data from clients that
use application protocols such as the HTTP
POST and PUT methods.
* Interfaces that extend ServletRequest interface allow the
servlet to retrieve more protocol-specific data. For
example, the HttpServletRequest interface contains methods
for accessing HTTP-specific header information."
* "The ServletResponse Interface"
* The ServletResponse interface gives the servlet methods for
replying to the client. It:
* Allows the servlet to set the content length and
MIME type of the reply.
* Provides an output stream, ServletOutputStream, and
a Writer through which the servlet can send
the reply data.
* Interfaces that extend the ServletResponse interface give
the servlet more protocol- specific capabilities. For
example, the HttpServletResponse interface contains
methods that allow the servlet to manipulate HTTP-specific
header information."
* so, note: HttpServletRequest's getWriter() method, used in
HelloServlet and HelloGeorge, to get a PrintWriter output
stream to write HTML to that then shows up within the web
browser as a result of this servlet's running;
* "Additional Capabilities of HTTP Servlets"
* The classes and interfaces described above make up a basic
Servlet.
* HTTP servlets have some additional objects that provide
session-tracking capabilities.
* The servlet writer can use these APIs to maintain state
between the servlet and the client that persists across
multiple connections during some time period.
* HTTP servlets also have objects that provide cookies. [!!]
The servlet writer uses the cookie API to save data with
the client and to retrieve this data."
* SO --- again consider st10HelloServlet.java:
// notice the packages imported...
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
// notice that you extend HttpServlet...
public class st10HelloServlet extends HttpServlet
{
// [SST] "Handle the HTTP GET method by building a
// simple web page."
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out;
// [SST]"set content type and other response
// header fields first"
response.setContentType("text/html");
// [SST] "then write the data of the response"
out = response.getWriter();
out.println("<HTML><BODY>");
out.println("<H2>Hello World</H2>");
out.println("</BODY></HTML>");
} // doGet
} // st10HelloServlet
* from [8]:
* "The classes mentioned in the Architecture of the Servlet
Package section are shown in the example in bold:
* ....extends the HttpServlet class, which implements
the Servlet interface.
* ....overrides the doGet method in the
HttpServlet class. The doGet method is
called when a client makes a GET request
(the default HTTP request method), and
results in the simple HTML page being
returned to the client.
* Within the doGet method,
* The user's request is represented by an
HttpServletRequest object.
* The response to the user is represented by an
HttpServletResponse object.
* Because text data is returned to the client, the
reply is sent using the Writer object obtained
from the HttpServletResponse object."
* notice that, in the very-similar servlet st10GreetMe.java, you see a
reminder of how to handle it if you want a double-quote character in
your HTML string: put a backslash in front of it to tell Java that a
double-quote character is desired...