useBean动作元素

阅读更多
作用:在jsp页面中实例化或者在指定范围内使用javabean:
 
二 代码
1、Users.java
package com.po;
/*
 * 用户类
 * */
public class Users {
 
         private String username ; //用户名
         private String password ; //密码
        
         //保留此默认的构造方法
         public Users()
        {
                
        }
 
         public String getUsername() {
                 return username ;
        }
 
         public void setUsername(String username) {
                 this . username = username;
        }
 
         public String getPassword() {
                 return password ;
        }
 
         public void setPassword(String password) {
                 this . password = password;
        }
        
        
}
2、useBean.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 %> " >
   
    < 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" >
        
  head >
 
  < body >
    < jsp:useBean id = "myUsers" class = "com.po.Users" scope = "page" />
    < h1 > 使用useBean动作创建 javabean 的实例 h1 >
    < hr >
       用户名: <%= myUsers.getUsername() %> < br >
       密码: <%= myUsers.getPassword() %> < br >
  body >
html >
 
三 测试

 


useBean动作元素_第1张图片
 
  • useBean动作元素_第2张图片
  • 大小: 33.8 KB
  • 查看图片附件

你可能感兴趣的:(useBean)