title: write textarea content from jsp page to text file
body: <p>I have jsp page that has a textarea and submit button. </p>
  
  <p>form.jsp</p>
  
  <pre><code>&lt;form method="post" action="submitForm"&gt;
              &lt;label for="commentTxt"&gt;Comments: &lt;/label&gt;&lt;br /&gt;
              &lt;textarea rows="20" cols="100" id="commentTxt" name="commentTxt" 
                  placeholder="Enter your comments."&gt;&lt;/textarea&gt;&lt;br /&gt;
  
              &lt;input type="submit" name="submit" value="Submit" /&gt;
  
          &lt;/form&gt;
  </code></pre>
  
  <p>I want to write the comments to text file, so in the servlet I have this code that call a java method to write to a txt file.</p>
  
  <p>submitForm.java</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      //doGet(request, response);
      // get user comments
      String comments = request.getParameter("commentTxt");
      FilesRW.writeStrTxt(comments);
  
  }
  </code></pre>
  
  <p>FilesRW.java</p>
  
  <pre><code>    public static void writeStrTxt( String str){
       FileWriter fileWriter = null;
      try{
          FileWriter wr = new FileWriter( new File("userComments.txt"));
          System.out.println(str);// this line print user comments in console
          wr.write(str);
          wr.close();
      }catch (IOException exe) {
           exe.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>I don't know what is the problem because user comments is sent to the fileRW class and i can print it to the console and see the user comments. However, when i open the file, is empty.  Also, when I try to use the writeStrTxt(String) method in the main of the same class (FilesRW.java) it works and it writes a text to the file. </p>
  
  <p>I need to call this method in servlet class (submitForm.java), but it does not write the text to the file. What is wrong in my code?</p>