title: Retrieving values from a hash map in my JSP
body: <p>Some background information of what I'm trying to achieve is a user hits the submit button on my JSP page it needs to send the message submitted to a text file and then I need to access the file and retrieve all the messages in the file in my JSP page.  Please help me out, I have spent too long and I'm not sure what I need to do to be able to iterate over the hash map to show all the messages.</p>
  
  <p>This is what my code looks like right now: </p>
  
  <p>Controller:</p>
  
  <pre><code>public class TwitServlet extends HttpServlet {
  
  @Override
      protected void doPost(HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException {
  
      String twits = "";
      String path = getServletContext().getRealPath("/WEB-INF/twit.txt");
      HttpSession session = request.getSession();
  
      String tweet = request.getParameter("tweet");
  
      System.out.print(tweet);
      String usern = (String) session.getAttribute("user");
      String alias = (String) session.getAttribute("uname");
  
      twitDB.insert(tweet, usern, alias, path);
  
      Map test = twitDB.getTwit("/Users/emilio/Desktop/twit.txt");
  
  
  
      session.setAttribute("test",test);
  
      getServletContext()
      .getRequestDispatcher("/home.jsp").forward(request, response);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Database(using txtfile for now):</p>
  
  <pre><code>ublic class twitDB {
  
  public static void insert (String twit, String user, String uname, String path) throws IOException {
  
      Date date = new Date();
      SimpleDateFormat ft = new SimpleDateFormat ("yyyy/MM/dd");
      String dateString = ft.format(date);
      File file = new File ("/Users/emilio/Desktop/twit.txt");
      try ( PrintWriter out = new PrintWriter(new FileWriter(file,    true))){
  
          out.println("[@"+uname+"]:" + " " + dateString);
          out.println(user); 
          out.println(twit);
          out.println(".");
          out.close();
      }
      catch (IOException iox) {
          //do stuff with exception
          iox.printStackTrace();
      }
  }
  
  public static Map&lt;String,Tweet&gt; getTwit(String filename) throws IOException {
      Map&lt;String,Tweet&gt; tweets = new HashMap&lt;String,Tweet&gt;();
      File file = new File ("/Users/emilio/Desktop/twit.txt");
      Scanner in = new Scanner(file);
  
      while (in.hasNextLine()) {
          String uname = in.nextLine();
          String name = in.nextLine();
          String twit = in.nextLine();
         String filler = in.nextLine();
         Tweet tweet = new Tweet(uname, name, twit);
  
          tweets.put(uname, tweet);
          tweets.put(name, tweet);
          tweets.put(twit,tweet );
  
      }
      in.close();
      return tweets;
      }
  
  
     }
  </code></pre>
  
  <p>Part of my JSP: </p>
  
  <pre><code> &lt;button type="submit" method="post" class="btn btn-twitter"&gt;
                                                          &lt;span   class="glyphicon glyphicon-pencil"/&gt;Tweet
                                                          &lt;/button&gt;
                                                      &lt;/div&gt;
  
                                                  &lt;/form&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class = "row feed"&gt;
                                          &lt;p&gt;
  
                              &lt;c:forEach items="${test}" var="test"&gt;
  
                          &lt;/c:forEach&gt;
  
                                          &lt;/p&gt;
  </code></pre>