title: Communicating between an HttpServlet and the client side of a GWT application
body: <p>I have a simple GWT application that consists of a FormPanel that contains a single FileUpload field. The action on this form is to send it to GWT.getModuleBaseURL() + "process", which web.xml in turn tells GWT is the FileProcessServlet.</p>
  
  <p>FileProcessServlet is a class that I made that extends HttpServlet and overrides doPost(HttpServletRequest req, HttpServletResponse resp) to parse the uploaded file and turn it into a Base64 string using Base64Utils. Now, I would like to pass the client side a single string that is the Base64 encoding of the file just sent it. </p>
  
  <p>For the life of me, I just cannot figure out how to do this. The GWT documentation on <a href="http://code.google.com/webtoolkit/doc/latest/tutorial/clientserver.html" rel="nofollow">Communicating with the Server</a> doesn't say anything about receiving information back from a FormPanel.submit(). There is no callback function associated with such a request, as is the case with GWT RPC. At the same time, I need to use a servlet, since I want to parse a file, and that can't be done by the client. Any suggestions? </p>
  
  <p>~~~~</p>
  
  <p>PS: One option is that I can write directly to the html page from the servlet using java.io's PrintWriter (as <a href="http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-First-Servlets.html" rel="nofollow">this</a> suggests). This may present a kind of solution where I store the Base64 string in a div with a special ID and then use DOM to get this content on the client end. However, I have not yet gotten PrintWriter to cooperate with me. Anytime I use it, with varying content types and character encodings, I still see nothing printed on the page. What I currently have attempting to print this out is:</p>
  
  <pre><code>        String base64 = Base64Utils.toBase64(file);
          resp.setContentType("text/html; charset=UTF-8");
          resp.setCharacterEncoding("UTF-8");
          PrintWriter out = resp.getWriter();
          out.print(base64);
          out.flush(); out.close();
  </code></pre>
  
  <p>But nothing comes out. The debugger has confirmed that the string base64 is not null nor empty on the penultimate line. Any help on this related front would also be appreciated. Nonetheless, I sincerely hope there is a better way.</p>