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 />
电 话:<input type = "text" id = "number" name = "number" size="15px" /><br />
<input type = "submit" value = "提交" /> <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
----------------解决方案--------------------------------------------------------
话说你这个Countnum javaBean 做了吗
----------------解决方案--------------------------------------------------------
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;
}
}
这个就是啊
----------------解决方案--------------------------------------------------------
回复 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;
}
}
这个就是啊
----------------解决方案--------------------------------------------------------
问题已解决:在引用页面要使用<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"/>
显示结果:后余生
----------------解决方案--------------------------------------------------------