Version 4

    Searchable Wiki Title: How can I access the HttpServletRequest and HttpServletReponse from within my endpoint?


    By implementing the 'javax.xml.rpc.server.ServiceLifecycle' interface the endpoint will have a 'javax.xml.rpc.server.ServletEndpointContext' object passed into the init method before the business method of the endpoint is invoked.


    The 'javax.xml.rpc.server.ServletEndpointContext' can be used to get the 'javax.xml.rpc.handler.MessageContext' which has a getProperty method that can be used to get certain properties.


    The following properties are available by default with JBossWS: -


    • for javax.servlet.ServletContext

    • for javax.servlet.http.HttpServletRequest

    • for javax.servlet.http.HttpServletResponse

    • for javax.servlet.http.HttpSession


    See the following endpoint example: -


    import java.rmi.RemoteException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.xml.rpc.ServiceException;
    import javax.xml.rpc.handler.MessageContext;
    import javax.xml.rpc.server.ServiceLifecycle;
    import javax.xml.rpc.server.ServletEndpointContext;
    public class PhoneBookImpl implements PhoneBook, ServiceLifecycle
       private ServletEndpointContext ctx;
       public void init(final Object ctx) throws ServiceException
          if (ctx instanceof ServletEndpointContext)
             this.ctx = (ServletEndpointContext) ctx;
             throw new ServiceException("Object passed into init is not a 'ServletEndpointContext'");
       public void destroy()
       public TelephoneNumber lookup(Person person) throws RemoteException
          MessageContext msgCtx = ctx.getMessageContext();
          HttpServletRequest request = (HttpServletRequest) msgCtx.getProperty("");
          HttpServletResponse response = (HttpServletResponse) msgCtx.getProperty("");
          System.out.println("Called from - " + request.getRemoteAddr());
          TelephoneNumber tn = new TelephoneNumber();
          return tn;