我最近开始研究 java ee 应用程序。我们大学项目的任务之一是建立一个航班预订系统。为此,我使用了许多 servlet 和一个有状态的 java bean。目前我正在使用的数据被加载到数组中,稍后我将添加一个数据库。
我的理解是,有状态 bean 中的信息应该在会话期间保留在那里,但是,我在途中的某个地方丢失了一组数据。我目前有 3 个 web servlet:
AirportFinder:这会根据用户的搜索词从可用选项中为用户提供起点和目的地机场。
FlightConfirmation:这提供了飞行该航线的航空公司列表以及价格
- BookingConfirmation:这要求用户确认预订详细信息。
我目前只有一个 bean:
- SearchFlights:这是一个有状态的bean,它目前处理数据,搜索数据库并返回结果。
当我尝试确认预订时出现问题。我创建了一个 ArrayList 并在 bean 的构造函数中对其进行了实例化。当 FlightConfrimation servlet 将信息传递回 SearchFlights bean 时,我将结果添加到 ArrayList,然后再将其传递回 FlightConfirmation servlet。
后来,当 BookingConfirmation servlet 需要数据时,我尝试从另一种方法访问此 ArrayList,但我不断收到空指针异常。当我最初添加数据时测试 ArrayList 的大小时,它是正确的。但是下一次 ArrayList 被 BookingConfirmation servlet 查询时,它的大小为 0。我对有状态 bean 应该如何工作的理解不正确吗?
这是我的 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();
}
}