javaBean

第一种使用方法

  index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="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%>">
    
    <title>My JSP 'reg.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>
  <%     
      Users user = new Users() ;
      user.setUsername("zhang");
      user.setPassword("zzz111");
   %>
   <h1>普通创建javabean</h1>
    用户名:<br><%=user.getUsername() %>
    密码:<br><%=user.getPassword() %>
    
  </body>
</html>

users.java

package com.po;

public class Users{
     private String username ;
     private String password ;
     
     public Users(){}

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
     

}

 

 

 

 

 

 

第二种使用

动作标签的使用

 

useBeans的使用

<jsp:useBean id=”标示符” calss=”java类名字” scope=”作用范围”/>

 

setProperty

 

 

login.jsp

<body>
<h1>系统登录</h1>
    <hr>
    <form name="loginForm" action="dologin.jsp" 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>提交</td>
                <td colspan="2" align="center"><input type="submit" value="登录"/></td>
            </tr>
        </table>
    </form>
    
  </body>

dologin.jsp

<body>
      <jsp:useBean id="myUsers" class="com.po.Users" scope="page" />
    <h1>setProperty动作元素</h1>
    <hr>
    <!-- 第一 根据表单自动匹配所有的 属性-->
    <jsp:setProperty property="*" name="myUsers"/>
    用户名:<%=myUsers.getUsername() %><br>
    密码: <%=myUsers.getPassword() %><br>
  </body>

  

 

<body>
      <jsp:useBean id="myUsers" class="com.po.Users" scope="page" />
    <h1>setProperty动作元素</h1>
    <hr>

   
    <jsp:setProperty property="*" name="myUsers"/>
        <!-- 使用传统的表达式方式获取用户名密码
            用户名:<%=myUsers.getUsername() %><br>
        密码: <%=myUsers.getPassword() %><br>
        -->
        <!-- 使用getProperty方式获取 -->
        用户名:<jsp:getProperty name="myUsers" property="username"/>
        密码:<jsp:getProperty name="myUsers" property="password"/>
  
  </body>

 

 

posted @ 2016-04-13 14:10  式微胡不归  阅读(143)  评论(0编辑  收藏  举报