---
title: "JSP的getProperty"
author: "cakin24"
date: "2017-10-05T07:31:27Z"
tags: []
categories: ["编程语言"]
slug: "jsp-getproperty"
draft: false
lastmod: "2017-10-05T07:31:27Z"
id: 2395355
url: "https://cakin24.iteye.com/blog/2395355"
---

一 作用

获取指定Javabean对象的属性值。

 

二 语法

&lt;jsp:getProperty name="Javabean实例名" property="属性名" /&gt;

 

三 代码

1、login.jsp

&lt;%@ page language="java" import="java.util.\*" contentType="text/html; charset=utf-8" %&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'login.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

    &lt;h1&gt;系统登录&lt;/h1&gt;

    &lt;hr&gt;

    &lt;form name="loginForm" action="dologin.jsp?mypass=999999" method="post"&gt;

      &lt;table&gt;

        &lt;tr&gt;

          &lt;td&gt;用户名：&lt;/td&gt;

          &lt;td&gt;&lt;input type="text" name="username" value=""/&gt;&lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;

          &lt;td&gt;密码：&lt;/td&gt;

          &lt;td&gt;&lt;input type="password" name="password" value=""/&gt;&lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;

          &lt;td colspan="2" align="center"&gt;&lt;input type="submit" value="登录"/&gt;&lt;/td&gt;

         

        &lt;/tr&gt;

      &lt;/table&gt;

    &lt;/form&gt;

  &lt;/body&gt;

&lt;/html&gt;

2、dologin.jsp

&lt;%@ page language="java" import="java.util.\*" contentType="text/html; charset=utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'dologin.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

    &lt;jsp:useBean id="myUsers" class="com.po.Users" scope="page"/&gt;

    &lt;h1&gt;setProperty动作元素&lt;/h1&gt;

    &lt;hr&gt;

   &lt;!--根据表单自动匹配所有的属性 --&gt;

   &lt;%--

   &lt;jsp:setProperty name="myUsers" property="\*"/&gt; 

   --%&gt;

   &lt;!--根据表单匹配所有部分的属性 --&gt;

   &lt;%--

   &lt;jsp:setProperty name="myUsers" property="username"/&gt; 

   --%&gt;

   &lt;!--根表单无关，通过手工赋值给属性 --&gt;

   &lt;%--

   &lt;jsp:setProperty name="myUsers" property="username" value="lisi"/&gt;

   &lt;jsp:setProperty name="myUsers" property="password" value="888888"/&gt;

   --%&gt;

   &lt;!--通过URL传参数给属性赋值 --&gt;

   &lt;jsp:setProperty name="myUsers" property="username"/&gt;

   &lt;jsp:setProperty name="myUsers" property="password" param="mypass"/&gt;

   &lt;!-- 使用传统的表达式方式来获取用户名和密码 --&gt;

   &lt;%--    

       用户名：&lt;%=myUsers.getUsername() %&gt;&lt;br&gt;

       密码：&lt;%=myUsers.getPassword() %&gt;&lt;br&gt;

   --%&gt;

   &lt;!-- 使用getProperty方式来获取用户名和密码 --&gt;

      用户名：&lt;jsp:getProperty name="myUsers" property="username"/&gt; &lt;br&gt;

      密码：&lt;jsp:getProperty name="myUsers" property="password"/&gt;&lt;br&gt;

   &lt;br&gt;

   &lt;br&gt;

  &lt;/body&gt;

&lt;/html&gt;

 

四 运行结果

![](http://dl2.iteye.com/upload/attachment/0127/2422/255c4091-6cc2-38ce-87b0-724f80543959.png)