title: Print out a variable in a Java servlet using output stream
body: <pre><code>public class DemoServlet extends HttpServlet {
  
      public void service(HttpServletRequest req, HttpServletResponse resp)
          throws IOException, ServletException {
  
          //prints out my string
          resp.getOutputStream().write("Hello from servlet\n".getBytes());
  
          String variable ="VAR";
          //trying to print out variable by this way but doesn't work
          resp.getOutputStream().write("%s\n".getBytes(),variable);
          //doesn't work this way either
          resp.getOutputStream().write("variable is:"+ variable +"something else\n".getBytes());
      }
  }
  </code></pre>
  
  <p>First, I was using <code>PageWriter out= resp.getWriter();</code> but then I switched to <code>ServletOutputStream</code> because I wanted to print images. Every thing else is OK but:</p>
  
  <pre><code>public void makedbconnection() {
      try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          Dbcon = DriverManager.getConnection("jdbc:mysql://localhost/test");
      } catch(Exception idc) {
         //ON THIS LINE, out is ServletOutputStream.
         idc.printStackTrace(out);
      }
      //System.out.println("connection made");
  }
  </code></pre>