logo

Recreate LotusScript Print method in Java

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.


Feedback

  1. Java Print Method

    Excellent. And thanks! I have just begun the processes of attempting to recreate code in Java whenever I need to do any major rework to an agent. This will help as I have numerous agents that produce output on the web.

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:



Navigate other articles in the category "Java"

« Previous Article Next Article »
Talking to applets with JavaScript   Getting Java Applets to talk to JavaScript

About This Article

Author: Jake Howlett
Category: Java
Keywords: print; output;

Options

Feedback
Print Friendly

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »