Date tip published: | 11/05/2001 |
Description: | If you are a LotusScript Web programmer, you use the LotusScript "Print" statement to direct output to the Web Browser. But how do you do that same thing with a Java agent? This tip will show you how to output text to a Web Browser from a Java agent. |
To learn more about Java programming use the following links:
Beginner Java Programming for R5 Notes and Domino
Intermediate Java Programming for R5 Notes and Domino
Java Programming Special
When you create a Java agent in Domino, Domino Designer creates the majority of the base code you need. A newly created Java agent without any programmer entered code looks like the following:
import lotus.domino.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
} catch(Exception e) {
e.printStackTrace();
}
}
}
Notice comment "// (Your code goes here)". This is where you start writing code for the agent. Like LotusScript, Java is a hierarchical based language. This means certain classes can inherit functionality from other classes. The second line where the class "JavaAgent" is created is:
public class JavaAgent extends AgentBase
This allows the Java agent to inherit all of the functionality of the AgentBase class. One of the methods of AgentBase class is a Java PrintWriter called "getAgentOutput()". The PrintWriter class provides tools to output data to the console or a log. The getAgentOutput() method returns an instance of PrintWriter, so all of the methods of the PrintWriter class are available to use. One of these methods is the "println()" method. When the getAgentOutput() method is used in a Domino web agent the output goes to the Web Browser to create a web page or XML output. The complete syntax would look like the following,
getAgentOutput().println("Output String");
To write out the message "Hello World" to a Web Browser client, add the following code right after the "Your code goes here" comment.
getAgentOutput().println("Content-type: text/html");
getAgentOutput().println("<html><head><title>Java tip</title></head>");
getAgentOutput().println("<body><h1>Hello World</h1></body></html>");
This will produce a HTML document that displays "Hello World".
It is rather cumbersome to include the entire getAgentOutput().println() syntax in every line of output. To make this easier you can create a PrintWriter with a short variable name to use when outputting to the browser. This will require two changes to the code.
a. Import the java.io package to include the PrintWriter class. At the top of the code, under the import lotus.domino.*; include the following statement:
import java.io.*
b. Add the following line to create a PrintWriter instance and assign it to the "pw" object reference variable:
PrintWriter pw = this.getAgentOutput();
To write output to the browser use the println() method like before. The simplified code would be as follows:
import lotus.domino.*;
import java.io.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
PrintWriter pw = this.getAgentOutput();
pw.println("Content-type: text/html");
pw.println("<html><head><title>Java tip</title></head>");
pw.println("<body><h1>Hello World</h1></body></html>");
} catch(Exception e) {
e.printStackTrace();
}
}
} |