欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaBean四个作用域范围的详解

程序员文章站 2024-04-01 19:52:40
javabean四个作用域范围的详解 一 说明 使用usebeans的scope属性可以用来指定javabean的作用范围。  二 四个作用范围...

javabean四个作用域范围的详解

一 说明

使用usebeans的scope属性可以用来指定javabean的作用范围。

 二 四个作用范围

JavaBean四个作用域范围的详解
 

三 代码

1、login.jsp

<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8" %>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
  
  <title>my jsp 'login.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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
    -->
 
 </head>
 
 <body>
  <h1>系统登录</h1>
  <hr>
  <form name="loginform" action="dologin.jsp?mypass=999999" method="post">
   <table>
    <tr>
     <td>用户名:</td>
     <td><input type="text" name="username" value=""/></td>
    </tr>
    <tr>
     <td>密码:</td>
     <td><input type="password" name="password" value=""/></td>
    </tr>
    <tr>
     <td colspan="2" align="center"><input type="submit" value="登录"/></td>
     
    </tr>
   </table>
  </form>
 </body>
</html>

2、dologin.jsp

<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
  
  <title>my jsp 'dologin.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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
    -->
 
 </head>
 
 <body>
  <jsp:usebean id="myusers" class="com.po.users" scope="page"/>
  <h1>setproperty动作元素</h1>
  <hr>
  <!--根据表单自动匹配所有的属性 -->
  <%--
  <jsp:setproperty name="myusers" property="*"/> 
  --%>
  <!--根据表单匹配所有部分的属性 -->
  <%--
  <jsp:setproperty name="myusers" property="username"/> 
  --%>
  <!--根表单无关,通过手工赋值给属性 -->
  <%--
  <jsp:setproperty name="myusers" property="username" value="lisi"/>
  <jsp:setproperty name="myusers" property="password" value="888888"/>
  --%>
  <!--通过url传参数给属性赋值 -->
  <jsp:setproperty name="myusers" property="username"/>
  <jsp:setproperty name="myusers" property="password" param="mypass"/>
  <!-- 使用传统的表达式方式来获取用户名和密码 -->
  <%--  
    用户名:<%=myusers.getusername() %><br>
    密码:<%=myusers.getpassword() %><br>
  --%>
  <!-- 使用getproperty方式来获取用户名和密码 -->
   用户名:<jsp:getproperty name="myusers" property="username"/> <br>
   密码:<jsp:getproperty name="myusers" property="password"/><br>
  <br>
  <br>
 
   <a href="testscope.jsp" rel="external nofollow" >测试javabean的四个作用域范围</a>
 
   <%
     request.getrequestdispatcher("testscope.jsp").forward(request, response);
   %>
 
 </body>
</html>

3、testscope.jsp

<%@ page language="java" import="java.util.*"
    contenttype="text/html; charset=utf-8"%>
<%@ page import="com.po.users"%>
<%
    string path = request.getcontextpath();
    string basepath = request.getscheme() + "://"
            + request.getservername() + ":" + request.getserverport()
            + path + "/";
%>
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
 
<title>my jsp 'testscope.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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
    -->
 
</head>
 
<body>
    <h1>javabean的四个作用域范围</h1>
    <hr>
    <jsp:usebean id="myusers" class="com.po.users" scope="page" />
    用户名:<jsp:getproperty name="myusers" property="username" /><br> 密码:<jsp:getproperty
        name="myusers" property="password" /><br>
    <!-- 使用内置对象获取用户名和密码 -->
    <hr>
    <%--
    用户名:<%=((users)application.getattribute("myusers")).getusername()%><br>
    密码:<%=((users)application.getattribute("myusers")).getpassword() %><br>
  --%>
    <%--
    用户名:<%=((users)session.getattribute("myusers")).getusername()%><br>
    密码:<%=((users)session.getattribute("myusers")).getpassword() %><br>
  --%>
    <%--
    用户名:<%=((users)request.getattribute("myusers")).getusername()%><br>
    密码:<%=((users)request.getattribute("myusers")).getpassword() %><br>
  --%>
    <%
        string username = "";
        string password = "";
        if (pagecontext.getattribute("myusers") != null) {
            username = ((users) pagecontext.getattribute("myusers"))
                    .getusername();
            password = ((users) pagecontext.getattribute("myusers"))
                    .getpassword();
        }
    %>
 
    用户名:<%=username%><br> 密码:<%=password%><br>
 
 
 
</body>
</html>
 

四 测试结果

JavaBean四个作用域范围的详解

 如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!