1

I have successfully deployed a Java EE app to openshift with tomcat7. I deployed the war file and the welcome page loads but when my app tries to read an input .txt file to the application it can't find it. Any help would be appreciated.

My input files (2) are in the /WEB-INF folder in my project in eclipse.

This is my relevant code from my startup servlet:

    public void doPost(HttpServletRequest req,
        HttpServletResponse resp) throws ServletException, IOException {

        //  11 lines of code setting strings according to input parameters
        //  from startup jsp which has forms for submitting operations

        if ("Dates and Draws".equals(dates))  {

            Powerball p = new Powerball();
            Megamillions m = new Megamillions();

            String path = this.getServletConfig().getServletContext().getRealPath("/WEB-INF");

            p.appPath = path; // appPath is a class variable
            m.appPath = path;

            p.readnums(req, resp);  // App is not reading input files in readnums methods

            m.readnums(req, resp);



            try  {

                // code for output using readnums methods, reading input files

                } finally {


                }


        }
    }

Powerball servlet readnums (same as Megamillions servlet readnums)

    void readnums(HttpServletRequest req,
        HttpServletResponse resp) throws ServletException, IOException , FileNotFoundException {

        int i,j;
        String Temp;
        String [] Temp2 = new String [80];


        String path = appPath + "/powerballnumbers.txt";

        File file_check = new File (path);

        if (!file_check.exists()) {

            errorMessage = "Input file not found";  //This is what gets displayed

            resp.setContentType("text/html");
            PrintWriter out = resp.getWriter();


            out.println("<html>");
            out.println("<body>");
            out.println("<h3>" + errorMessage + "</h3>");
            out.println("<br><br>");

            out.println("<form action=\"Startup\" method=\"get\"");
            out.println("Go back to previous page: ");
            out.println("<br><br>");
            out.println("<input type=\"submit\" name=\"action\" value=\"Go Back to previous page\">");
            out.println("</form>");

            out.println("</body>");
            out.println("</html>");

            out.close();

            return;

        } else  {

            errorMessage = "";

        }


        draws = 1;

        BufferedReader br = null;

        br = new BufferedReader (new FileReader(new File(path)));

        while ((Temp = br.readLine()) != null) {

            if (Temp.isEmpty()) break;

            Temp2 = Temp.split("\\s+");

            int LastNonEmpty = -1;

            for (i = 0; i < Temp2.length; i++) {

                if (Temp2[i] != "") {

                    LastNonEmpty += 1;

                    Temp2[LastNonEmpty] = Temp2[i];

                }

            }

            drawdate [draws] = Temp2 [0];

            j = 1;

            for (i = 1; i <= 5; i++) {

                Temp = Temp2 [j];

                nums [draws][i] =  Integer.parseInt(Temp); // nums is class variable

                j ++;
            }

            Temp = Temp2 [6];

            xball [draws] =  Integer.parseInt(Temp); // xball is class variable

            draws++;
        }

        br.close();

        draws--;


    }
te7
  • 601
  • 1
  • 6
  • 23
  • Food for thought: http://stackoverflow.com/questions/12160639/what-does-servletcontext-getrealpath-mean-and-when-should-i-use-it – BalusC Jul 25 '15 at 17:13
  • @BalusC Thanks for that. getRealPath() seems to be working though. I don't want to go around trying to fix something that is working so far. I read that link. Very good post by you. If you think I absolutely shouldn't use it let me know and I'll try the changes proposed by your post in that link. – te7 Jul 25 '15 at 18:14
  • How would I reference an input file in the WEB-INF folder with servletContext.getResourcePaths(/)? – te7 Jul 25 '15 at 18:21
  • Since the servlet class that contains the "readnums" method never gets instanced by the server, it never gets a context. So I passed to that servlet's doPost method the Startup context that gets instanced by the server with "ServletContext context = getServletContext();". I passed "context" to the doPost method in the second class, and then onto the "readnums" method that uses getResourceAsStream, using that passed context: "InputStream input = context.getResourceAsStream(path);" – te7 Jul 27 '15 at 05:13
  • Info: getResourcePaths returns all the paths to each file in /WEB-INF in one string variable...not usable as far as I can tell. FYI. – te7 Jul 27 '15 at 05:13

1 Answers1

2

Make sure that the unpackWARs attribute is set to true in server.xml, otherwise the war file will not be expanded and Tomcat will not be able to find your file.

David Levesque
  • 22,181
  • 8
  • 67
  • 82
  • In my eclipse project the server.xml is under tomcat. the unpackWars is set to true. Wouldn't that be a configuration for the tomcat server at openshift? Can I include this setting to my war file somewhere? – te7 Jul 25 '15 at 15:00
  • See the `Other Tips` section at the bottom of this page: https://developers.openshift.com/en/tomcat-getting-started.html#step3 – David Levesque Jul 25 '15 at 15:02
  • That fixed it. Thanks a million! – te7 Jul 25 '15 at 15:11