Java 如何在jsp文件中而不是在servlet中显示设置的对象值?
晚上好!我在尝试转换这些代码块时遇到了问题,因此它将显示在jsp中而不是servlet中。我不知道如何检索cartBean中的项目。 这是我的手推车控制器Java 如何在jsp文件中而不是在servlet中显示设置的对象值?,java,jsp,servlets,Java,Jsp,Servlets,晚上好!我在尝试转换这些代码块时遇到了问题,因此它将显示在jsp中而不是servlet中。我不知道如何检索cartBean中的项目。 这是我的手推车控制器 <% session.setMaxInactiveInterval(1800); String action = request.getParameter("action"); if(action.equals("Add")) try{ String upc = request.getPara
<%
session.setMaxInactiveInterval(1800);
String action = request.getParameter("action");
if(action.equals("Add"))
try{
String upc = request.getParameter("upc");
String name = request.getParameter("name");
int quantity = Integer.parseInt(request.getParameter("quantity"));
double price = Double.parseDouble(request.getParameter("price"));
synchronized(session) // lock session protect this from multiple threads
{
ShoppingCart cart = (ShoppingCart)session.getAttribute("Cart");
if(cart == null) // new sesssion, just create a cart
{
cart = new ShoppingCart();
session.setAttribute("Cart", cart);
}
cart.add(item);
cart.display(out);
}
}
catch(Exception ex)
{
out.println(ex.toString());
}
}
else
if(action.equals("Empty"))
{
synchronized(session)
{
ShoppingCart cart = (ShoppingCart)session.getAttribute("Cart");
if(cart == null)
{
cart = new ShoppingCart();
session.setAttribute("Cart", cart);
}
cart.empty();
cart.display(out);
}
}
%>
}
我试图获取session属性,但不知道如何检索我在购物车中添加的产品。谢谢你的帮助 我已经解决了。如果有人想知道怎么做,下面是怎么做的:
public ArrayList<productBean> getItemList() {
return itemList;
}
public void setItemList(ArrayList<productBean> itemList) {
this.itemList = itemList;
}
public ArrayList getItemList(){
返回项目列表;
}
公共无效setItemList(ArrayList itemList){
this.itemList=itemList;
}
我在我的项目bean中添加了这个
public void display(JspWriter out)
{
try{
java.text.DecimalFormat currency = new java.text.DecimalFormat("$ #,###,##0.00");
out.println("<h3>Cart contents</h3>");
out.println("<table border=1>");
out.println("<tr><th>UPC</th><th>Name</th><th>Price</th><th>Quantity</th><th>Total</th></tr>");
double total = 0;
for(int i = 0; i < itemlist.size(); i++)
{
Item item = (Item)itemlist.get(i);
out.println("<tr><td>"+item.upc+"</td>"+
"<td>"+item.name+"</td>"+
"<td align=right>"+ currency.format(item.price)+"</td>"+
"<td align=right>"+ item.quantity+"</td>"+
"<td align=right>"+ currency.format(item.price*item.quantity)+"</td></tr>");
total += item.price*item.quantity;
}
out.println("<tr><td colspan = 4>Total purchase</td>");
out.println("<td align=right>"+currency.format(total)+"</td></tr>");
out.println("<tr><td colspan = 4>Sales tax @6%</td>"+
"<td align=right>"+ currency.format(total*.06)+"</td></tr>");
out.println("<tr><td colspan = 4>Amount due</td>"+
"<td align=right>"+ currency.format(total*1.06)+"</td></tr>");
out.println("</table>");
}
catch(IOException ex)
{
System.err.println(ex.toString()); // just send the exception to the error log
}
public class Item {
String upc = "";
String name = "";
int quantity = 0;
double price = 0;
public Item()
{
}
public Item(String upc, String name, int quantity, double price)
{
this.upc = upc;
this.name = name;
this.quantity= quantity;
this.price = price;
}
public ArrayList<productBean> getItemList() {
return itemList;
}
public void setItemList(ArrayList<productBean> itemList) {
this.itemList = itemList;
}