I hava a servleta that serves files?
I\'m building a page with a \"Download\" button usung a GWT.
How to make a GWT client download a file in an onClick even
COMPLETE EXAMPLE TO DOWNLOAD a .xls FILE USIGN GWT,apache-poi
Environment : GWT SDK 2.4
Spring 3.0.2M
JAVA : 1.7
Needed Jar file to create .xls file :: poi-3.0.1-FINAL.jar
exportButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
String link = GWT.getModuleBaseURL() + "myfiledownload";
Window.open(link , "", "");
}//onClick
});//addClickHandler
Now at the browser when the user hits button exportButton then the control navigates to web.xml and searches for url-pattern that ends with /myfiledownload
web.xml
fileDownload
com.sbabamca.server.FileDownloadServlet
fileDownload
/app/myfiledownload
After execuiting the code in web.xml the controls tries to execute the servlet with name FileDownloadServlet
FileDownloadServlet.java
------------------------
package com.sbabamca.server;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.metadata.ClassMetadata;
import com.fpix.hibernate.util.HibernateUtil;
import com.fpix.util.date.MyDate;
import java.io.IOException;
import java.util.ArrayList;
public class MyFileServlet extends HttpServlet {
SessionFactory sessionFactory;
Session session;
Transaction tx = null;
@SuppressWarnings({ "unchecked", "deprecation" })
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-Disposition", "attachment; filename=users.xls");
try {
sessionFactory = HibernateUtil.getSessionFactory();
if(!sessionFactory.getCurrentSession().isOpen())
{
session = sessionFactory.openSession();
}
else
{
session = sessionFactory.getCurrentSession();
}
tx = session.beginTransaction();
tx.begin();
Query qry = session.createQuery("from com.fpix.dto.User u ");
ArrayList u = (ArrayList)qry.list();
Query qry1 = session.createQuery("from com.fpix.dto.Profile p ");
ArrayList p = (ArrayList)qry1.list();
/*
* code to get the column name of User and Profile Entity
*/
ClassMetadata cm = sessionFactory.getClassMetadata(com.fpix.dto.User.class);
String userAttributes[] = cm.getPropertyNames();
ClassMetadata cm1 = sessionFactory.getClassMetadata(com.fpix.dto.Profile.class);
String profileAttributes[] = cm1.getPropertyNames();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("id");
/*
* code to create the columns names in .xls file
*/
int j = 0;
for(int i=0;i
- 热议问题