[jboss-user] [JBossWS] - Re: How can I use session sope in jbossws?

froden do-not-reply at jboss.com
Thu Jan 31 06:37:15 EST 2008


You mean for the webservice? This is the interface:

package services;
  | 
  | import javax.jws.WebMethod;
  | import javax.jws.WebParam;
  | import javax.jws.WebService;
  | import javax.jws.soap.SOAPBinding;
  | 
  | @WebService(
  |     name = "EndpointInterface",
  |     targetNamespace = "http://mynamespace/xml"
  | )
  | @SOAPBinding(style = SOAPBinding.Style.RPC)
  | public interface UserService {
  |   @WebMethod(operationName = "registerUser", action = "urn:registerUser")
  |   public String registerUser(
  |       @WebParam(name = "nickname") String nickname,
  |       @WebParam(name = "password") String password,
  |       @WebParam(name = "email") String email
  |   );
  | }

And the implementation:

package services;
  | 
  | import javax.annotation.Resource;
  | import javax.ejb.EJB;
  | import javax.ejb.EJBContext;
  | import javax.ejb.Stateless;
  | import javax.jws.WebService;
  | import javax.naming.InitialContext;
  | import javax.servlet.http.HttpSession;
  | import javax.xml.ws.WebServiceContext;
  | import javax.xml.ws.WebServiceException;
  | import javax.xml.ws.handler.MessageContext;
  | 
  | import org.jboss.ws.annotation.WebContext;
  | import org.jboss.ws.core.CommonMessageContext;
  | import org.jboss.ws.core.jaxws.WebServiceContextEJB;
  | import org.jboss.ws.core.jaxws.handler.SOAPMessageContextJAXWS;
  | import org.jboss.ws.core.soap.MessageContextAssociation;
  | 
  | @WebService(
  |     endpointInterface = services.UserService",
  |     serviceName = "UserService"
  | )
  | @WebContext(contextRoot="/ws", urlPattern="/UserService")
  | @Stateless
  | public class UserServiceImpl implements UserService {
  | 
  |   WebServiceContext wsContext;
  |   
  |   @Resource 
  |   EJBContext ejbCtx;
  |   public String registerUser(String nickname, String password, String email) { 
  | 
  |     CommonMessageContext msgContext = MessageContextAssociation.peekMessageContext(); 
  |     wsContext = new WebServiceContextEJB((SOAPMessageContextJAXWS)msgContext, ejbCtx);
  |     MessageContext mc = wsContext.getMessageContext();
  |     
  |     HttpSession session = ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
  |     
  |     if (session == null)
  |         throw new WebServiceException("No session in WebServiceContext");
  |     
  |     String[] names = session.getValueNames();
  |     System.out.println("There are " + names.length + " variables in the session.");
  |     for(int i = 0; i < names.length; i++) {
  |       System.out.println("We have a session variable called " + names[ i ] + " holding the value " + session.getAttribute(names[ i ]));
  |     }
  | 
  |     // Get a session property "counter" from context
  |     Integer counter = (Integer)session.getAttribute("counter");
  |     if (counter == null) {
  |         counter = new Integer(0);
  |         System.out.println("Starting the Session");
  |     }
  | 
  |     System.out.println("Session id is: " + session.getId());
  |     counter = new Integer(counter.intValue() + 1);
  |     session.setAttribute("counter", counter);
  |     
  |     System.out.println("Counter is: " + counter);
  |     return "blabla";
  |   }
  | 
  | }

Sorry about the mess hehe

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4125093#4125093

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4125093



More information about the jboss-user mailing list