"J2ee "
This Section focuses on Servlets,JSPs,JNDI,EJB,JMS.
Q) Java File Download Servlet
Ans)
public class FileDownloadServlet extends HttpServlet {
private static final long serialVersionUID = 8305367618713715640L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
} catch (Exception e) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"error occurred while processing: " + e.getMessage());
}
Servlet - Excel Sheet - DownLoads Data as excel Sheet.
This Example shows how to download Data as a Excel sheet By Using Java POI.
Step 1 :
Create a FileDownloadServlet.java
Create a FileDownloadServlet.java
-------------------------------------------
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileDownloadServlet extends HttpServlet {
private static final long serialVersionUID = 8305367618713715640L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
DownloadHandler downloadHandler = new DownloadHandler(req,resp);
String Result = downloadHandler.processAction();
String Result = downloadHandler.processAction();
} catch (Exception e) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"error occurred while processing: " + e.getMessage());
}
}
}
Step 2 :
}
Step 2 :
Implement DownloadHandler.java
-------------------------------------------------------------------------------------
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
-------------------------------------------------------------------------------------
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.comcast.commsales.trunking.client.model.CustomerDataVO;
import com.comcast.commsales.trunking.client.model.validator.ValueValidator;
import com.comcast.commsales.trunking.client.proxy.TrunkingService;
import com.comcast.commsales.trunking.client.model.validator.ValueValidator;
import com.comcast.commsales.trunking.client.proxy.TrunkingService;
public class DownloadHandler {
HttpServletRequest req;
HttpServletResponse resp;
public DownloadHandlerImpl(){
}
public DownloadHandlerImpl(HttpServletRequest req, HttpServletResponse resp){
this.req = req;
this.resp = resp;
}
@SuppressWarnings("unchecked")
public String processAction() throws Exception {
OutputStream os = resp.getOutputStream();
HttpSession session = req.getSession();
Boolean isTemplate = (Boolean)session.getAttribute("isTemplate");
ArrayList<CustomerDataVO> CustomerDataVOList = ( ArrayList<CustomerDataVO>)session.getAttribute("exportedCustomerData");
session.removeAttribute("isTemplate"); //Remove the Session Attribute
session.removeAttribute("exportedTFS"); //Remove the Session Attribute
resp.setContentType("application/vnd.ms-excel");
String displayname="tollFree.xls";
resp.setHeader("Content-Disposition","inline; filename=\"" + displayname + "\"");
try{
HttpServletResponse resp;
public DownloadHandlerImpl(){
}
public DownloadHandlerImpl(HttpServletRequest req, HttpServletResponse resp){
this.req = req;
this.resp = resp;
}
@SuppressWarnings("unchecked")
public String processAction() throws Exception {
OutputStream os = resp.getOutputStream();
HttpSession session = req.getSession();
Boolean isTemplate = (Boolean)session.getAttribute("isTemplate");
ArrayList<CustomerDataVO> CustomerDataVOList = ( ArrayList<CustomerDataVO>)session.getAttribute("exportedCustomerData");
session.removeAttribute("isTemplate"); //Remove the Session Attribute
session.removeAttribute("exportedTFS"); //Remove the Session Attribute
resp.setContentType("application/vnd.ms-excel");
String displayname="tollFree.xls";
resp.setHeader("Content-Disposition","inline; filename=\"" + displayname + "\"");
try{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("Customer FistName");
rowhead.createCell((short) 1).setCellValue("Customer LastName");
if(isTemplate != null && !isTemplate.booleanValue()){
int index = 1;
for(CustomerDataVO vo : customerDataVOList){
HSSFRow row1 = sheet.createRow((short) index);
row1.createCell((short) 0).setCellValue(vo.getFirstName());
row1.createCell((short) 1).setCellValue(vo.getLastName());
index++;
}
}else{
HSSFRow row1 = sheet.createRow((short)1);
row1.createCell((short) 0).setCellValue("xxxxxxxx");
row1.createCell((short) 1).setCellValue("xxxxxxxx");
HSSFRow row2 = sheet.createRow((short) 2);
row2.createCell((short) 0).setCellValue("Required");
row2.createCell((short) 1).setCellValue("Required");
}
wb.write(os);
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("Customer FistName");
rowhead.createCell((short) 1).setCellValue("Customer LastName");
if(isTemplate != null && !isTemplate.booleanValue()){
int index = 1;
for(CustomerDataVO vo : customerDataVOList){
HSSFRow row1 = sheet.createRow((short) index);
row1.createCell((short) 0).setCellValue(vo.getFirstName());
row1.createCell((short) 1).setCellValue(vo.getLastName());
index++;
}
}else{
HSSFRow row1 = sheet.createRow((short)1);
row1.createCell((short) 0).setCellValue("xxxxxxxx");
row1.createCell((short) 1).setCellValue("xxxxxxxx");
HSSFRow row2 = sheet.createRow((short) 2);
row2.createCell((short) 0).setCellValue("Required");
row2.createCell((short) 1).setCellValue("Required");
}
wb.write(os);
}catch(Exception e){
}finally{
os.flush();
os.close();
resp.flushBuffer();
}
os.flush();
os.close();
resp.flushBuffer();
}
return "";
}
}
}
}