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 path) throws 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()), servletPath, pathInfo);
}
}
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public Servlet getWidgetServlet(String key) throws 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.
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 servletResponse) throws ServletException, IOException {
// Set up handy environment variables
HttpServletRequest req = (HttpServletRequest) servletRequest;
HttpServletResponse res = (HttpServletResponse) servletResponse;
ServletOutputStream out = res.getOutputStream();
FacesContext facesContext = this.getFacesContext(req, res);
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();
}
}
}
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
No comments:
Post a Comment