lundi 22 juin 2015

How to trigger a doGet before dispalying a jsp page

I have a webpage which is named export.jsp, when the user acces domain/export.jsp I want to execute a doGet and only after executing this doGet method I want to display the actual jsp page.

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://ift.tt/ra1lAU" xmlns="http://ift.tt/nSRXKP" xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/LU8AHS" 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.

Aucun commentaire:

Enregistrer un commentaire