1 Reply Latest reply on May 27, 2006 12:19 AM by Julien Martin

    Session maintenance with j2ee generated stub

    Julien Martin Expert

      Hello,

      I am trying to maintain state between a JSE endpoint and a servlet that acts as a client.

      Here is the code for the client:

      /*
       * addClient.java
       *
       * Created on May 22, 2006, 8:44 PM
       */
      
      package pack;
      
      import java.io.*;
      import java.net.*;
      
      import javax.servlet.*;
      import javax.servlet.http.*;
      import javax.xml.rpc.Stub;
      
      /**
       *
       * @author julien
       * @version
       */
      public class AddClient extends HttpServlet {
      
       /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
       * @param request servlet request
       * @param response servlet response
       */
       protected void processRequest(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
       PrintWriter out = response.getWriter();
       try { // This code block invokes the add operation on web service
       int sum = getAdderWebServiceSEIPort().add(4);
       out.println("<html>");
       out.println("<head>");
       out.println("<title>Servlet addClient</title>");
       out.println("</head>");
       out.println("<body>");
       out.println("<h1>Servlet addClient at " + sum + "</h1>");
       out.println("</body>");
       out.println("</html>");
       out.close();
      
       } catch(java.rmi.RemoteException ex) {
       // TODO handle remote exception
       } catch(Exception ex) {
       // TODO handle custom exceptions here
       }
       }
      
       // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
       /** Handles the HTTP <code>GET</code> method.
       * @param request servlet request
       * @param response servlet response
       */
       protected void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
       processRequest(request, response);
       }
      
       /** Handles the HTTP <code>POST</code> method.
       * @param request servlet request
       * @param response servlet response
       */
       protected void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
       processRequest(request, response);
       }
      
       /** Returns a short description of the servlet.
       */
       public String getServletInfo() {
       return "Short description";
       }
       // </editor-fold>
      
       private pack.AdderWebService getAdderWebService() {
       pack.AdderWebService adderWebService = null;
       try {
       javax.naming.InitialContext ic = new javax.naming.InitialContext();
       adderWebService = (pack.AdderWebService) ic.lookup("java:comp/env/service/AdderWebService");
      
       } catch(javax.naming.NamingException ex) {
       ex.printStackTrace();
       }
       return adderWebService;
       }
      
       private pack.AdderWebServiceSEI getAdderWebServiceSEIPort() {
       pack.AdderWebServiceSEI adderWebServiceSEIPort = null;
       try {
       Stub stub;
       adderWebServiceSEIPort = getAdderWebService().getAdderWebServiceSEIPort();
       stub = (Stub)adderWebServiceSEIPort;
       stub._setProperty(javax.xml.rpc.Stub.SESSION_MAINTAIN_PROPERTY, true);
       adderWebServiceSEIPort= (pack.AdderWebServiceSEI)stub;
       } catch(javax.xml.rpc.ServiceException ex) {
       ex.printStackTrace();
       }
       return adderWebServiceSEIPort;
       }
      }
      
      


      and here is the code for the JSE endpoint:

      package pack;
      
      import javax.servlet.http.HttpSession;
      import javax.servlet.http.HttpSessionEvent;
      import javax.servlet.http.HttpSessionListener;
      import javax.xml.rpc.ServiceException;
      import javax.xml.rpc.handler.soap.SOAPMessageContext;
      import javax.xml.rpc.server.ServiceLifecycle;
      import javax.xml.rpc.server.ServletEndpointContext;
      
      
      /**
       * This is the implementation bean class for the AdderWebService web service.
       * Created May 22, 2006 8:30:25 PM
       * @author julien
       */
      public class AdderWebServiceImpl implements AdderWebServiceSEI, ServiceLifecycle{
      
       ServletEndpointContext endptCtx;
       SOAPMessageContext msgCtx;
      
       /**
       * Web service operation
       */
       public int add(int number) throws java.rmi.RemoteException {
       System.out.println("add called");
      
       HttpSession session = endptCtx.getHttpSession();
       System.out.println("SESSION ID "+session.getId());
      
       if(session !=null){
      
       System.out.println("session not null");
       String idFromClient = msgCtx.getProperty("id").toString(); System.out.println("----"+idFromClient);
       String id = (String)session.getAttribute("id"); System.out.println("----"+id);
      
       if (id!= null && idFromClient.equals(id)){
       System.out.println("nested non null");
       int total = (Integer)session.getAttribute("total") + number;
       session.setAttribute("total",total);
       return total;
       } else{
       System.out.println("nested null");
       session.setAttribute("id", idFromClient);
       int total = number;
       session.setAttribute("total",total);
       System.out.println("here"+session.getAttribute("id"));
       return total;
       }
      
       } else{
       System.out.println("session null");
       return 0;
       }
       }
      
       public void init(Object context) throws ServiceException {
       System.out.println("init called");
       endptCtx = (ServletEndpointContext) context;
       msgCtx = (SOAPMessageContext)endptCtx.getMessageContext();
       }
      
       public void destroy() {
       System.out.println("destroy called");
       }
      
      
      
      }
      


      Here is the output from the console:

      1:53:47,692 INFO [AxisService] Web Service deployed: http://ordinateur:8080/WebApplicationFiveEndpoint/AdderWebService
      01:53:48,720 INFO [STDOUT] session Created
      01:53:57,708 INFO [STDOUT] init called
      01:53:57,769 INFO [STDOUT] org.jboss.axis.message.SOAPHeaderElementAxisImpl@1aa34dc[[mi:message-id: null]]
      01:53:57,769 INFO [STDOUT] 123456789
      01:53:57,774 INFO [STDOUT] add called
      01:53:57,774 INFO [STDOUT] session Created
      01:53:57,775 INFO [STDOUT] SESSION ID B15EEC235A416BE62D36192EAEB8AA88
      01:53:57,775 INFO [STDOUT] session not null
      01:53:57,776 INFO [STDOUT] ----123456789
      01:53:57,776 INFO [STDOUT] ----null
      01:53:57,776 INFO [STDOUT] nested null
      01:53:57,778 INFO [STDOUT] here123456789
      01:53:57,792 INFO [STDOUT] destroy called
      01:54:04,027 INFO [STDOUT] init called
      01:54:04,042 INFO [STDOUT] org.jboss.axis.message.SOAPHeaderElementAxisImpl@19ab9c5[[mi:message-id: null]]
      01:54:04,042 INFO [STDOUT] 123456789
      01:54:04,043 INFO [STDOUT] add called
      01:54:04,044 INFO [STDOUT] session Created
      01:54:04,044 INFO [STDOUT] SESSION ID 1C0770F2DF80971FC59F54DE3CCBBCE0
      01:54:04,044 INFO [STDOUT] session not null
      01:54:04,044 INFO [STDOUT] ----123456789
      01:54:04,045 INFO [STDOUT] ----null
      01:54:04,045 INFO [STDOUT] nested null
      01:54:04,045 INFO [STDOUT] here123456789
      01:54:04,082 INFO [STDOUT] destroy called
      


      As you can see, I have enabled session maintenance and still it does not work. FYI, I use Jboss 4.0.3. Can anyone help please??

      Thanks in advance,

      Julien.