public class MyFileServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
resp.setContentType("text/plain");
resp.setHeader("Content-Disposition", "attachment; filename=output.txt");
PrintWriter out = resp.getWriter();
out.println("This is the output content");
out.println("Probably something dynamic should go in here");
}
}
Then you'll want to write the client side to fetch the file.
public class MyEntryPoint implements EntryPoint {
public void onModuleLoad() {
String link = GWT.getModuleBaseURL() + "servlet/myfiledownload";
RootPanel.get().add(new HTML("<a href=\"" + link + "\">Download File</a>"));
}
}
You can also use Window.open(link, "downloadWindow", "");
to download the file from an EventListener.
Finally you'll need to configure the servlet in either your Module.gwt.xml file
(for hosted mode), or in your web.xml file for web mode.
Module.gwt.xml example, add:
<servlet path="/servlet/myfiledownload"
class="your.package.name.here.MyFileServlet" />
web.xml add:
<servlet>
<servlet-name>MyFileServlet</servlet-name>
<servlet-class>your.package.name.here.MyFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyFileServlet</servlet-name>
<url-pattern>/your.package.name.here/servlet/myfiledownload</url-pattern>
</servlet-mapping>
Like I show in the web.xml example, you'll need to make sure that the servlet is
bound to the module base directory (where the nocache.html files all live), and
not next to the host HTML page. Another important factor is: the Servlet must
not be in your "client" package, since the GWT compiler shouldn't get hold of it.
Hope this helps.
Jason.
HTTP ERROR: 405
HTTP method GET is not supported by this URLRequestURI=/name/Download