title: Access GWT POST parameters via servlet?
body: <p>I'm creating a GWT application that will be accessed by a POST request, which contains parameters I care about (user id, etc.).</p>
  
  <p>All the reading I've done so far has led me to believe that I should create a servlet (I'm using tomcat) that will handle the POST parameters and then forward to my GWT application.  I've gotten this working, but I'm still having trouble passing this data to my application.  I've seen 3 suggested approaches:</p>
  
  <ul>
  <li>Save data to context:  I have this working right now, but I'm not happy with it.  When the servlet is accessed, I parse the parameters and update the context of my GWT web application and then forward to the application where I make an RPC call to read the context.  This does what I want it to, but this creates a race condition when multiple users try to access the application at the same and the context is rapidly changing.</li>
  <li>Store data in session:  I've tried saving the data to the request session in my servlet, and then accessing the session in my RPC, but I always get a new/different session, so I assume I'm mucking this up somewhere.</li>
  </ul>
  
  <p><strong>Save session on servlet</strong></p>
  
  <pre><code>HttpSession session = request.getSession();
  session.setAttribute("test", "testValue");
  response.sendRedirect(response.encodeRedirectURL("/GWT_Application"));
  </code></pre>
  
  <p><strong>Access session in RPC</strong></p>
  
  <pre><code>HttpSession session = this.getThreadLocalRequest().getSession();
  session.getAttribute("test");
  </code></pre>
  
  <p>This returns a different session, which results in the "test" attribute being null.</p>
  
  <ul>
  <li>Pass data in URL:  My application will be opened in an iframe, meaning <code>Window.location.getParameter()</code> will not be usable.</li>
  </ul>
  
  <p>Any help would be greatly appreciated!  I'm still learning GWT and web development in general so don't be afraid to call me out on any obvious or silly mistakes.</p>
  
  <p>Thanks!</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>I figured out what the issue was with my session approach:  the servlet in which I was previously trying to save the session data was in a separate tomcat web app from my GWT application.  Moving them to the same web app solved my problems and it now works.  I'm not sure, but I'm guessing that this was a problem because redirecting to another web app switches the context.  I'll outline my whole approach in the hopes this saves someone else some time later:</p>
  
  <p>Put your servlet code in the server folder of your GWT project:</p>
  
  <pre><code>package GWTApplication.server;
  public class myServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
      session.setAttribute("myAttribute", request.getParameter("myParam");
      // handle rest of POST parameters
      response.sendRedirect(response.encodeRedirectURL("/GWTApplication");
    }
  }
  </code></pre>
  
  <p>Map servlet in your GWT application's web.xml:</p>
  
  <pre><code>&lt;servlet&gt;
    &lt;servlet-name&gt;myServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;GWTApplication.myServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;myServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/myServlet&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>This servlet should now be accessible at .../GWTApplication/myServlet</p>
  
  <p>Next make a standar RPC.  Within whatever method you will be calling in the <code>ServiceImpl</code> class on the server:</p>
  
  <pre><code>HttpSession session = this.getThreadLocalRequest().getSession();
  return session.getAttribute("myAttribute");
  </code></pre>
  
  <p>Finally, make your RPC call in the <code>onModuleLoad()</code> method of you GWT application.  As a recap:</p>
  
  <ol>
  <li>Send the original POST request to the servlet</li>
  <li>Save POST parameters to session variables</li>
  <li>Redirect to GWT application</li>
  <li>Make RPC call in <code>onModuleLoad()</code></li>
  <li>Read session variables in <code>ServiceImpl</code> class</li>
  </ol>