我创建了一个使用登录页面、servlet、托管 bean 和 jsp 页面的 Web 应用程序。无论我尝试过什么,我都无法让 JSP 页面显示来自我的 servlet 的项目名称数组,这些项目名称是使用 bean 对象的数组列表获取的。我什至尝试在 servlet 中创建一个 bean 对象,然后只传递一个字符串变量。我已经尝试同时使用会话和请求,但无法使其正常工作,这是我的代码:
小服务程序:
package xxxx;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ItemServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ItemInventory iI = new ItemInventory();
String[] itemNames = iI.getNames();
HttpSession session = request.getSession();
session.setAttribute("itemNames", itemNames);
request.setAttribute("itemNames", itemNames);
RequestDispatcher rd = request.getRequestDispatcher("/secureApps/ItemsCatalog.jsp");
rd.forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
JSP:
<%@page import="xxxx.ItemInventory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String[] itemName = (String[])session.getAttribute("itemNames");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Item Catalog</title>
</head>
<body>
<h1>Item Catalog</h1>
<Select Name="itemNames">
<% for(int i=0;i < itemName.length; i++){
%>
<option value ="<%= itemName[i]%>"><%= itemName[i]%></option>
<%
}
%>
</select>
${itemBean.itemID}
</body>
</html>
豆子
package xxxx;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class ItemBean {
String itemName;
String itemID;
String itemDescription;
String itemPrice;
String itemQuantity;
public ItemBean(){}
public ItemBean(String iD, String name, String description, String price, String quantity){
this.itemID = iD;
this.itemName = name;
this.itemDescription = description;
this.itemPrice = price;
this.itemQuantity = quantity;
}
public String getItemName(){
return itemName;
}
}
ItemInventory 类:
package xxxx;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class ItemInventory {
ArrayList <ItemBean> items = new ArrayList<ItemBean>();
public static void main(String[] args){}
public void ItemInventory() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("/catalog.txt"));
String text = null;
while ((text=br.readLine())!=null){
String[] itemArray = text.split(",");
items.add(new ItemBean (itemArray[0], itemArray[1], itemArray[2], itemArray[3], itemArray[4]));
}
br.close();
}
public Integer getSize(){
Integer x = items.size();
return x;
}
public String[] getNames(){
int y = items.size();
String[] itemNames = new String[y];
for (int i = items.size() - 1; i >=0; i--){
itemNames[i] = items.get(i).getItemName();
}
return itemNames;
}
}
请帮忙,我已经在这几个小时了....