如何在显示 jsp 页面之前触发 doGet
How to trigger a doGet before displaying a jsp page
我有一个名为export.jsp的网页,当用户访问domain/export.jsp时,我想让他通过servlet的doGet方法自动下载一个csv文件.之后我想显示实际的 jsp 文件,即 export.jsp
我的doGet方法如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//RequestDispatcher rd = request.getRequestDispatcher("/export.jsp");
//rd.forward(request, response);
//getting and loading the property file
Properties propConfig = new Properties();
Properties propLog = new Properties();
Statement stmt = null;
Connection connection = null;
try {
//Loading the property files
propConfig.load(getServletContext().getResourceAsStream("/WEB-INF/config.properties"));
propLog.load(getServletContext().getResourceAsStream("/WEB-INF/log4j.properties"));
PropertyConfigurator.configure(propLog);
logger.info("loaded log4j properties From Path: /WEB-INF/log4j.properties");
connection = DbTools.getConnection(propConfig.getProperty("db_hostname"),
propConfig.getProperty("db_port"), propConfig.getProperty("db_serviceName"),
propConfig.getProperty("db_userName"),propConfig.getProperty("db_password"));
stmt = connection.createStatement();
ResultSet rset = stmt .executeQuery("SELECT * FROM QACOMPLETE_DEFECT");
File csvfile = FileTools.generateCsvFile(rset);
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "inline; filename=\""+csvfile.getName()+"\"");
OutputStream outputStream = response.getOutputStream();
outputStream.write(Files.readAllBytes(Paths.get(csvfile.getAbsolutePath())));
outputStream.flush();
outputStream.close();
} catch (SQLException e) {
logger.error("SQLException", e);
}catch (IOException e) {
logger.error("IOexception, The user may have cancel the download ", e);
}
finally
{
DbTools.closeQuietly(stmt);
DbTools.closeQuietly(connection);
}
}
我的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<description></description>
<display-name>DefectExporter</display-name>
<servlet-name>DefectExporter</servlet-name>
<servlet-class>exporter.DefectExporter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DefectExporter</servlet-name>
<url-pattern>/export.jsp</url-pattern>
</servlet-mapping>
</web-app>
我做错了什么吗?因为当我在浏览器中输入url http://localhost:7001/DefectsExporter/export.jsp
时,doGet方法被触发但是网页export.jsp没有出现。
你不能那样做,至少没有 Javascript。
ServletResponse 只能做一件事:return csv 数据,或 return HTML 页面。您绝对需要 2 个不同的请求(无论是简单的普通请求还是 javascript 一个请求):第一个是下载 csv 文件,第二个是显示 JSP.
我能想到的满足这种要求的唯一方法是 servlet 通过 javascript 和客户端发送一个页面,首先下载(并保存)csv 文件,然后使用不同的 URL 发送对 JSP 页面的请求。但是 AFAIK,没有办法在服务器端执行此操作。
我有一个名为export.jsp的网页,当用户访问domain/export.jsp时,我想让他通过servlet的doGet方法自动下载一个csv文件.之后我想显示实际的 jsp 文件,即 export.jsp
我的doGet方法如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//RequestDispatcher rd = request.getRequestDispatcher("/export.jsp");
//rd.forward(request, response);
//getting and loading the property file
Properties propConfig = new Properties();
Properties propLog = new Properties();
Statement stmt = null;
Connection connection = null;
try {
//Loading the property files
propConfig.load(getServletContext().getResourceAsStream("/WEB-INF/config.properties"));
propLog.load(getServletContext().getResourceAsStream("/WEB-INF/log4j.properties"));
PropertyConfigurator.configure(propLog);
logger.info("loaded log4j properties From Path: /WEB-INF/log4j.properties");
connection = DbTools.getConnection(propConfig.getProperty("db_hostname"),
propConfig.getProperty("db_port"), propConfig.getProperty("db_serviceName"),
propConfig.getProperty("db_userName"),propConfig.getProperty("db_password"));
stmt = connection.createStatement();
ResultSet rset = stmt .executeQuery("SELECT * FROM QACOMPLETE_DEFECT");
File csvfile = FileTools.generateCsvFile(rset);
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "inline; filename=\""+csvfile.getName()+"\"");
OutputStream outputStream = response.getOutputStream();
outputStream.write(Files.readAllBytes(Paths.get(csvfile.getAbsolutePath())));
outputStream.flush();
outputStream.close();
} catch (SQLException e) {
logger.error("SQLException", e);
}catch (IOException e) {
logger.error("IOexception, The user may have cancel the download ", e);
}
finally
{
DbTools.closeQuietly(stmt);
DbTools.closeQuietly(connection);
}
}
我的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<description></description>
<display-name>DefectExporter</display-name>
<servlet-name>DefectExporter</servlet-name>
<servlet-class>exporter.DefectExporter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DefectExporter</servlet-name>
<url-pattern>/export.jsp</url-pattern>
</servlet-mapping>
</web-app>
我做错了什么吗?因为当我在浏览器中输入url http://localhost:7001/DefectsExporter/export.jsp
时,doGet方法被触发但是网页export.jsp没有出现。
你不能那样做,至少没有 Javascript。
ServletResponse 只能做一件事:return csv 数据,或 return HTML 页面。您绝对需要 2 个不同的请求(无论是简单的普通请求还是 javascript 一个请求):第一个是下载 csv 文件,第二个是显示 JSP.
我能想到的满足这种要求的唯一方法是 servlet 通过 javascript 和客户端发送一个页面,首先下载(并保存)csv 文件,然后使用不同的 URL 发送对 JSP 页面的请求。但是 AFAIK,没有办法在服务器端执行此操作。