title: How to get attribute in JSP from servlet?
body: <p>I want to get a request attribute in my JSP page after completing an action. In my doPost the set up is like this:</p>
  
  <pre><code>String operation = request.getParameter("operation");
  
  if(operation.equals("someAction")) {
    //Do something
  } else if (operation.equals("otherAction")) {
    if (someParameter != null){
     //Doing a for loop
     //When loop is finished (so outside for loop)
        request.setAttribute("msg", "Attribute message");
        request.getSession().setAttribute("msg2","Session message");       
        request.getRequestDispatcher("/Absolute/path/Final.jsp").forward(request, response);
    }
  }
  </code></pre>
  
  <p>This code gives me no message at all after completing the method, BUT, if I refresh the page suddenly the session message appears. I want the message to appear immediately, so <code>req.setAttribute</code> seems like what I want. I added the absolute path because with a relative one the program entered an infinite loop.</p>
  
  <p>Relevant parts of my JSP:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  
  &lt;p&gt;
      Message from request: &lt;b&gt;&lt;%= request.getAttribute("msg") %&gt;&lt;/b&gt;
      &lt;br /&gt;
      Message from session: &lt;b&gt;&lt;%= session.getAttribute("msg2") %&gt;&lt;/b&gt; //&lt;---This is the only one that works (after refresh)
      &lt;br /&gt;
      Message from EL: &lt;b&gt;${msg2}&lt;/b&gt;
      &lt;br /&gt;     
      Message from EL: &lt;b&gt;${msg3}&lt;/b&gt;
  
  &lt;/p&gt;
  </code></pre>
  
  <p>This is my jQuery code:</p>
  
  <pre><code>$.ajax({
     type: "POST",
     url: "http://localhost:8080/MaJSPProject/MainServlet/*",
     dataType: 'JSON',
     data: {pTableData:JSON.stringify(TableData), operation:"submitLFoodTable", dropdownField: document.getElementById('dropdownField').value},
     success: function(msg){
         // return value stored in msg variable
         console.log(msg);
     }
  </code></pre>
  
  <p>});</p>
  
  <p>What am I missing?</p>