JavaBean在Servlet和JSP之间的传递
JavaBean可以灵活的在Servlet和JSP之间进行传递,首先我们在Servlet的创建一个JavaBean,如果将该JavaBean放在request内,在JSP获取的时候要指定scope为request。例如,在Servlet中这样:
CCBBean ccb = new CCBBean();
ccb.setPosID(posID);
ccb.setBranchID(branchID);
ccb.setOrderID(orderID);
ccb.setPayment(payment);
ccb.setCurCode(curCode);
ccb.setRemark1(remark1);
ccb.setRemark2(remark2);
ccb.setAccType("01");
ccb.setSuccess("Y");
request.setAttribute("ccb", ccb);
可以看出我们在request中放入一个CCBBean,ID为ccb。在JSP获取JavaBean时这样写:
<jsp:useBean id="ccb" class="bean.CCBBean" scope="request"></jsp:useBean>
CCBBean ccb = new CCBBean();
ccb.setPosID(posID);
ccb.setBranchID(branchID);
ccb.setOrderID(orderID);
ccb.setPayment(payment);
ccb.setCurCode(curCode);
ccb.setRemark1(remark1);
ccb.setRemark2(remark2);
ccb.setAccType("01");
ccb.setSuccess("Y");
request.setAttribute("ccb", ccb);
可以看出我们在request中放入一个CCBBean,ID为ccb。在JSP获取JavaBean时这样写:
<jsp:useBean id="ccb" class="bean.CCBBean" scope="request"></jsp:useBean>