JSTL使用入门(续1)

file name:ImplicitObjectScope.jsp

<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<jsp:useBean id="simpleBean" scope="request" class="jsptag.SimpleBean">
</jsp:useBean>
<html>
<head>
<title>
ImplicitObjectScope
</title>
</head>
<body bgcolor="#ffffff">
<h3>
ImplicitObjectScope.jsp
</h3>
<p><strong>
 首先设定作用域分别为application/session/request/page的四个变量
</strong></p>
<c:set var="applicationVar" value="applicationVarValue" scope="application"/>
<c:set var="sessionVar" value="sessionVarValue" scope="session"/>
<c:set var="requestVar" value="requestVarValue" scope="request"/>
<c:set var="pageVar" value="pageVarValue" scope="page"/>
<%//同上等效
/*
session.setAttribute("sessionVar","sessionVarValue");
application.setAttribute("applicationVar","applicationVarValue");
request.setAttribute("requestVar","requestVarValue");
pageContext.setAttribute("pageVar","pageVarValue");
*/%>
session variable value:
<c:out value="${sessionScope.sessionVar}" default=" there is not session  variable"/>
<br />
application variable value:
<c:out value="${applicationScope.applicationVar}" >
  there is not application variable"
</c:out>
<br />
request variable value:
<c:out value="${requestScope.requestVar}">
  there is not request variable
</c:out>
<br />
page variable value:
<c:out value="${pageScope.pageVar}">
  there is not page variable
</c:out>
<p>
<form action="ImplicitObjectScope.jsp" method="post">
  <input type="hidden" name="requestName" value="this is the value of requestName"/>
  <input type="hidden" name="isForward" value="Y"/>
  <input type="submit" value="点击此处提交到本页,并将请求转发到ImplicitObjectScope1页面"/>
</form>
<form action="ImplicitObjectScope.jsp" method="post">
  <input type="hidden" name="requestName" value="this is the value of requestName"/>
  <input type="hidden" name="isForward" value="Y"/>
  <input type="hidden" name="isForwarAgain" value="Y"/>
  <input type="submit" value="点击此处提交到本页,并将请求转发到ImplicitObjectScope1页面,并再次将请求转发到destOfForward页面"/>
</form>
<form action="ImplicitObjectScope1.jsp" method="post">
  <input type="hidden" name="requestName" value="this is the value of requestName"/>
  <input type="submit" value="点击此处,直接提交到ImplicitObjectScope1页面"/>
</form>
<form action="ImplicitObjectScope1.jsp" method="post">
  <input type="hidden" name="requestName" value="this is the value of requestName"/>
  <input type="hidden" name="isForwarAgain" value="Y"/>
  <input type="submit" value="点击此处,直接提交到ImplicitObjectScope1页面,再将请求转发到destOfForward页面"/>
</form>
</p>
<c:if test="${not empty param.isForward}">
 <jsp:forward page="ImplicitObjectScope1.jsp" />
</c:if>
<p>
  以下为使用forEach 标记实现累加的范例(EL结合JavaBean的使用)。
</p>
<c:set value="0" var="count">
</c:set>
<table border="1">
 <tr>
  <td>index</td><td>number</td><td>sum</td>
 </tr>
<c:forEach items="${simpleBean.numberList}" var="number" varStatus="status">
 <tr>
  <td><c:out value="${status.index}"/></td>
  <td>
  <c:out value="${number}"/>
  <c:set var="count2" value="${count}"/>
  <c:set var="count" value="${count2+number}"/>
  </td>
  <td>count:<c:out value="${count}"/></td>
 </tr>
</c:forEach>
</body>
</html>



file name:ImplicitObjectScope1.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<jsp:useBean id="simpleBean" scope="request" class="jsptag.SimpleBean">
</jsp:useBean>
<jsp:setProperty name="simpleBean" property="*" />
<c:if test="${not empty param.isForwarAgain}">
 <jsp:forward page="destOfForward.jsp"/>
</c:if>
<html>
<head>
<title>
ImplicitObjectScope1
</title>
</head>
<body bgcolor="#ffffff">
<h3>
ImplicitObjectScope1.jsp
</h3>
<p>
 注意比较不同作用域的变量,其值有无变化
</p>
<p>
session variable value:
<c:out value="${sessionScope.sessionVar}" default=" there is not session  variable"/>
<br />
application variable value:
<c:out value="${applicationScope.applicationVar}" >
  there is not application variable"
</c:out>
<br />
request variable value:
<c:out value="${requestScope.requestVar}">
  there is not request variable
</c:out>
<br />
page variable value:
<c:out value="${pageScope.pageVar}">
  there is not page variable
</c:out>
</p>
<p>
如下,为EL结合JavaBean的例子:
<br />
output value of reqeustName:
<c:out value="${simpleBean.requestName}">
  the simpleBean.requestName is not existed.
</c:out>
</p>
</table>
</body>
</html>


file name:destOfForward.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<jsp:useBean id="simpleBean" scope="page" class="jsptag.SimpleBean"/>
<%--试比较将scope改为request有何不同--%>
<%--jsp:useBean id="simpleBean" scope="page" class="jsptag.SimpleBean"/--%>
<html>
<head>
<title>
destOfForward
</title>
</head>
<body bgcolor="#ffffff">
<h3>
destOfForward.jsp
</h3>
<p>
注意比较此处输出的变化
<br />
request variable:
<strong>
<c:out value="${requestScope.requestVar}">
  there is not request variable
</c:out>
</strong>
</p>
<br />
output value of reqeustName:
<strong>
<c:out value="${simpleBean.requestName}">
  the simpleBean.requestName is not existed.
</c:out>
</strong>
<br />
<p>
注意:在这里,JavaBean(SimpleBean)的scope为page,与前页为request不同,所以输出为不存在的信息。
</p>
</body>
</html>
file name:SimpleBean.java
package jsptag;

/**
 * <p>Title: </p>
 * <p>Description: A Simple Java Bean</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author zqy
 * @version 1.0
 */
import java.util.ArrayList;
public class SimpleBean {

 private String requestName = null;
 private ArrayList numberList=new ArrayList();

 public SimpleBean() {
  for(int i=1;i<=10;i++)
   this.numberList.add(String.valueOf(i));
 }

 public void setRequestName(String requestName) {
  this.requestName = requestName;
 }

 public String getRequestName() {
  return this.requestName;
 }

 public void setNumberList(ArrayList numberList)
 {
  this.numberList=numberList;
 }

 public ArrayList getNumberList()
 {
  return this.numberList;
 }
}

补:
下面这段话关于如何区别page scope和request scope:
"Objects are created within a JSP page instance that is responding to a request object. There are several scopes:
 page - Objects with page scope are accessible only within the page where they are created. All references to such an object shall be released after the response is sent back to the client from the JSP page or the request is forwarded somewhere else. References to objects with page scope are stored in the pageContext object.
 request - Objects with request scope are accessible from pages processing the same request where they were created. References to the object shall be re-leased after the request is processed. In particular, if the request is forwarded to a resource in the same runtime, the object is still reachable. References to objects with request scope are stored in the request object."

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值