I have a webpage which is named export.jsp, when the user acces domain/export.jsp, I want him to automatically download a csv file via the doGet method of a servlet. Afterwards I want to display the actual jsp file, which is export.jsp
My doGet method is as follows:
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);
}
}
my web.xml is as follows:
<?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>
Am I doing something wrong ? Because when I enter the url http://localhost:7001/DefectsExporter/export.jsp
in a browser, the doGet method is triggered but the web page export.jsp does not appear.
You cannot do that, at least without Javascript.
A ServletResponse can do only one thing : either return csv data, or return an HTML page. You absolutely need 2 different requests (be them simple normal requests or javascript one) : first to download a csv file, second to display the JSP.
The only way I can imagine to meet such a requirement would be that servlet sends a page that through javascript, and client side, first download (and save) the csv file and then using a different URL sends a request for the JSP page. BUT AFAIK, there no way to do that server side.