Recently I've got a requirement to build address picker for web application which uses Active directory.I've spend couple of hours on net to find some sample application/code but no success. Even though I had implemented ActiveX and JNDI based program which search users in Active directory but those were for Notes client based application.
After some case study , I came to know only solution is to write Java Servlet to connect Active directory and build address picker on the web.After reading some Servlet tutorial and Notes help file got some confidence in writing Servlet in Domino application.I must say, It was quite easy and fun to write Servlet program.Here is my address picker which works perfectly in IE and Firefox allowing multiple users selection.



Let's start some postmortem on the code. First check my core logic to connect LDAP and retrieve records.Probably it's same as which I've posted in my last blog



In above code, I'm looping through each person records in Active Directory and writing in the list box using PrintWriter class.

Now lets check the code in very simple steps.

Step 1: You need following classes to be imported in your agent,


import java.io.*; // For PrintWriter class
import javax.servlet.*; // For Servlets class
import javax.servlet.http.*; // For Servlets class
import lotus.domino.*; // For Domino class
import java.util.*; // For util
import javax.naming.*; // For JNDI
import javax.naming.directory.*; // For JNDI


Step 2: Construct basic skeleton to write Java class.

public class namePicker extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{

try {
// Code goes here

}catch (NotesException n) {
System.out.println("Exception ID: " + n.id);
System.out.println("Exception description: " + n.text);
}
finally { NotesThread.stermThread(); }
}

}


Above code extends a Java interface called HttpServlet.Inside the code, there are a few predefined methods you want to override, for example, here doGet() method gets called with HttpServletRequest object and HttpServletResponse object as parameters. The HttpServletRequest is a java object that is created by the container and captures an incoming HTTP request in an object form.HttpServletResponse provide HTTP-specific functionality in sending a response. For example, it has methods to access HTTP headers and cookies.

Step 3: Using setContentType method to sets the content type which will be sent to the client.Next, Creating PrintWriter object from getWriter method of HttpServletResponse class.This class is used to write contents on the browser.


res.setContentType("text/html");
PrintWriter printOnBrowser = res.getWriter();


So our code will be ,

public class namePicker extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
res.setContentType("text/html");
PrintWriter printOnBrowser = res.getWriter();
try {
//codes goes here

}catch (NotesException n) {
System.out.println("Exception ID: " + n.id);
System.out.println("Exception description: " + n.text);
}
finally { NotesThread.stermThread(); }
}

}


Step 4: Now we need to create NotesThread and initialize Domino session. To do so,

NotesThread.sinitThread();
Session session = NotesFactory.createSession();


Using sinitThread() method of NotesThread class and NotesFactory class to creation Notes session. Check designer help to get more idea.So our code changed to,

public class namePicker extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
res.setContentType("text/html");
PrintWriter printOnBrowser = res.getWriter();
try {
NotesThread.sinitThread();
Session session = NotesFactory.createSession();

}catch (NotesException n) {
System.out.println("Exception ID: " + n.id);
System.out.println("Exception description: " + n.text);
}
finally { NotesThread.stermThread(); }
}
}


Step 4: Now you're well set to write HTML and JavaScript in Servlet which will be printed on browser like Lotus Script "Print" statement. Something like,



Step 5 At the end don't forget to destroy Domino objects which you've created because we are using Multi threaded programming , mistakes can screw your Domino server. To destroy Domino objects, User recycle()

session.recycle();


How I'm calling my Servlet from Domino

window.open( '/servlet/namePicker','newWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,
scrollbars=no,resizable=no,width=540,height=340') ;


Note that I'm referring "Servlet" folder from my server directory which is in (Lotus\Domino\data\domino\servlet)



Full Servlet code can't be posted here because lack of HTML support on this site.If anyone interested in getting full code ,drop me an e-mail.Along with that I've done similar name picker which use NAB using Servlet.Hopefully soon both sets of code will be posted in OpenNTF.

Hope this article can be useful to those who wish to integrate Domino with third party application using Java Servlet.

Oops, Forgot to mentioned some links for those who are not sure how to run Servlet in Domino, there is some gotcha in that :). You may follow Jake's article ,


Creating servlets inside the Domino Designer

Java servlets: Extending your Domino applications

Only thing is changed from Jake's article about adding ,jsdk.jar in your agent . You don't need to do any more ( at least with Designer 8.5 )

2 Responses to "Active directory name picker using Domino Java and Servlet"

  1. gravatar Tej Dixit Says:

    Great..

  2. gravatar RituRaj Says:

    Great help to us by providing these kind of stuffs...great Rishi

    Ritesh

Leave a Reply

preload preload preload