| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2300 人关注过本帖
标题:jsp:useBean Scope 问题
只看楼主 加入收藏
胡胜彬
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-7-27
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
jsp:useBean Scope 问题
新学jsp技术;
看到javaBean技术,<jsp:useBean scope = "request" />
自己测试,在被请求的页面,不能找到javaBean实例(Attempted a bean operation on a null object.)。
求指教,scope范围现在不支持了么?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("utf-8"); %>
<%@page contentType="text/html; charset=utf-8" %>
<jsp:useBean id="countBean"  class = "com.my.edu.Countnum" scope = "session" />
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>" />
   
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache" />
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="expires" content="0" />   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
    <meta http-equiv="description" content="This is my page" />
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
 
  <body>
        <div style = "text-align: center;">
            <h1>jsp:setProperty和jsp:getProperty的实验</h1>
            <div>
            <form action = "view/example01.jsp" method = "post">
            客户名:<input type = "text" id = "name" name = "name"  size = "15px" /><br />
            电&nbsp;&nbsp;话:<input type = "text" id = "number" name = "number" size="15px" /><br />
            <input type = "submit" value = "提交" />&nbsp;&nbsp;<input type = "reset" value = "重置" />
            </form>
            </div>
        </div>
        <hr/>
        <div>
        <p>网页计数器</p>
        页面访问次数:<%=countBean.getNum() %><br/>
        <hr/>
        <div style="outline: black solid 1px ;">
        请尝试以下操作,观察页面的访问次数的变化情况:<br/>
        1.打开浏览器访问JSP页面。<br />
        2.在浏览器窗口刷新该JSP页面。<br />
        3.再次打开浏览器并访问此JSP网页。<br/>
        4.在网络中的另一台计算机访问此JSP网页。
        </div>
        </div>
  </body>
</html>



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page contentType="text/html; charset=utf-8" %>
<%request.setCharacterEncoding("UTF-8"); %>

<jsp:useBean id="login" class = "com.my.edu.LoginBean"  />
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>" />
   
    <title>My JSP 'example01.jsp' starting page</title>
   
    <meta http-equiv="pragma" content="no-cache" />
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="expires" content="0" />   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
    <meta http-equiv="description" content="This is my page" />
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
 
  <body>
              <div><jsp:getProperty property="num" name="countBean"/></div>
            <div>
            <h1>接受index页面信息by jsp:setProperty和jsp:getProperty</h1>
            <div>
            <!--jsp:setProperty property="*" name="login"/--><!-- 第一种设置属性值方法* -->
            <jsp:setProperty property="name" name="login" param="number" /><!-- 第三种 -->
            <jsp:setProperty property="number" name="login" value = "${param.name }" /><!-- 第4种 -->
            用户名:<jsp:getProperty property="name" name="login"/><br/>
            电话:<jsp:getProperty property="number" name="login"/>
            </div>
            </div>
  </body>
</html>


package com.my.edu;

public class Countnum
{
    private int num = 0;
   
    public void setNum(int num)
    {
        this.num = num;
    }
   
    public int getNum()
    {
        num++;
        return num;
    }

}
搜索更多相关主题的帖子: operation request import null java 
2013-07-27 16:43
ren829
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:15
帖 子:255
专家分:1174
注 册:2006-3-11
收藏
得分:20 
话说你这个Countnum  javaBean 做了吗
2013-07-27 16:54
胡胜彬
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-7-27
收藏
得分:0 
package com.my.edu;

public class Countnum
{
    private int num = 0;
   
    public void setNum(int num)
    {
        this.num = num;
    }
   
    public int getNum()
    {
        num++;
        return num;
    }

}
这个就是啊
2013-07-27 17:09
胡胜彬
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-7-27
收藏
得分:0 
回复 2楼 ren829
package com.my.edu;

public class Countnum
{
    private int num = 0;
   
    public void setNum(int num)
    {
        this.num = num;
    }
   
    public int getNum()
    {
        num++;
        return num;
    }

}
这个就是啊
2013-07-27 17:09
胡胜彬
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-7-27
收藏
得分:0 
问题已解决:在引用页面要使用<jsp:useBean />去获得对象,且属性要不变。
例如:index.jsp创建bean
<jsp:useBean id="login" class="test.my.UserBean" scope="application" />
<jsp:setProperty property="name" name="login" value="后余生" />
<jsp:setProperty property="pass" name="login" value ="123456" />
index02.jsp使用bean
<jsp:useBean id="login" class="test.my.UserBean" scope="application" />
<jsp:getProperty property="name" name="login"/>
显示结果:后余生
2013-07-31 21:47
快速回复:jsp:useBean Scope 问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019633 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved