Sun
ONE Active Server Pages Product Home Page Developer Site Version
 

ContentsPreviousNextIndex



Programmatic Access


The Java wrappers for the ASP intrinsic objects have two uses:

Example

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.

AspTest.asp
<%
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
 
%>
AspTest.class
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>");
    }
 
}
AspTest.asp.html
<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>


ContentsPreviousNextIndex