문제

I have recently started studying java ee applications. One of our tasks for a college project is to build a flight booking system. To do this I am using a number of servlets and a stateful java bean. At the moment the data I'm using is loaded into arrays, later I'll add a database.

It's my understanding that the information in the stateful bean should remain there for the duration of the session, however, I'm loosing an array of data somewhere along the way. I currently have 3 web servlets:

  • AirportFinder: this provides the user with start and destination airports from the available options based on their search terms.

  • FlightConfirmation: this provides a list of airlines that fly that route, along with the prices

  • BookingConfirmation: this asks the user to confirm the booking details.

I currently only have one bean:

  • SearchFlights: this is a stateful bean, it currently handles the processing of data, searching the databases and returning the results.

The problem arises when I try to confirm the booking. I have created an ArrayList and instantiated it in the constructor of the bean. When the FlightConfrimation servlet passes information back to the SearchFlights bean I add the results to the ArrayList before passing it back to the FlightConfirmation servlet.

Later, I try to access this ArrayList from another method when the BookingConfirmation servlet needs the data, but I keep getting a null pointer exception. When I test the size of the ArrayList when I initially add the data it's correct. But the next time the ArrayList is queried by the BookingConfirmation servlet it has a size of 0. Is my understanding of how a stateful bean is supposed to work incorrect?

Here is my code for the SearchFlights bean:

public class SearchFlights {

    String[][] bookedFlights;
    String filePath = "C:\\Users\\some path\\Documents\\NetBeansProjects\\FlightFinder\\database_files\\";
    BufferedReader reader;
    ArrayList<String[]> airports;
    ArrayList<String[]> airlines;
    ArrayList<String[]> routes;
    ArrayList<String[]> routeResults;

    public SearchFlights() {
        airports = new ArrayList();
        airlines = new ArrayList();
        routes = new ArrayList();
        routeResults = new ArrayList();

        try {
            reader = new BufferedReader(new FileReader(filePath + "airports.dat"));
            while (reader.ready()) {
                String row = reader.readLine();
                row = row.replaceAll("\"", "");
                airports.add(row.split(","));
            }
            reader = new BufferedReader(new FileReader(filePath + "airlines.dat"));
            while (reader.ready()) {
                String row = reader.readLine();
                row = row.replaceAll("\"", "");
                airlines.add(row.split("\\s*,\\s*"));
            }
            reader = new BufferedReader(new FileReader(filePath + "routes.dat"));
            while (reader.ready()) {
                String row = reader.readLine();
                row = row.replaceAll("\"", "");
                routes.add(row.split("\\s*,\\s*"));
                routes.get(routes.size() - 1)[7] = String.valueOf((int) (Math.random() * 500));
            }

            reader.close();


        } catch (Exception ex) {
            Logger.getLogger(SearchFlights.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public String searchDatabase(String from, String to) {

        ArrayList<String[]> fromIATA = new ArrayList();
        ArrayList<String[]> toIATA = new ArrayList();

        int found = 0;
        for (String[] airport : airports) {

            if (airport[2].toLowerCase().contains(from.toLowerCase())) {

                fromIATA.add(airport);
                found++;

            } else if (airport[2].toLowerCase().contains(to.toLowerCase())) {

                toIATA.add(airport);
                found++;
            }

        StringBuilder form = new StringBuilder();
        form.append("<h2>Select From:</h2>");
        for (int i = 0; i < fromIATA.size(); i++) {
            String[] result = fromIATA.get(i);
            form.append("<input type=\"radio\" name=\"from\" value=\"").append(result[0]).append("\">")
                    .append("<b>").append(result[1]).append("</b>")
                    .append(", ").append(result[2])
                    .append(", ").append(result[3])
                    .append(", ").append(result[4])
                    .append(", ").append(result[5])
                    .append(", ").append(result[6])
                    .append(", ").append(result[7])
                    .append(", ").append(result[8])
                    .append(", ").append(result[9])
                    .append("<br>");

        }
        form.append("<h2>Select To:</h2>");
        for (int i = 0; i < toIATA.size(); i++) {
            String[] result = toIATA.get(i);
            form.append("<input type=\"radio\" name=\"to\" value=\"").append(result[0]).append("\">")
                    .append("<b>").append(result[1]).append("</b>")
                    .append(", ").append(result[2])
                    .append(", ").append(result[3])
                    .append(", ").append(result[4])
                    .append(", ").append(result[5])
                    .append(", ").append(result[6])
                    .append(", ").append(result[7])
                    .append(", ").append(result[8])
                    .append(", ").append(result[9])
                    .append("<br>");
        }


        StringBuilder message = new StringBuilder(
                "<!DOCTYPE html>"
                + "<html>"
                + "<head>"
                + "<title>Flight Finder Results</title>"
                + "</head>"
                + "<body>"
                + "<h1>Available Flights </h1>"
                + "<form action=\"FlightConfirmation\"  method=\"post\">"
                + form.toString()
                + "<input type=\"submit\" value=\"Select Flight\">"
                + "</form>"
                + "</body>"
                + "</html>");



        return message.toString();
    }

    public ArrayList<String[]> findRoutes(String from, String to) {
        ArrayList<String[]> validRoutes = new ArrayList();

        for (String[] route : routes) {

            if (route[3].toLowerCase().equals(from.toLowerCase()) && route[5].toLowerCase().equals(to.toLowerCase())) {
                validRoutes.add(route);
                routeResults.add(route);
            }
        }

        System.out.println("Results size = " + routeResults.size());
        return validRoutes;
    }

    public ArrayList<String> getAirlineNames(ArrayList<String[]> airlineRoutes) {
        ArrayList<String> names = new ArrayList();
        for (String[] airline : airlineRoutes) {

            for (int i = 0; i < airlines.size(); i++) {
                if (airlines.get(i)[3].equals(airline[0])) {
                    names.add(airlines.get(i)[1]);
                }
            }
        }

        return names;
    }

    public String[] getAirportNames(String from, String to) {
        String[] names = new String[2];
        for (String[] airport : airports) {

            if (airport[0].equals(from)) {
                names[0] = airport[1];
            } else if (airport[0].equals(to)) {
                names[1] = airport[1];
            }
        }

        return names;
    }

    public String getResultForConfirmaiton(int i) {
        StringBuilder form = new StringBuilder();

        System.out.println("routeResults called from BookingConfirmation: " + routeResults.size());
        String[] result = routeResults.get(i);
        String[] airportNames = getAirportNames(result[2], result[4]);
        ArrayList<String> airlineNames = getAirlineNames(routeResults);
        form.append("<input type=\"radio\" name=\"selectedflight\" value=\"").append("\">")
                .append("<b>").append(airlineNames.get(i)).append("</b>")
                .append(": ").append(airportNames[0])
                .append(" to ").append(airportNames[1])
                .append(". Price: ").append(result[7])
                .append(".<br>");
        return form.toString();
    }
}
도움이 되었습니까?

해결책

My understanding was incorrect, I believe the answer is that stateful beans remain active with data through method calls from a single servlet, not throughout the whole user process, so once that servlet is finished with the stateful bean the data is lost.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top