Creating servlets inside the Domino Designer

Jake Howlett, 21 March 2001

Category: Java; Keywords: servlet class designer

According to an article on Notes.net's Iris Today webzine about developing servlets for Domino:

The standard Domino Designer development environment does not support the development of servlets, so you must use a third-party IDE or the SUN command-line JDK to develop servlet code


It may not be supported but that is not to say you can't do it. Imagine you don't have the luxury of a 3rd party IDE on your machine. This article helps by explaining how you can develop servlets from within the Domino Designer IDE.

First of all, the classes that we are going to use are contained in a file called jsdk.jar, which should be in the root of your Lotus Domino directory. To use these classes you could simply include it in your agent, but the best thing to do is to place this in a Script Library before we start creating the servlet.

Creating a ServletSupport Script LibraryThe jsdk.jar file should now be included in the Script Library list to the right, like below:

Image

Finish off by pressing "OK" in dialog box then closing and saving the Script Library, calling it something like "ServletSupport".

Creating an agent that uses this Script LibraryCreating the code for the servlet

Now comes the interesting part. Remove ALL the code that Domino has automatically generated from the agent. Replace it with this code that will create a fairly simple servlet:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;

/**
* Simple servlet that only outputs a simple string when called.
*/
public class MyServlet extends HttpServlet {

/**
* Called when the HTTP task is starting.
* Initialize database (JDBC) connections etc. here.
*
* @param config the ServletConfig object passed by the servlet container.
*/
public void init(ServletConfig config) {
System.out.println(this.getClass().getName() + " initialized");
}

/**
* Called when a user makes a HTTP GET, with a URL.
*
* @param request the request of the client.
* @param response the response that should be sent to the client.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
PrintWriter out = response.getWriter();
out.println("doGet in " + this.getClass().getName() + "entered");
} catch(IOException e) {
//Something went wrong.
e.printStackTrace();
}
}

/**
* Called when a user makes a HTTP POST from a HTML form.
*
* @param request the request of the client.
* @param response the response that should be sent to the client.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
PrintWriter out = response.getWriter();
out.println("doPost in " + this.getClass().getName() + "
entered");
} catch(IOException e) {
//Something went wrong.
e.printStackTrace();
}
}

/**
* Called when the HTTP task is shut down.
* Always close open database (JDBC) connections etc. here.
*/
public void destroy() {
System.out.println(this.getClass().getName() + " destroyed");
}
}


Give the agent a name such as "MyServlet" and compile it (don't close the agent yet!). It should look something like:

Image

Now here is the trick. When you compiled the code Domino Designer created a .java and a .class file in the TEMP directory of your operating system (usually "C:\Temp").

Inside the TEMP directory, the Designer creates folders named "jarXXXXX.dir", where XXXXX is a number. In one of these directories, a class-file with the name "MyServlet.class" should exist, as shown below:

Image

Make a copy of this file and paste it in to the "servlet" directory of the Domino server on which you want it to run. Restart the server's HTTP task and you can then start to use your servlet.

At this point you could of course throw this agent away. We only used it to create our .class file. It may be worth keeping it though in case you need to make any changes to the servlet in the future.

This article assumes that you have already set up the server to run Java servlets. If you haven't then this article on Notes.net describes how. There is also plenty of help in Domino's help files.

So there we have it. You can develop servlets in the Desiger after all. If, however, you see no point and would rather write the servlets in a 3rd party IDE then here is an article from Notes.net describing how.