Wednesday, June 19, 2019

Servlet Implementation in Domino

In this article, I am going to show you how to implement the servlet in Domino. The implementation is using DesignerFacesServlet, not the tradition HttpServlet. The DesignerFacesServlet allows us to use the XPages scope variable.

1. Import servlet library into the database. The servlet library can find in <NOTES>\framework\shared\eclipse\plugins\com.ibm.domino.xsp.adapter_<VERSION>.

2. Create Servlet factory to map the URL “https://server/database/xsp/myservlet” to “test.servlet.HelloWorldServlet”.
package test.servlet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import com.ibm.designer.runtime.domino.adapter.ComponentModule;
import com.ibm.designer.runtime.domino.adapter.IServletFactory;
import com.ibm.designer.runtime.domino.adapter.ServletMatch;
public class ServletFactory implements IServletFactory {
      private static final Map<String, String> servletClasses = new HashMap<String, String>();
      private static final Map<String, String> servletNames = new HashMap<String, String>();
      private ComponentModule module;
    public void init(ComponentModule module) {
           System.out.println("TestFactory:init");
           servletClasses.put("myservlet""test.servlet.HelloWorldServlet");
           servletNames.put("myservlet""Hello World");
           this.module = module;
    }
    public ServletMatch getServletMatch(String contextPath, String paththrows ServletException {
        try {
                  String servletPath = "";
                  // iterate the servletNames map
                  Iterator<Map.Entry<String, String>> it = servletNames.entrySet().iterator();
                  while (it.hasNext()) {
                        Map.Entry<String, String> pairs = it.next();
                        if (path.contains("/" + pairs.getKey())) {
                              String pathInfo = path;
                              return new ServletMatch(getWidgetServlet(pairs.getKey()), servletPathpathInfo);
                        }
                  }
        } catch (Throwable t) {
            t.printStackTrace();
        }       
        return null;
    }
    public Servlet getWidgetServlet(String keythrows ServletException {
 return module.createServlet(servletClasses.get(key), servletNames.get(key), null);
    }
}
3. Enable servlet factory services.
3.1 Open the database in Package Explorer.
3.2 Go to folder “Code/Java/META-INF/services/ and create a text file name “com.ibm.xsp.adapter.servletFactory”.
3.3 In the text file, fill “test.servlet.ServletFactory” to enable the servlet factory.

4. Create the HelloWorld Servlet.
package test.servlet;
import com.ibm.xsp.webapp.DesignerFacesServlet;
import java.io.*;
import javax.faces.context.FacesContext;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends DesignerFacesServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponsethrows ServletException, IOException {
        // Set up handy environment variables
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse res = (HttpServletResponse) servletResponse;
        ServletOutputStream out = res.getOutputStream();
        FacesContext facesContext = this.getFacesContext(reqres);
        try {
            res.setContentType("text/plain");
            System.out.println("User hit the service.");
        } catch(Exception e) {
            e.printStackTrace(new PrintStream(out));
        } finally {
                  facesContext.responseComplete();
                  facesContext.release();
                  out.close();
        }
    }
}
5. Test the servlet by entering URL https://server/database/xsp/myservlet.




6. The implement is easy by creating three files.


Download Sample Database

References:
https://edm00se.io/xpages-servlets/servlet-implementation/
https://www.ibm.com/developerworks/cn/lotus/xpage-servlet/index.html