MVC简介

A servlet can store data for JSP pages in three main places: in the HttpServletRequest, in the HttpSession, and in the ServletContext. These storage locations correspond to the three nondefault values of the scope attribute of jsp:useBean: that is, request, session, and application.
in the HttpServletRequest Time
ValueObject value = new ValueObject(...);
HttpSession session = request.getSession();
session.setAttribute("key", value);
Next, the servlet would forward to a JSP page that uses the following to retrieve the data:
<jsp:useBean id="key" type="somePackage.ValueObject"
             scope="session" />
Storing data that the JSP page will use in this request and in later requests from any client. First, the servlet would create and store data as follows:
ValueObject value = new ValueObject(...);
getServletContext().setAttribute("key", value);
Next, the servlet would forward to a JSP page that uses the following to retrieve the data:
<jsp:useBean id="key" type="somePackage.ValueObject"
             scope="application" />
the servlet code is normally synchronized to prevent the data changing between the servlet and the JSP page.
The JSP page never creates the objects. The servlet, not the JSP page, should create all the data objects. So, to guarantee that the JSP page will not create objects, you should use
<jsp:useBean ... type="package.Class" />
instead of
<jsp:useBean ... class="package.Class" />.
The scope you specify should match the storage location used by the servlet. For example, the following three forms would be used for request-, session-, and application-based sharing, respectively.
<jsp:useBean id="key" type="somePackage.SomeBeanClass"
             scope="request" />
<jsp:useBean id="key" type="somePackage.SomeBeanClass"
             scope="session" />
<jsp:useBean id="key" type="somePackage.SomeBeanClass"
             scope="application" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值