Calling a servlet from GWT with post data and a download file created by a servlet

I have an ExportServlet that generates XLSX (Excel) files that my user will request from my GWT application by clicking the export button.

If I use the GET approach, the user is prompted to download the file. The code is as follows:

@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { byte[] xlsx = createTest(""); sendXLSX(resp, xlsx, "test.xlsx"); } catch (Exception e) { e.printStackTrace(); } } void sendXLSX(HttpServletResponse response, byte[] bytes, String name) throws IOException { ServletOutputStream stream = null; stream = response.getOutputStream(); response.setContentType(CONTENT_TYPE_XLSX); response.addHeader("Content-Type", CONTENT_TYPE_XLSX); response.addHeader("Content-Disposition", "inline; filename=" + name); response.setContentLength((int) bytes.length); stream.write(bytes); stream.close(); } 

This is called by the GWT client as follows:

 String url = GWT.getModuleBaseURL() + "ExportServlet"; Window.open(url, "", ""); 

and the user will be prompted to download the file. Ok, that is what I want :)

But I would like to add a lot of data to the request, and afaik, there is a limit to how much data you can put in the URL parameter ("ExportServlet? Data = ..."), so I would, for example, wrap this in a POST request.

I tried the following from GWT:

 String url = GWT.getModuleBaseURL() + "ExportServlet"; RequestBuilder builder = new RequestBuilder( RequestBuilder.POST, url); Request response = builder.sendRequest("test data", new RequestCallback() { @Override public void onResponseReceived(Request request, Response response) { System.out.println(""); } @Override public void onError(Request request, Throwable exception) { System.out.println(""); } }); 

and this is in my servlet:

 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { String data = req.getReader().readLine(); byte[] xlsx = createTest(data); sendXLSX(resp, xlsx, "test.xlsx"); } catch (Exception e) { e.printStackTrace(); } } 

but the user is not prompted to download the file. The doPost method is called and the data is received by the servlet, but should I extract the XLSX file from the response that I get in the GWT application? and how can i do this?

Appreciate any help or comments :)

+8
post servlets download gwt
source share
2 answers

Ok, so I went with the 3rd approach in a related SO question.

Create a FormPanel, put your data in hidden fields, set the URL of your servlet, programmatically request a form submission method and the described problem in this SO question will be solved.

How to retrieve data from hidden fields:

So my doPost looks something like this:

 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iterator = upload.getItemIterator(req); while (iterator.hasNext()) { FileItemStream item = iterator.next(); if (item.isFormField()) { String name = item.getFieldName(); if (name.equalsIgnoreCase("fieldName")) { data = Streams.asString(item.openStream(), ENCODING_UTF8); } } } ... 

I have a hidden field in my form called "fieldName" where data is placed and passed to the servlet. Hope this helps :)

+7
source share

Hi, I have exactly the same problem and solve it using directlyly call servlet using Window.Location.replace(URL.encode(servletUrl));

check also this link related to the problem

+1
source share

All Articles