【发布时间】:2018-05-06 11:15:05
【问题描述】:
我的 j2EE 项目中有一个 servlet,我正在为硬木项目计算一些材料。我有一个 ArrayList,我在其中添加了所需的必要数量的材料。我想将 ArrayList 设置为 reqeust 属性,以便最终可以在 jsp 页面上显示它们。
String execute(HttpServletRequest request, HttpServletResponse response) throws LoginSampleException {
//here is going to a mehtod to acces to database and get the information
ArrayList<Tree> trees = new ArrayList<>();
for(Tree tree: trees){
int amount = tree.calculate(tree.getLength(), tree.getLengthPrUnit());
ArrayList <Integer> amountMaterials = new ArrayList<>();
amountMaterials.add(amount);
request.setAttribute("amountMaterials", amountMaterials);
}
return null; // here I'm eventually going to redirect to my jsp-page
}
我应该将 request.setAttribute 放在循环之外还是无关紧要
这里是替代版本
ArrayList<Integer> amountMaterials = null;
ArrayList<Tree> trees = new ArrayList<>();
for(Tree tree: trees){
int amount = tree.calculate(tree.getLength(), tree.getLengthPrUnit());
amountMaterials.add(amount);
}
request.setAttribute("amountMaterials", amountMaterials);
return null;
}
【问题讨论】:
标签: jsp servlets jakarta-ee