WAIWebApplicationService
base class.
Run
method for processing the incoming HTTP request. (For details, see "Defining a Method to Process Requests".)
getServiceInfo
method for returning information about the service and its version.
Before continuing on, note the following points:
netscape.WAI.*
, org.omg.CORBA.*
, and org.omg.CosNaming.*
: import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import netscape.WAI.*;
nisb.zip
and WAI.zip
in your CLASSPATH
environment variable. These files are located in the server_root
/wai/java
directory in UNIX and in the server_root
\wai\java
directory on Windows NT. For example, in C shell on UNIX, enter the following command (if your
server is installed under
/usr/netscape/suitespot
):
setenv CLASSPATH "$CLASSPATH":/usr/netscape/suitespot/wai/java/nisb.zip:/usr/netscape/suitespot/wai/java/WAI.zip
On Windows NT, open the System Control Panel, and add these zip files to
your CLASSPATH environment variable listed there.
server_root
/wai/examples/WASP
directory on UNIX and the server_root
\wai\examples\WASP
directory on Windows NT.You can follow this example as a guideline for writing and compiling your
application.
WAIWebApplicationService
base class. (This class represents a web application service.)
For example, the WASP example provided with the web server declares a MyWebApplicationService
class, which is derived from the WAIWebApplicationService
base class:
import java.applet.*;
import java.io.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.lang.*;
/* Make sure to import these classes. */
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import netscape.WAI.*;
...
/*
* Implementation class for A WAS.
* Extends wrapper class for WAI CORBA object
*/
class MyWebApplicationService extends WAIWebApplicationService {
String instanceName;
MyWebApplicationService(java.lang.String name) throws
org.omg.CosNaming.NamingContextPackage.CannotProceed,
org.omg.CosNaming.NamingContextPackage.InvalidName,
org.omg.CosNaming.NamingContextPackage.AlreadyBound,
org.omg.CORBA.SystemException{
super(name);
instanceName = name;
}
...
The class that you define represents your web service. You need to define the following methods for your class; these methods are virtual methods in the WAIWebApplicationService
base class:
Run
This method is called by the web server to process HTTP requests for this
service. For details on defining this method, see "Defining a Method to
Process Requests".
getServiceInfo
This method returns information about your web service (such as version
information). For details on defining this method, see "Providing
Information About the Service".
public int Run(netscape.WAI.HttpServerRequest request);
request
represents the HTTP request to be processed. You can call the methods of this object to get data from the request, set data in the response headers, and send the response back to the client.
The rest of this section explains how you can use these methods and objects to process the request. WAI functions enable you to do the following tasks:
netscape.WAI.HttpServerRequest
class (see the section "netscape::WAI::HttpServerRequest" for details), you can get data from the client's HTTP request. You can call functions accomplish the following tasks:
netscape.WAI.HttpServerRequest
class, you can get headers from the corresponding HTTP request by calling the getRequestHeader
method. For example, the following section of code gets the user-agent
HTTP request header from the incoming request:
public int Run(netscape.WAI.HttpServerRequest request) {
...
/* Prepare an output stream to send data back to the client. */
ByteArrayOutputStream streamBuf = new ByteArrayOutputStream();
PrintStream content = new PrintStream(streamBuf);
...
/* Get the value of the user-agent header. */
org.omg.CORBA.StringHolder value = new org.omg.CORBA.StringHolder();
if (request.getRequestHeader("user-agent", value) == HttpServerReturnType.Success){
content.print("User agent: " + value.value);
}
...
}
In addition to HTTP headers, you can get other types of information (such as CGI 1.1 environment variables) from the HTTP request by calling the getRequestInfo
method of the netscape.WAI.HttpServerRequest
class.
The section "getRequestInfo" lists the types of information you can retrieve from the request. Note that the CGI 1.1 environment variables that describe the server are accessible through the getInfo
method. See "Getting Information about the Server" for details.
The following section of code gets and prints the value of the REMOTE_ADDR
CGI 1.1 environment variable from the incoming request:
public int Run(netscape.WAI.HttpServerRequest request) {
...
/* Prepare an output stream to send data back to the client. */
ByteArrayOutputStream streamBuf = new ByteArrayOutputStream();
PrintStream content = new PrintStream(streamBuf);
...
/* Get the client's IP address. */
org.omg.CORBA.StringHolder value = new org.omg.CORBA.StringHolder();
if (request.getRequestInfo("REMOTE_ADDR", value) == HttpServerReturnType.Success){
content.print("Client addr: " + value.value);
}
...
}
Getting Information about the Server
WAI also provides methods for getting information about the server, such as the server identifier or CGI 1.1 environment variables that describe the server (for example, SERVER_NAME
or SERVER_PORT
).
These methods are available as part of the netscape.WAI.HttpServerContext
class (for more information, see the section "netscape::WAI::HttpServerContext"). You can get an object of this class by using the getContext
method of the netscape.WAI.HttpServerRequest
class.
For example, the following section of code gets an netscape.WAI.HttpServerContext
object:
public int Run(netscape.WAI.HttpServerRequest request) {
...
/* Get the HttpServerContext object describing this web server. */
HttpServerContext context = request.getContext();
...
}
To get information about the server, you can call the getInfo
method of the netscape.WAI.HttpServerContext
object and specify the type of information that you want to retrieve. For example, the following section of code gets the value of the SERVER_PORT
CGI 1.1 environment variable:
public int Run(netscape.WAI.HttpServerRequest request) {
...
/* Prepare an output stream to send data back to the client. */
ByteArrayOutputStream streamBuf = new ByteArrayOutputStream();
PrintStream content = new PrintStream(streamBuf);
/* Get the HttpServerContext object for this web server. */
HttpServerContext context = request.getContext();
...
/* Get the port number that the web server listens to. */
org.omg.CORBA.StringHolder svar;
if (context.getInfo("SERVER_PORT", svar) == HttpReturnType.Success){
content.print("Web Server port number: " + svar);
}
...
}
For a list of the types of information you can retrieve from this method, see the section "getInfo".
You can also use methods that specifically retrieve a certain type of information. For example, to get the port number that the server listens to, you can call the getPort
method:
public int Run(netscape.WAI.HttpServerRequest request) {
...
/* Prepare an output stream to send data back to the client. */
ByteArrayOutputStream streamBuf = new ByteArrayOutputStream();
PrintStream content = new PrintStream(streamBuf);
/* Get the HttpServerContext object for this web server. */
HttpServerContext context = request.getContext();
...
/* Get the port number that the web server listens to. */
int portNum = 0;
if ((portNum = context.getPort()) != 0){
content.print("Web Server port number: " + portNum);
}
...
}
For details on getting server information, see the section "netscape::WAI::HttpServerContext".
Getting and Setting Cookies in the Client
Before a client accesses a URL, the client checks the domain name in the URL against the cookies that it has. If any cookies are from the same domain as the URL, the client includes a header in the HTTP request that contains the name/value pairs from the matching cookies.
The Cookie
header has the following format:
Cookie: name=value; [name1=value1; name2=value2 ... ]
To get these name/value pairs from the HTTP request, call the getCookie
method. To set your own name/value pairs in a client, call the setCookie
method.
The following example illustrates how you can use these methods to get and set cookies in the client.
public int Run(netscape.WAI.HttpServerRequest request)
{
...
org.omg.CORBA.StringHolder
cookiebuff = new org.omg.CORBA.StringHolder(); /* If no cookie has been set in the client, set a cookie. */
if (request.getCookie(cookiebuff)== HttpServerReturnType.Failure)
request.setCookie("MY_NAME", "My Value", "", "", "/", false);
...
}
Sending the Response Back to the Client
Methods of the HttpServerRequest
class also allow you to control the response sent back to the client. You can call these functions to accomplish the following tasks:
addResponseHeader
method to set any header in the response. For example, the following section of code adds the Pragma
header to the response:
public int Run(netscape.WAI.HttpServerRequest request)
{
...
request.addResponseHeader("Pragma", "no-cache");
...
}
You can also call functions that set specific types of headers. For example, you can call:
setResponseContentType
to specify the content type of the response (the Content-type
header)
setResponseContentLength
to specify the length of the response in bytes (the Content-length
header)
setResponseStatus
method. For example, the following section of code sets the response code to a 404 status code ("File Not Found"):
public int Run(netscape.WAI.HttpServerRequest request)
{
...
request.setResponseStatus(404, "");
...
}
Sending the Response
After you have specified the length of the content you want sent back to the client, you can start sending the response to the client. Call the StartResponse
method to start sending the response.
To send the rest of the data to the client, call the WriteClient
method.
The following example sends the string Hello World
back to the client:
public int Run(netscape.WAI.HttpServerRequest request)
{
...
/* Prepare an output stream to send data back to the client. */
ByteArrayOutputStream streamBuf = new ByteArrayOutputStream();
PrintStream content = new PrintStream(streamBuf);
...
/* Send "Hello World" to the print stream. */
String buffer = "Hello World\n";
content.print(buffer);
/* Convert the string to a byte array for WriteClient(). */
HttpServerReturnType rc;
byte[] outbuff = streamBuf.toByteArray();
try {
/* Specify the length of the data you will send. */
rc = request.setResponseContentLength(outbuff.length);
/* Start sending your response. */
request.StartResponse();
}
catch(org.omg.CORBA.SystemException e){
}
/* Write data back to the client. */
int write_cnt = request.WriteClient(outbuff);
...
}
Redirecting Users to Another Page
In your WAI application, you can also redirect users to a different page than the requested page. You can either automatically redirect the user to a new page, or you can present the user with a link to click on manually.
To automatically redirect the user to a different page, do the following:
addResponseHeader
setResponseStatus
StartResponse
public int Run(HttpServerRequest request){
try {
request.addResponseHeader("Location", "http://www.newsite.com/");
request.setResponseStatus(301, "Moved permanently to newsite.com!");
request.StartResponse();
}
catch(org.omg.CORBA.SystemException e){
}
catch(java.lang.Exception e) {
System.err.println(e);
}
return 0;
}To give the user the choice of going to the new location (rather than automatically redirecting the URL), you can call the
RespondRedirect
method:
public int Run(HttpServerRequest request){
request.RespondRedirect("http://www.newsite.com/");
try {
request.StartResponse();
}
catch(org.omg.CORBA.SystemException e){
}
catch(java.lang.Exception e) {
System.err.println(e);
}
return 0;
}
Calling this method will send the following page back to the client:
Moved Temporarily
This document has moved to a new location. Please update your documents and hotlists accordingly.
The word "location" on this page is a link pointing to the new location of the page.
Providing Information About the Service
Part of the WAIWebApplicationService
base class is the virtual getServiceInfo
method. When you write your web application class (which is derived from the base class), you need to include a definition of this method.
The getServiceInfo
method should provide information about the web service, such as the name of the author, the version of the service, and so on.
The following sections of code defines the getServiceInfo
method for a web service class WebApplicationServicePrototype
.
...
public java.lang.String getServiceInfo(){
return "Java Test Web Application Service V1.0\nCopyright Netscape Communications Corporation\nAuthor: Mozilla\n";
}
...
Registering Your Web Application Service
org.omg.CORBA.ORB.init()
method to initialize the ORB. This method returns an ORB
object. ORB
object's BOA_init()
method to initialize the BOA. This method returns a BOA
object.For example:
/* Initialize the object request broker (ORB). */
ORB orb = org.omg.CORBA.ORB.init();
/* Initialize the basic object adapter (BOA). */
BOA boa = orb.BOA_init();
For more information on these objects and methods, see the Netscape Internet Service Broker for Java Reference Guide.
You need to register your web service to the web server under this instance
name. The instance name that you select for your web service can be an
arbitrary name; it does not need to be the same name as your application.
(For example, if your application is named
MyApp.class
, your instance
name can be MyWebService
. They do not need to have the same name.)
Note, however, that your instance name must be unique. No other
registered WAI application can have the same name.
RegisterService
Pass the name of the web server's hostname and port number as an
argument (in the form
hostname
:
portnumber
) to this method.
Note that if your web server is running with SSL enabled, you need to
specify a different value for this argument. For details, see "Registering With
an SSL Enabled Server".
mpi
from the web service class MyWebApplicationService
. The example registers this object to the web server under the instance name MyJavaService
.
...
String host = "myhost.mydomain.com:81";
String instanceName = "MyJavaService";
try {
/* Initialize the object request broker (ORB). */
ORB orb = org.omg.CORBA.ORB.init();
/* Initialize the basic object adapter (BOA). */
BOA boa = orb.BOA_init();
/* Create the web service. */
try {
MyWebApplicationService
mpi = new MyWebApplicationService(instanceName);
System.out.println(mpi + " is ready.");
/* Register the web service. */
mpi.RegisterService(host);
/* Wait for incoming requests */
boa.impl_is_ready();
}
catch(java.lang.Exception e){
System.out.println("WAS failed to initialize.");
System.err.println(e);
}
...
RegisterService
. (In the case of an SSL-enabled server, the method gets the object reference from the Interoperable Object Reference (IOR) file.)
file:
This file is located in the path_to_IOR_file
wai/NameService
directory under your server root directory. The file uses the following naming convention:
For example, on the machine named server_id
.IOR preston
, the IOR might be named https-preston.IOR
.
Suppose your web server is running on the host machine named feathers
on port number 8080. Suppose that the server is installed under the server root directory /usr/netscape/suitespot
with the server identifier https-feathers
. If SSL is enabled, you register your WAI application in Java by calling:
RegisterService("file:/usr/netscape/suitespot/wai/NameService/https-feathers.IOR" );
The RegisterService
method uses the Interoperable Object Reference (IOR) file to get the object reference for the naming service. This object reference is used to register your application.
Running Your Web Service
After you write and compile your application, you can run your application to make your web service available. The web server should recognize your application, if you've registered it (see "Registering Your Web Application Service").
End users can access your service by going to the URL:
http://server_name:port_number/iiop/instance_name
For example, you can access the JavaWASP
example by going to the URL:
http://server_name:port_number/iiop/JavaWASP
Last Updated: 12/04/97 16:12:39
Any sample code included above is provided for your use on an "AS IS" basis, under the Netscape License Agreement - Terms of Use