title: How to create session only once, when the server starts?
body: <p>I'm creating a chatbot for a java application with Watson Assistant, the servlet code:</p>
  
  <pre><code>public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  
      String sessionIdOut = "";               
      String question = req.getParameter("message");       
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
      // Set up Assistant service.                
      IamOptions iamOptions = new IamOptions.Builder().apiKey("&lt;apikey&gt;").build();
      Assistant service = new Assistant("2018-09-20", iamOptions);
      service.setEndPoint("https://gateway-lon.watsonplatform.net/assistant/api/");
      assistantId = "&lt;assistantid&gt;"; 
  
      // Create session.             
      CreateSessionOptions createSessionOptions = new CreateSessionOptions.Builder(assistantId).build();
      SessionResponse session = service.createSession(createSessionOptions).execute();
       sessionId = session.getSessionId();            
  
      // Suppress log messages in stdout.
      LogManager.getLogManager().reset(); 
  
      // Initialize with an empty value to start the conversation.
      String inputText = question;
  
  
      // Send message to assistant.
      MessageInput input = new MessageInput.Builder().text(inputText).build();
      MessageOptions messageOptions = new MessageOptions.Builder(assistantId, sessionId)
                                                            .input(input)
                                                            .build();
      MessageResponse response = service.message(messageOptions).execute();
  
  
      // Print the output from the dialog if any. Assumes a single text response.
      List&lt;DialogRuntimeResponseGeneric&gt; responseGeneric = response.getOutput().getGeneric();
      if(responseGeneric.size() &gt; 0) {
          System.out.println(response.getOutput()/*.getGeneric().get(0).getText()*/);
          String answer =  response.getOutput().getGeneric().get(0).getText();
          // set up the response
          res.setContentType("text/html");
          res.setHeader("Cache-Control", "no-cache");
          // write out the response string
          res.getWriter( ).write(answer);                
      }
  
      // Prompt for next round of input.
      System.out.print("&gt;&gt; ");                
  }
  </code></pre>
  
  <p>Currently, the servlet always creates a new session and sets up the assistant when the GET request arrives from the user interface. I want it to create a new session and set up assistant service only once when the server starts. </p>
  
  <p>Tryed to solve the problem by adding init() function, and writing the session creation and assistant setup code inside that init() function like this: </p>
  
  <pre><code>@Override
  public void init() throws ServletException {
  
      // Set up Assistant service.                
      IamOptions iamOptions = new IamOptions.Builder().apiKey("&lt;apikey&gt;").build();
      Assistant service = new Assistant("2018-09-20", iamOptions);
      service.setEndPoint("https://gateway-lon.watsonplatform.net/assistant/api/");
      assistantId = "&lt;assistantid&gt;";  
  
      // Create session.             
      CreateSessionOptions createSessionOptions = new CreateSessionOptions.Builder(assistantId).build();
      SessionResponse session = service.createSession(createSessionOptions).execute();        
       sessionId = session.getSessionId();
      super.init();       
  }
  </code></pre>
  
  <p>But it doesn't work, when I write a question in user interface, it sends me back 500 status code.</p>