Servlet将空数组传递给JSP?

时间:2013-04-19 04:35:11

标签: java jsp servlets javabeans

我创建了一个使用登录页面,servlet,托管bean和jsp页面的Web应用程序。无论我尝试过什么,我都无法让JSP页面显示我的servlet中使用bean对象的arraylist获取的项目名称数组。我甚至尝试在servlet中创建一个bean对象,然后只传递一个字符串变量。我尝试过使用会话和请求,并且无法让这个工作在这里是我的代码:

的Servlet

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>

The Bean

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;
}

}

请帮助,我已经在这里待了几个小时......

2 个答案:

答案 0 :(得分:0)

您没有提供ItemInventory的代码,因此很难确认iI.getNames()是否确实返回了包含数据的数组。

代码对我来说是正确的,因此如果iI.getNames()返回数据,它应该可以正常工作。

检查阵列的长度。

此外,我发现您期望itemBean但未在会话或请求范围中提供。

此外,servlet上也没有使用itemBean

我错过了什么吗?

答案 1 :(得分:0)

您的Servlet / JSP代码没有任何问题。

此处int y = items.size();这将始终返回0,因为您之前已将其初始化。

你在这里误解了一些概念。当您创建类main()的对象时,将不会直接执行。所以,在你的情况下,它返回空数组。

您应该从main()中删除代码,并且可以编写单独的方法来执行此操作。此外,在构造函数

中包含业务逻辑是不好的做法

示例:ItemInventory.java

public void fillItems(){
    try{
        BufferedReader br = new BufferedReader(new FileReader("/catalog.txt")); 
        String text = null;
        while ((text=br.readLine())!=null){
           String[] itemArray = text.split(","); 
           // you might want to check array size
           items.add(new ItemBean (itemArray[0], itemArray[1], itemArray[2], itemArray[3], itemArray[4]));

        }

        br.close();
    }catch(Exception e){
      e.printStackTrace();
    }
}

在Servlet中

ItemInventory iI = new ItemInventory();
iI.fillItems();
String[] itemNames = iI.getNames();