title: I am looking for a way to create asynchronous & stateless ssh connection using JSch?
body: <p>Hi I am trying to create a putty like interface in my web application. 
  1.  It will have a text box to enter a command (Temporarily hard coding the server user credentials) and If the user hits return key, it will send ajax request to the server.
  2.  Server will creates jsch &amp; session and channle object and execute that user command in the remote shell.
  3.  And I will populate the response in user browser screen.
  I don’t want the point number two as above for further request. I want it as “Server will check for existing channel and using that channel it will execute”.
  To achieve this I tried storing the channel object in session. But I need to execute the .connect() method of the channel object on every request (Which return Last Login time…., It seem it is doing login process using the older credentials) ie, Only the state is store in terms of user name &amp; password and not the connect and server session.
      Could someone suggest me a way to get a solution for my problem with JSch.
  Or suggest me any other way to achieve my requirement. (Putty like interface in Browser window)</p>
  
  <p>ie, I am looking for a way to create asynchronous &amp; stateless ssh connection using  JSch ?</p>
  
  <p>This is my code </p>
  
  <pre><code>protected void doPost(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      String createSession = request.getParameter("createSession");
      String logOff = request.getParameter("logOff");
      userVoice = request.getParameter("string");
      userVoice = userVoice == null ? "" : userVoice;
      userVoice = userVoice + "\n";
      writer = response.getWriter();
      try {
          HttpSession httpSession = request.getSession();
          //channel = (Channel) httpSession.getAttribute("channel");
          if(channel!= null &amp;&amp; channel.isConnected())
          {
              /*
               * channelOutput = (InputStream) httpSession
               * .getAttribute("channelOutput"); channelInput = (OutputStream)
               * httpSession .getAttribute("channelInput");
               */
              channelOutput = channel.getInputStream();
              channelInput = channel.getOutputStream();
  
          }
  
          if (createSession != null &amp;&amp; logOff == null) {
              String username = request.getParameter("username"); // "bninet";
              String password = request.getParameter("password"); // "password";
              String host = request.getParameter("host"); // "10.77.246.120";
                                                          // // sample ip
                                                          // address
              int port = Integer.parseInt(request.getParameter("port"));
              JSch jsch = new JSch();
              Session session = jsch.getSession(username, host, port);
              session.setPassword(password);
              Properties properties = new Properties();
              properties.put("StrictHostKeyChecking", "no");
              session.setConfig(properties);
              session.setPassword(password);
              session.connect(30000);
              channel = session.openChannel("shell");
              setIOforChannel(channel, httpSession);
          //  httpSession.setAttribute("channel", channel);
          } else if (channelOutput != null &amp;&amp; channelInput != null) {
              if (logOff != null) {
                  userVoice = "exit";
              }
              channelInput.write((userVoice + "\n").getBytes());
              //channel.connect();
  
              if (logOff != null) {
                  channel.disconnect();
              //  httpSession.removeAttribute("channelOutput");
              //  httpSession.removeAttribute("channelInput");
              }
          } else {
              writer.write("No session Available.\n Please create a session using createSession tool ");
              return;
          }
          Thread.sleep(1000);
          String returnData = streamToString(channelOutput);
          int i = 0;
          while (!returnData.isEmpty() &amp;&amp; i &lt; 5) {
              writer.write(returnData);
              Thread.sleep(1000);
              returnData = streamToString(channelOutput);
              i++;
          }
  
      } catch (Exception e) {
          writer.write("Error Occured --  " + e.getMessage());
      } finally {
          writer.flush();
          writer.close();
  
      }
  
  }
  </code></pre>