📄 filterresultservlet.java
字号:
package com.esri.solutions.jitk.web.tasks.filter;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import javax.faces.FactoryFinder;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.LifecycleFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
/**
* MVS-043/45: Filter Task support servlet. This servlet returns the results in xhtml format.
*/
@SuppressWarnings("serial")
public class FilterResultServlet extends HttpServlet {
private static Logger logger = Logger.getLogger(FilterResultServlet.class.getName());
/**
* Called by the servlet container to indicate to a servlet
* that the servlet is being placed into service.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
//TODO: Download a CSV export (or file from session memory) is a common operation.
// this class should be refactored to a common component and named as such.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException {
try {
String id = request.getParameter("id");
String csv_id = request.getParameter("csv_id");
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext == null) {
FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
facesContext = contextFactory.getFacesContext(request.getSession().getServletContext(), request, response, lifecycle);
}
ServletOutputStream out = response.getOutputStream();
if (id != null) {
String xml = (String)facesContext.getExternalContext().getSessionMap().get(id);
out.write(xml.getBytes());
}
if (csv_id != null) {
//TODO: Where is the Session memory being released? If the answer is nowhere,
// release it at an appropriate place. Do not leak session memory.
String csv = (String)facesContext.getExternalContext().getSessionMap().get(csv_id);
response.addHeader("Content-Disposition", "attachment; filename=Export.csv");
response.addHeader("Content-Length", String.valueOf(csv.getBytes().length) );
response.setContentType("application/octet-stream");
out.write(csv.getBytes());
}
out.flush();
out.close();
}
catch (Exception e) {
logger.log(Priority.ERROR, "Unable to do get", e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -