JavaBean四个作用域范围的详解

 更新时间:2017年10月12日 10:09:39   作者:cakin24  
这篇文章主要介绍了JavaBean四个作用域范围的详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

JavaBean四个作用域范围的详解

一 说明

使用useBeans的scope属性可以用来指定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>
 

四 测试结果

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

相关文章

  • 深入浅出MyBatis中映射文件和实体类的关联性

    深入浅出MyBatis中映射文件和实体类的关联性

    这篇文章主要介绍了MyBatis中映射文件和实体类的关联性的相关知识,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Java运行时动态生成类实现过程详解

    Java运行时动态生成类实现过程详解

    这篇文章主要介绍了Java运行时动态生成类实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Java多线程Semaphore工具的使用详解

    Java多线程Semaphore工具的使用详解

    Semaphore 是一种用于控制线程并发访问数的同步工具。它通过维护一定数量的许可证来限制对共享资源的访问,许可证的数量就是可以同时访问共享资源的线程数目,需要的朋友可以参考下
    2023-05-05
  • java中的编码转换过程(以utf8和gbk为例)

    java中的编码转换过程(以utf8和gbk为例)

    这篇文章主要介绍了java中的编码转换过程(以utf8和gbk为例),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • spring data jpa如何只查询实体部分字段

    spring data jpa如何只查询实体部分字段

    这篇文章主要介绍了spring data jpa如何只查询实体部分字段的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • 微服务链路追踪Spring Cloud Sleuth整合Zipkin解析

    微服务链路追踪Spring Cloud Sleuth整合Zipkin解析

    这篇文章主要为大家介绍了微服务链路追踪Spring Cloud Sleuth整合Zipkin解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • 关于@ComponentScan TypeFilter自定义指定扫描bean的规则

    关于@ComponentScan TypeFilter自定义指定扫描bean的规则

    这篇文章主要介绍了关于@ComponentScan TypeFilter自定义指定扫描bean的规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • SpringBoot创建自定义Starter代码实例

    SpringBoot创建自定义Starter代码实例

    这篇文章主要介绍了SpringBoot创建自定义Starter代码实例,自定义 Starter 是一种在软件开发中常用的技术,它可以帮助开发者快速搭建项目的基础框架和配置,可以将一些常用的功能、依赖和配置封装成一个可复用的模块,方便在不同的项目中使用,需要的朋友可以参考下
    2023-11-11
  • Java接口和抽象类实例分析

    Java接口和抽象类实例分析

    这篇文章主要介绍了Java接口和抽象类,实例分析了java接口与抽象类的概念与相关使用技巧,需要的朋友可以参考下
    2015-05-05
  • Java多线程中的Future类详细解读

    Java多线程中的Future类详细解读

    这篇文章主要介绍了Java多线程中的Future类详细解读,Future表示一个可能还没有完成的异步任务的结果,针对这个结果可以添加Callback以便在任务执行成功或失败后作出相应的操作,需要的朋友可以参考下
    2023-11-11

最新评论