Wednesday, December 07, 2005

Getting the "HttpServletRequest" in a J2EE JAX-RPC service

Below is an example J2EE JAX-RPC web service that shows how to get the "HttpServletRequest" which can return the contextpath and access any servlet request data. NOTE: This was wrote in the editor of my blog, so it could contain syntax issues. It has not been tested. :)

/////////////////////////////////////////////////////////////////////////////////////////

import javax.xml.rpc.server.ServiceLifecycle;
import javax.xml.rpc.server.ServletEndpointContext;
import javax.xml.rpc.handler.MessageContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

Class TestService implements ServiceLifecycle {

private String ctxPath;
private ServletEndpointContext epc;
private MessageContext mc;
private ServletContext sc;
private HttpServletRequest request;

public void init(Object obj) throws ServiceException {
epc = (ServletEndpointContext)obj;
if (epc != null) {
mc = (MessageContext)epc.getMessageContext();
sc = (ServletContext)epc.getServletContext();
if (mc != null) {
request = (HttpServletRequest) mc.getProperty("transport.http.servletRequest");
if (request != null) {
ctxPath = request.getContextPath();
}
}
}
}

public String doAction () {
return (new String("The contextPath for this service is: " + this.ctxPath));
}

}

0 Comments:

Post a Comment

<< Home