file download invoke servlet issues

280 views
Skip to first unread message

Steve

unread,
Jan 8, 2008, 12:42:53 PM1/8/08
to Google Web Toolkit
I know variations of this have been posted already but after reading
several posts i still can't get my servlet to work and it's starting
to drive me nuts.

Servlet is trying to download a file to the browser and display the
Open/Save dialog for the file. Servlet executes correctly with no
errors but fails to display the Open/Save dialog.

Has anyone any ideas why the code/config below would not work. Running
the app in hosted mode.

In web.xml under tomcat.webapps.ROOT/WEB-INF i have:

<servlet>
<servlet-name>BackUpServlet</servlet-name>
<servlet-class>com.myApp.server.servlets.BackUpServlet</servlet-
class>
</servlet>

<servlet-mapping>
<servlet-name>BackUpServlet</servlet-name>
<url-pattern>/com.myApp.server.servlets/BackUpServlet</url-pattern>
</servlet-mapping>

Servlet invoked:

RequestBuilder rb = new RequestBuilder(RequestBuilder.POST,
GWT.getModuleBaseURL() + "BackUpServlet");

try {
rb.sendRequest("BackUp", new RequestCallback() {
public void onError(Request request, Throwable exception) {
Window.alert("Error backing up file");
}

public void onResponseReceived(Request request, Response
response) {
System.out.println("response Recieved");
}
});

} catch (RequestException e) {
e.printStackTrace();
}

Servlet code:

package com.myApp.server.servlets;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BackUpServlet extends HttpServlet

{
private static final long serialVersionUID = 1L;

public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException

{
byte[] backUpData =
(byte[])request.getSession().getAttribute("backupbytes");

//use todays date as the filename;
Calendar todaysDate = Calendar.getInstance();

String filename = "" + todaysDate.get(Calendar.DAY_OF_MONTH)+
todaysDate.get(Calendar.MONTH)+todaysDate.get(Calendar.YEAR)+
todaysDate.get(Calendar.HOUR)+
todaysDate.get(Calendar.MINUTE)+
todaysDate.get(Calendar.MILLISECOND)+".001";

try {
File file = new File(filename);

ServletOutputStream servletOutputStream =
response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType( filename );

response.setContentType( (mimetype != null) ? mimetype :
"application/octet-stream" );
response.setContentLength( (int)file.length() );
response.setHeader( "Content-Disposition", "attachement; filename=
\"" + filename + "\"" );

servletOutputStream.write(backUpData);
servletOutputStream.flush();
servletOutputStream.close();

} catch (Exception e) {
e.printStackTrace();
}
}

Steve

unread,
Feb 8, 2008, 8:20:26 AM2/8/08
to Google Web Toolkit
Finally got this working by using a FakeIFrame class and simplyfing
code in the servlet:

Servlet:

try {
response.setContentType("application/download" );
response.setHeader( "Content-Disposition", "attachment; filename=
\"" + filename);

ServletOutputStream servletOutputStream =
response.getOutputStream();

servletOutputStream.write(backUpData);
servletOutputStream.flush();
servletOutputStream.close();

} catch (Exception e) {
e.printStackTrace();
}

FakeIFrame :

public class FakeIFrame extends Frame {
public FakeIFrame(String url) {
super();
setSize("0px", "0px");
setVisible(false);
sinkEvents(Event.ONLOAD);
RootPanel.get().add(this);
setUrl(url);
}

public void onBrowserEvent(Event event) {
if (DOM.eventGetType(event) == Event.ONLOAD) {
unsinkEvents(Event.ONLOAD);
DOM.eventCancelBubble(event, true);
RootPanel.get().remove(this);
} else {
super.onBrowserEvent(event);
}
}
}

Invocation:

// Load url i.e. backupServlet in a fake iframe window
new FakeIFrame(GWT.getModuleBaseURL() + "BackUpServlet");

Added to web.xml in tomcat.webapps.ROOT :

<servlet>
<servlet-name>BackUpServlet</servlet-name>
<servlet-class>com.mybusiness.servlets.BackUpServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>BackUpServlet</servlet-name>
<url-pattern>/BackUpServlet</url-pattern>
</servlet-mapping>

On Jan 8, 5:42 pm, Steve <stephen.lloydhi...@gmail.com> wrote:
> I know variations of this have been posted already but after reading
> several posts i still can't get myservletto work and it's starting
> to drive me nuts.
>
> Servletis trying todownloada file to the browser and display the
> Open/Save dialog for the file.Servletexecutes correctly with no

oleng

unread,
Feb 11, 2008, 11:39:22 AM2/11/08
to Google Web Toolkit
Thanks for figuring this out. I had the exact same requirement with a
slight variation (I needed to
export out data to Excel format). I got this to work using your
solution.

oleng

unread,
Feb 11, 2008, 11:39:22 AM2/11/08
to Google Web Toolkit
Thanks for figuring this out. I had the exact same requirement with a
slight variation (I needed to
export out data to Excel format). I got this to work using your
solution.

On Feb 8, 8:20 am, Steve <stephen.lloydhi...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages