|
CSCI A348/548
|
Projects.
Grad students extra projects.
JavaScript shopping cart.
SQL, JDBC, XML, JSP.
(PHP ?)
SSL
Java.
1. Overview of Java servlets.
Apache Tomcat.
2. First servlets
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Servlet template.
* <p>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class ServletTemplate extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Use "request" to read incoming HTTP headers
// (e.g. cookies) and HTML form data (e.g. data the user
// entered and submitted).
// Use "response" to specify the HTTP response status
// code and headers (e.g. the content type, cookies).
PrintWriter out = response.getWriter();
// Use "out" to send content to browser
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Very simplistic servlet that generates plain text.
* <p>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
cd $myServlets until now.
Why.
Anywhere else?
Start and stop Tomcat.
Contexts?
(By the way:
insetenv startTomcat $TOMCAT_HOME/bin/startup.sh setenv stopTomcat $TOMCAT_HOME/bin/shutdown.sh
.cshrc). Now what?
We'll be working in
Let's call that$TOMCAT_HOME/webapps/ROOT/WEB-INF/classes
$coreServlets or anything you want.
So we put it there and call it byburrowww.cs.indiana.edu% pwd /nfs/paca/home/user1/dgerman burrowww.cs.indiana.edu% tail -6 ~/.cshrc setenv CLASSPATH $JMFHOME/lib/jmf.jar:$CLASSPATH setenv startTomcat $TOMCAT_HOME/bin/startup.sh setenv stopTomcat $TOMCAT_HOME/bin/shutdown.sh setenv coreServlets $TOMCAT_HOME/webapps/ROOT/WEB-INF/classes burrowww.cs.indiana.edu% source ~/.cshrc burrowww.cs.indiana.edu% echo $coreServlets /u/dgerman/apache/jakarta-tomcat-3.2.1/webapps/ROOT/WEB-INF/classes burrowww.cs.indiana.edu% cd $coreServlets burrowww.cs.indiana.edu%
21xxx/servlet/HelloWorld
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet that generates HTML.
* <p>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class HelloWWW extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
"<BODY>\n" +
"<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
Again, we place this in the same place. And we use the same URL mapping
Also, packaging servlets (see next step).http://burrowww.cs.indiana.edu:21xxx/servlet/HelloWorld
CGI.pm-type of utility functions.
First create this file in $coreServlets
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet that generates HTML.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class HelloWWW2 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
"<BODY>\n" +
"<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
and then add this one:
package coreservlets;
import javax.servlet.*;
import javax.servlet.http.*;
/** Some simple time savers. Note that most are static methods.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class ServletUtilities {
public static final String DOCTYPE =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">";
public static String headWithTitle(String title) {
return(DOCTYPE + "\n" +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n");
}
/** Read a parameter with the specified name, convert it
* to an int, and return it. Return the designated default
* value if the parameter doesn't exist or if it is an
* illegal integer format.
*/
public static int getIntParameter(HttpServletRequest request,
String paramName,
int defaultValue) {
String paramString = request.getParameter(paramName);
int paramValue;
try {
paramValue = Integer.parseInt(paramString);
} catch(NumberFormatException nfe) { // null or bad format
paramValue = defaultValue;
}
return(paramValue);
}
/** Given an array of Cookies, a name, and a default value,
* this method tries to find the value of the cookie with
* the given name. If there is no cookie matching the name
* in the array, then the default value is returned instead.
*/
public static String getCookieValue(Cookie[] cookies,
String cookieName,
String defaultValue) {
if (cookies != null) {
for(int i=0; i<cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
}
return(defaultValue);
}
/** Given an array of cookies and a name, this method tries
* to find and return the cookie from the array that has
* the given name. If there is no cookie matching the name
* in the array, null is returned.
*/
public static Cookie getCookie(Cookie[] cookies,
String cookieName) {
if (cookies != null) {
for(int i=0; i<cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie);
}
}
return(null);
}
/** Given a string, this method replaces all occurrences of
* '<' with '<', all occurrences of '>' with
* '>', and (to handle cases that occur inside attribute
* values), all occurrences of double quotes with
* '"' and all occurrences of '&' with '&'.
* Without such filtering, an arbitrary string
* could not safely be inserted in a Web page.
*/
public static String filter(String input) {
StringBuffer filtered = new StringBuffer(input.length());
char c;
for(int i=0; i<input.length(); i++) {
c = input.charAt(i);
if (c == '<') {
filtered.append("<");
} else if (c == '>') {
filtered.append(">");
} else if (c == '"') {
filtered.append(""");
} else if (c == '&') {
filtered.append("&");
} else {
filtered.append(c);
}
}
return(filtered.toString());
}
}
Now compile them:
javac -d . He*W2.java S*Util*.javaWhere are the
.class files being placed? Then access the new packaged servlet from
http://burrowww.cs.indiana.edu:21xxx/servlet/coreservlets.HelloWWW2Note the dot(s).
Also, note that something is (as of yet) unused.
What's the largest such thing?
Here's a hint:
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet that generates HTML. This variation of
* HelloWWW uses the ServletUtilities utility class.
* <p>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class HelloWWW3 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(ServletUtilities.headWithTitle("Hello WWW") +
"<BODY>\n" +
"<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
init method
service method
doGet,
doPost,
doXxx methods
SingleThreadModel interface
destroy method
The rule is: For complex initializations, store the data in a separate file, and use the init parameters to give the location of that file.
Bring this in:
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Example using servlet initialization. Here, the message
* to print and the number of times the message should be
* repeated is taken from the init parameters.
* <p>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class ShowMessage extends HttpServlet {
private String message;
private String defaultMessage = "No message.";
private int repeats = 1;
public void init(ServletConfig config)
throws ServletException {
// Always call super.init
super.init(config);
message = config.getInitParameter("message");
if (message == null) {
message = defaultMessage;
}
try {
String repeatString = config.getInitParameter("repeats");
repeats = Integer.parseInt(repeatString);
} catch(NumberFormatException nfe) {
// NumberFormatException handles case where repeatString
// is null *and* case where it is something in an
// illegal format. Either way, do nothing in catch,
// as the previous value (1) for the repeats field will
// remain valid because the Integer.parseInt throws
// the exception *before* the value gets assigned
// to repeats.
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "The ShowMessage Servlet";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>");
for(int i=0; i<repeats; i++) {
out.println(message + "<BR>");
}
out.println("</BODY></HTML>");
}
}
Be careful with (re)compilation. Then prepare this file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<servlet>
<servlet-name>
ShowMsg
</servlet-name>
<servlet-class>
coreservlets.ShowMessage
</servlet-class>
<init-param>
<param-name>
message
</param-name>
<param-value>
This is the message to be repeated.
</param-value>
</init-param>
<init-param>
<param-name>
repeats
</param-name>
<param-value>
5
</param-value>
</init-param>
</servlet>
</web-app>
This should be in $coreServlets/../web.xml (note the contrived path).
It would have been better to write: $TOMCAT_HOME/webapps/ROOT/WEB-INF/web.xml instead.
(Simply change the file you find there.)
The stop and re-start the server (use $stopTomcat
and $startTomcat defined).
Now check from :21xxx/servlet/coreservlets.ShowMessage
Something is not exactly right, is it?
What is it that needs to be fixed?
Check again from :21xxx/servlet/ShowMsg
Why does it work?
How do you pronounce shibboleth?.
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Example using servlet initialization and the
* getLastModified method.
* <p>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class LotteryNumbers extends HttpServlet {
private long modTime;
private int[] numbers = new int[10];
/** The init method is called only when the servlet
* is first loaded, before the first request
* is processed.
*/
public void init() throws ServletException {
// Round to nearest second (ie 1000 milliseconds)
modTime = System.currentTimeMillis()/1000*1000;
for(int i=0; i<numbers.length; i++) {
numbers[i] = randomNum();
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Your Lottery Numbers";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<B>Based upon extensive research of " +
"astro-illogical trends, psychic farces, " +
"and detailed statistical claptrap, " +
"we have chosen the " + numbers.length +
" best lottery numbers for you.</B>" +
"<OL>");
for(int i=0; i<numbers.length; i++) {
out.println(" <LI>" + numbers[i]);
}
out.println("</OL>" +
"</BODY></HTML>");
}
/** The standard service method compares this date
* against any date specified in the If-Modified-Since
* request header. If the getLastModified date is
* later, or if there is no If-Modified-Since header,
* the doGet method is called normally. But if the
* getLastModified date is the same or earlier,
* the service method sends back a 304 (Not Modified)
* response, and does <B>not</B> call doGet.
* The browser should used its cached version of
* the page in such a case.
*/
public long getLastModified(HttpServletRequest request) {
return(modTime);
}
// A random int from 0 to 99.
private int randomNum() {
return((int)(Math.random() * 100));
}
}
Now, test it. How do you access it?
(See WebClient below)
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/** A graphical client that lets you interactively connect to
* Web servers and send custom request lines and
* request headers.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class WebClient extends CloseableFrame
implements Runnable, Interruptible, ActionListener {
public static void main(String[] args) {
new WebClient("Web Client");
}
private LabeledTextField hostField, portField,
requestLineField;
private TextArea requestHeadersArea, resultArea;
private String host, requestLine;
private int port;
private String[] requestHeaders = new String[30];
private Button submitButton, interruptButton;
private boolean isInterrupted = false;
public WebClient(String title) {
super(title);
setBackground(Color.lightGray);
setLayout(new BorderLayout(5, 30));
int fontSize = 14;
Font labelFont =
new Font("Serif", Font.BOLD, fontSize);
Font headingFont =
new Font("SansSerif", Font.BOLD, fontSize+4);
Font textFont =
new Font("Monospaced", Font.BOLD, fontSize-2);
Panel inputPanel = new Panel();
inputPanel.setLayout(new BorderLayout());
Panel labelPanel = new Panel();
labelPanel.setLayout(new GridLayout(4,1));
hostField = new LabeledTextField("Host:", labelFont,
30, textFont);
portField = new LabeledTextField("Port:", labelFont,
"80", 5, textFont);
// Use HTTP 1.0 for compatibility with the most servers.
// If you switch this to 1.1, you *must* supply a
// Host: request header.
requestLineField =
new LabeledTextField("Request Line:", labelFont,
"GET / HTTP/1.0", 50, textFont);
labelPanel.add(hostField);
labelPanel.add(portField);
labelPanel.add(requestLineField);
Label requestHeadersLabel =
new Label("Request Headers:");
requestHeadersLabel.setFont(labelFont);
labelPanel.add(requestHeadersLabel);
inputPanel.add(labelPanel, BorderLayout.NORTH);
requestHeadersArea = new TextArea(5, 80);
requestHeadersArea.setFont(textFont);
inputPanel.add(requestHeadersArea, BorderLayout.CENTER);
Panel buttonPanel = new Panel();
submitButton = new Button("Submit Request");
submitButton.addActionListener(this);
submitButton.setFont(labelFont);
buttonPanel.add(submitButton);
inputPanel.add(buttonPanel, BorderLayout.SOUTH);
add(inputPanel, BorderLayout.NORTH);
Panel resultPanel = new Panel();
resultPanel.setLayout(new BorderLayout());
Label resultLabel =
new Label("Results", Label.CENTER);
resultLabel.setFont(headingFont);
resultPanel.add(resultLabel, BorderLayout.NORTH);
resultArea = new TextArea();
resultArea.setFont(textFont);
resultPanel.add(resultArea, BorderLayout.CENTER);
Panel interruptPanel = new Panel();
interruptButton = new Button("Interrupt Download");
interruptButton.addActionListener(this);
interruptButton.setFont(labelFont);
interruptPanel.add(interruptButton);
resultPanel.add(interruptPanel, BorderLayout.SOUTH);
add(resultPanel, BorderLayout.CENTER);
setSize(600, 700);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == submitButton) {
Thread downloader = new Thread(this);
downloader.start();
} else if (event.getSource() == interruptButton) {
isInterrupted = true;
}
}
public void run() {
isInterrupted = false;
if (hasLegalArgs())
new HttpClient(host, port, requestLine,
requestHeaders, resultArea, this);
}
public boolean isInterrupted() {
return(isInterrupted);
}
private boolean hasLegalArgs() {
host = hostField.getTextField().getText();
if (host.length() == 0) {
report("Missing hostname");
return(false);
}
String portString =
portField.getTextField().getText();
if (portString.length() == 0) {
report("Missing port number");
return(false);
}
try {
port = Integer.parseInt(portString);
} catch(NumberFormatException nfe) {
report("Illegal port number: " + portString);
return(false);
}
requestLine =
requestLineField.getTextField().getText();
if (requestLine.length() == 0) {
report("Missing request line");
return(false);
}
getRequestHeaders();
return(true);
}
private void report(String s) {
resultArea.setText(s);
}
private void getRequestHeaders() {
for(int i=0; i<requestHeaders.length; i++)
requestHeaders[i] = null;
int headerNum = 0;
String header =
requestHeadersArea.getText();
StringTokenizer tok =
new StringTokenizer(header, "\r\n");
while (tok.hasMoreTokens())
requestHeaders[headerNum++] = tok.nextToken();
}
}
Here are some supporting files.
This is file HttpClient.java
import java.awt.*;
import java.net.*;
import java.io.*;
/** The underlying network client used by WebClient.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class HttpClient extends NetworkClient {
private String requestLine;
private String[] requestHeaders;
private TextArea outputArea;
private Interruptible app;
public HttpClient(String host, int port,
String requestLine, String[] requestHeaders,
TextArea outputArea, Interruptible app) {
super(host, port);
this.requestLine = requestLine;
this.requestHeaders = requestHeaders;
this.outputArea = outputArea;
this.app = app;
if (checkHost(host))
connect();
}
protected void handleConnection(Socket uriSocket)
throws IOException {
try {
PrintWriter out = SocketUtil.getWriter(uriSocket);
BufferedReader in = SocketUtil.getReader(uriSocket);
outputArea.setText("");
out.println(requestLine);
for(int i=0; i<requestHeaders.length; i++) {
if (requestHeaders[i] == null)
break;
else
out.println(requestHeaders[i]);
}
out.println();
String line;
while ((line = in.readLine()) != null &&
!app.isInterrupted())
outputArea.append(line + "\n");
if (app.isInterrupted())
outputArea.append("---- Download Interrupted ----");
} catch(Exception e) {
outputArea.setText("Error: " + e);
}
}
private boolean checkHost(String host) {
try {
InetAddress.getByName(host);
return(true);
} catch(UnknownHostException uhe) {
outputArea.setText("Bogus host: " + host);
return(false);
}
}
}
This is NetworkClient.java
import java.net.*;
import java.io.*;
/** A starting point for network clients. You'll need to
* override handleConnection, but in many cases
* connect can remain unchanged. It uses
* SocketUtil to simplify the creation of the
* PrintWriter and BufferedReader.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class NetworkClient {
protected String host;
protected int port;
/** Register host and port. The connection won't
* actually be established until you call
* connect.
*/
public NetworkClient(String host, int port) {
this.host = host;
this.port = port;
}
/** Establishes the connection, then passes the socket
* to handleConnection.
*/
public void connect() {
try {
Socket client = new Socket(host, port);
handleConnection(client);
} catch(UnknownHostException uhe) {
System.out.println("Unknown host: " + host);
uhe.printStackTrace();
} catch(IOException ioe) {
System.out.println("IOException: " + ioe);
ioe.printStackTrace();
}
}
/** This is the method you will override when
* making a network client for your task.
* The default version sends a single line
* ("Generic Network Client") to the server,
* reads one line of response, prints it, then exits.
*/
protected void handleConnection(Socket client)
throws IOException {
PrintWriter out =
SocketUtil.getWriter(client);
BufferedReader in =
SocketUtil.getReader(client);
out.println("Generic Network Client");
System.out.println
("Generic Network Client:\n" +
"Made connection to " + host +
" and got '" + in.readLine() + "' in response");
client.close();
}
/** The hostname of the server we're contacting. */
public String getHost() {
return(host);
}
/** The port connection will be made on. */
public int getPort() {
return(port);
}
}
This is SocketUtil.java
import java.net.*;
import java.io.*;
/** A shorthand way to create BufferedReaders and
* PrintWriters associated with a Socket.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class SocketUtil {
/** Make a BufferedReader to get incoming data. */
public static BufferedReader getReader(Socket s)
throws IOException {
return(new BufferedReader(
new InputStreamReader(s.getInputStream())));
}
/** Make a PrintWriter to send outgoing data.
* This PrintWriter will automatically flush stream
* when println is called.
*/
public static PrintWriter getWriter(Socket s)
throws IOException {
// 2nd argument of true means autoflush
return(new PrintWriter(s.getOutputStream(), true));
}
}
This is CloseableFrame.java
import java.awt.*;
import java.awt.event.*;
/** A Frame that you can actually quit. Used as
* the starting point for most Java 1.1 graphical
* applications.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class CloseableFrame extends Frame {
public CloseableFrame(String title) {
super(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
/** Since we are doing something permanent, we need
* to call super.processWindowEvent <B>first</B>.
*/
public void processWindowEvent(WindowEvent event) {
super.processWindowEvent(event); // Handle listeners
if (event.getID() == WindowEvent.WINDOW_CLOSING)
System.exit(0);
}
}
This is LabeledTextField.java
import java.awt.*;
/** A TextField with an associated Label.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class LabeledTextField extends Panel {
private Label label;
private TextField textField;
public LabeledTextField(String labelString,
Font labelFont,
int textFieldSize,
Font textFont) {
setLayout(new FlowLayout(FlowLayout.LEFT));
label = new Label(labelString, Label.RIGHT);
if (labelFont != null)
label.setFont(labelFont);
add(label);
textField = new TextField(textFieldSize);
if (textFont != null)
textField.setFont(textFont);
add(textField);
}
public LabeledTextField(String labelString,
String textFieldString) {
this(labelString, null, textFieldString,
textFieldString.length(), null);
}
public LabeledTextField(String labelString,
int textFieldSize) {
this(labelString, null, textFieldSize, null);
}
public LabeledTextField(String labelString,
Font labelFont,
String textFieldString,
int textFieldSize,
Font textFont) {
this(labelString, labelFont,
textFieldSize, textFont);
textField.setText(textFieldString);
}
/** The Label at the left side of the LabeledTextField.
* To manipulate the Label, do:
* <PRE>
* LabeledTextField ltf = new LabeledTextField(...);
* ltf.getLabel().someLabelMethod(...);
* </PRE>
*
* @see #getTextField
*/
public Label getLabel() {
return(label);
}
/** The TextField at the right side of the
* LabeledTextField.
*
* @see #getLabel
*/
public TextField getTextField() {
return(textField);
}
}
And this is Interruptible.java
/** An interface for classes that can be polled to see
* if they've been interrupted. Used by HttpClient
* and WebClient to allow the user to interrupt a network
* download.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public interface Interruptible {
public boolean isInterrupted();
}
Put them all in a directory ~/mhall, compile and run WebClient.
3. Handling the Client Request: Form Data
4. Handling the Client Request: HTTP Request Headers
5. Accessing the Standard CGI Variables
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
/** Creates a table showing the current value of each
* of the standard CGI variables.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class ShowCGIVariables extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String[][] variables =
{ { "AUTH_TYPE", request.getAuthType() },
{ "CONTENT_LENGTH",
String.valueOf(request.getContentLength()) },
{ "CONTENT_TYPE", request.getContentType() },
{ "DOCUMENT_ROOT",
getServletContext().getRealPath("/") },
{ "PATH_INFO", request.getPathInfo() },
{ "PATH_TRANSLATED", request.getPathTranslated() },
{ "QUERY_STRING", request.getQueryString() },
{ "REMOTE_ADDR", request.getRemoteAddr() },
{ "REMOTE_HOST", request.getRemoteHost() },
{ "REMOTE_USER", request.getRemoteUser() },
{ "REQUEST_METHOD", request.getMethod() },
{ "SCRIPT_NAME", request.getServletPath() },
{ "SERVER_NAME", request.getServerName() },
{ "SERVER_PORT",
String.valueOf(request.getServerPort()) },
{ "SERVER_PROTOCOL", request.getProtocol() },
{ "SERVER_SOFTWARE",
getServletContext().getServerInfo() }
};
String title = "Servlet Example: Showing CGI Variables";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
"<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>CGI Variable Name<TH>Value");
for(int i=0; i<variables.length; i++) {
String varName = variables[i][0];
String varValue = variables[i][1];
if (varValue == null)
varValue = "<I>Not specified</I>";
out.println("<TR><TD>" + varName + "<TD>" + varValue);
}
out.println("</TABLE></BODY></HTML>");
}
/** POST and GET requests handled identically. */
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Test it. Too good to be true?
More to come soon:
6. Generating the Server Response: HTTP Status Codes
7. Generating the Server Response: HTTP Response Headers
8. Handling Cookies
9. Session Tracking. An On-Line Store Using a Shopping Cart and Session Tracking
A348/A548