Recreate LotusScript Print method in Java

Jake Howlett, 1 March 2001

Category: Java; Keywords: print output

Having all written agents for the web in LotusScript, the Print method should seem like second nature to us. What about writing web agents in Java. How do we go about writing to the browser then? Well we use the PrintWriter class. Here is an agent that prints two separate lines to the browser.
import lotus.domino.*;
import java.io.*;

public class JavaAgent extends AgentBase {

public void NotesMain() {

try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
PrintWriter out = getAgentOutput();

out.println("This is line 1<br>");
out.println("This is line 2");

} catch(Exception e) {
e.printStackTrace();
}
}
}


The obvious result being that "This is line 1" and "This is line 2" gets returned, seperated by a new line.

Fair enough you may well be thinking, we all know that. What's your point? Well, I don't know about you but having to write "out.println(..." everytime starts to annoy me. Why can't we just use "print(..."? Well, we can. Here's how:

import lotus.domino.*;
import java.io.*;

public class JavaAgent extends AgentBase {

PrintWriter out;

public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
out = getAgentOutput();
print( "This is line 1 <br>" );
print( "This is line 2" );

} catch(Exception e) {
e.printStackTrace();
}
}

void print( String s )
{
out.println( s );
}

}


The difference here is that the PrintWriter we are using (out) has been declared outside of the "Main" method and is available to the new method we have written (print). We can now pass any String to the println method of this PrintWriter (out) using our new method (print) once we have set "out" to be the AgentOutput.

Whilst all that this does is make it easier for the coder's fingers it probably makes the actual code more complicated. However, whether you use this or not, hopefully, you have learnt something about how Java classes, methods and variables work when used beyond the basic framework that the Domino Designer IDE provides.