Results 1 to 2 of 2
  1. #1
    TimmerCA is offline Member
    Join Date
    Jun 2012
    Posts
    10
    Rep Power
    0

    Question ServletContext from within ServerEndpoint

    Hi,

    I'm trying to understand how to get access to my ServletContext from within a ServerEndpoint. In particular, I need access to things like my database connection information. I followed a post I found through Google that suggested creating a ServerEndpointConfig.Configurator that could grab the HttpSession from the HandshakeRequest parameter, but that only works if the user already has an HttpSession. If a session has not been created for the connection, then there's no way to create one in the ServerEndpointConfig.Configurator. That's a problem as I'll have many clients connecting to the ServerEndpoint that haven't visited any other pages in the web application.

    I'm having a hard time believing that the folks who implemented the ServerEndpoint class didn't think that those web socket servers would need to access anything else about the server on which the ServerEndpoint is running, but that certainly seems to be the case.

    In short, how do people access databases or other such resources from within a ServerEndpoint?

  2. #2
    TimmerCA is offline Member
    Join Date
    Jun 2012
    Posts
    10
    Rep Power
    0

    Default Re: ServletContext from within ServerEndpoint

    I think I figured it out. This seems to work:

    Java Code:
    @WebListener
    @ServerEndpoint(value = "/socket")
    public class Socket implements ServletContextListener {
      private static ServletContext servletContext;
    
      @Override
      public void contextInitialized(ServletContextEvent servletContextEvent) {
        servletContext = servletContextEvent.getServletContext();
    
        servletContext.log("Socket Context Initialized");
      }
    
      @Override
      public void contextDestroyed(ServletContextEvent servletContextEvent) {
        servletContext.log("Socket Context Destroyed");
      }
    
      @OnOpen
      public void onOpen(Session session, EndpointConfig config) {
        servletContext.log(
          session.getId()
          + ": Connected"
        );
      }
    
      @OnMessage
      public void onMessage(Session session, String message) {
        servletContext.log(
          session.getId()
          + ": "
          + message
        );
      }
    
      @OnClose
      public void onClose(Session session, CloseReason reason) {
        servletContext.log(
          session.getId()
          + ": Disconnected: "
          + Integer.toString(reason.getCloseCode().getCode())
        );
      }
    }

Similar Threads

  1. Synchronization of ServletContext Object
    By JKoder70014 in forum Advanced Java
    Replies: 4
    Last Post: 08-04-2011, 05:56 PM
  2. i need help for ServletContext
    By samiahmed in forum Java Servlet
    Replies: 2
    Last Post: 09-24-2008, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •