Product Home Page | Developer Site | Version | |
The Java wrappers for the ASP intrinsic objects have two uses:
The wrappers are used to call methods of existing Java classes designed to be called from servlets and JSPs. Such methods often take Servlet interface arguments. These methods can now be called directly from ASP by substituting the ASP Request and Response objects for the ServletRequest and ServletResponse arguments to these methods. This mechanism is illustrated in the first method of the AspTest.asp portion of the following example.
Developers can write Java methods intended for use by ASP pages and access the ASP intrinsic objects directly from Java code. This mechanism is illustrated in the second and third methods in the AspTest.asp portion of the following example.
A class called com.sun.asp.AspContext implements javax.servlet.ServletContext on the CLASSPATH of the virtual machine running in the ASP engine. This class has a static method called getScriptingContext, which returns an instance of AspContext wrapping the ASP intrinsic objects for the current page. In addition to the ServletContext methods, this class has getResponse() and getRequest() methods returning HttpServletRequest and HttpServletResponse wrapping the current ASP Request and Response objects. (The com.sun.asp.AspContext class has no use outside of ASP, and will not work outside of ASP.)
The following example illustrates ASP servlet functionality. Running AspTest.asp with the compiled AspTest.class on the classpath will display the contents of AspTest.asp.html when the request /AspTest.asp?foo=bar
is sent to the browser.
<%
Set obj = NewJavaObject("AspTest")
'Display the query string using the first method. This Java method
'takes HttpServletRequest and HttpServletResponse arguments, so
'the ASP Response and Request objects are passed.
obj.FirstTest Request, Response
Response.write "<br><br>"
'Display the query string using the second method. This Java method
'accesses the ASP Request and Response objects directly from the
'Java code.
obj.SecondTest
Response.Write "<br><br>"
'Set a session variable from ASP and display it from Java.
Session("key") = "value"
obj.ThirdTest
%>
import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.asp.*;
import java.io.IOException;
import java.io.PrintWriter;
public class AspTest {
public AspTest(){}
//This method can be called from ASP by passing the ASP Request
//and Response objects as arguments. The Chili!Bean recognizes
//those objects and substitutes instances of its own
//implementations of HttpServletRequest and
//HttpServletResponse.
public void FirstTest(HttpServletRequest req,
HttpServletResponse res)
throws IOException {
PrintWriter writer = res.getWriter();
String queryString = req.getQueryString();
writer.println("The QueryString is:<br>");
writer.println(queryString);
}
//This method does the same thing. In this case,
//instances of HttpServletRequest and HttpServletResponse
//that wrap the ASP Request and Response objects are
//obtained in the Java code by calling the AspContext static
//method getScriptingContext. The returned AspContext
//object has methods that return the Request and
//Response wrapper.
public void SecondTest()
throws IOException {
AspContext ctxt = AspContext.getScriptingContext();
HttpServletRequest req = ctxt.getRequest();
HttpServletResponse res = ctxt.getResponse();
FirstTest(req,res);
}
//This method displays the value of a session variable.
//The Java code accesses the session data from the ASP
//page calling it.
public void ThirdTest()
throws IOException {
AspContext ctxt = AspContext.getScriptingContext();
HttpServletRequest req = ctxt.getRequest();
HttpServletResponse res = ctxt.getResponse();
HttpSession sess = req.getSession();
PrintWriter writer = res.getWriter();
writer.println("The value of the session variable key is " +
sess.getAttribute("key") + "<br>");
}
}
<html><head></head><body>The QueryString is:<br>
foo=bar
<br><br>The QueryString is:<br>
foo=bar
<br><br>The value of the session variable key if value<br></body></html>
Copyright © 2003 Sun Microsystems, Inc. All rights reserved.