JSP动作

一、JSP动作简介

JSP动作元素(action elements),动作元素为请求处理阶段提供信息。动作元素遵循XML元素的语法,有一个包含元素名的开始标签,可以有属性,可选的内容,与开始标签匹配的结束标签。

JSP动作元素的分类:

1、第一类是与存取JavaBean有关的,包括:

<jsp:useBean>   <jsp:setProperty>   <jsp:getProperty>

2、第二类是JSP1.2就开始有的基础元素,包括6个动作元素:

<jsp:include>  <jsp:forward>   <jsp:param>   <jsp:plugin>  <jsp:params>   <jsp:fallback>

3、第三类是JSP2.0新增加的元素,主要与JSP Document有关  包括6个元素

<jsp:root>   <jsp:declarration>   <jsp:scriptlet>  <jsp:expression>  <jsp:text>  <jsp:output>

4、第四类是JSP2.0新增的动作元素,主要是用来动态生成XML元素标签的值,包含3个动作

<jsp:attribute>   <jsp:body>  <jsp:element>

5、第五类是JSP2.0新增的动作元素,主要用在TagFile中,有2个元素

<jsp:invoke>   <jsp:dobody>

二、四个基础动作元素

1、include动作

<jsp:include>动作元素表示在JSP文件被请求时包含一个静态的或者动态的文件。

(1)语法格式如下:

<jsp:include page="path" flush="true">

其中,page="path"表示相对路径,或者认为相对路径的表达式。flush="true" 表示缓冲区满时会被清空,一般使用flush为true,他默认值是false。

代码示例:

Date.jsp代码:

<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="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 'Date.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>
    <%
    Date d=new Date();
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd月  hh:mm:ss");
    String time=sdf.format(d);
     %>
     <%=time %>
  </body>
</html>

Include_Action.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="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 'Include_Action.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>
    <h1>include动作元素</h1>
    <hr>
    当前时间为:<jsp:include page="Date.jsp" flush="true"/>
  </body>
</html>

运行结果截图:
JSP动作

 (2)include指令与include动作元素比较

  include指令 jsp:include动作
语法格式 <%@include file="  "%>  <jsp:include page=" ..."
发生作用的时间 页面转换期间 请求期间
包含的内容 文件的实际内容 页面的输出
转换成的Servlet 主页面和包含页面转换为一个Servlet 主页面和包含页面转换为独立的Servlet(两个Servlet)
编译时间 较慢——资源必须被解析 较快
执行时间 稍快 较慢——每次资源必须被解析

2、forward动作

<jsp:forward>将客户端所发出来的请求,从一个JSP页面转交给另一个页面(可以是一个HTML文件,JSP文件,PHP文件,CGI文件,甚至是一个java程序段)。

(1)语法格式:

<jsp:forward page={"relativeURL"|"<%=expression%>"}/>

page属性包含的是一个相对的URL。page的值既可以直接给出,也可以在请求的时候动态计算。

<jsp:forward page="URL"/> 等同于<% request.getRequestDispatcher("URL").forward(request,response)  请求转发

代码示例:

index.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"
isErrorPage="true"  errorPage="error.jsp"
isThreadSafe="true"
session="true" buffer="8kb" autoFlush="true"  
info="..."
%>
<%
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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <body>
          <h1>用户注册</h1>
          <br>
          <form action="response.jsp" method="post">
              <table>
              <tr>
                 <td>用户名: </td>
                 <td><input type="text" name="username"/></td>
              </tr>
              <tr>
                 <td>爱好:</td>
                 <td>
                     <input type="checkbox" name="favorite" value="read"/>读书
                     <input type="checkbox" name="favorite" value="music"/>音乐
                     <input type="checkbox" name="favorite" value="movie"/>电影
                 </td>              
              </tr>
              <tr>
                  <td><input type="submit" value="提交"/></td>
              </tr>
              </table>    
          </form>
          <br>
          <!-- <a href="request.jsp?username=小帅">测试URL传参</a> -->
         
  </body>
</html>

response.jsp代码

<%@page import="java.io.PrintWriter"%>
<%@ page language="java" import="java.util.*" pageEncoding="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 'response.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>
    <h1>response内置对象</h1>
    <%
    response.setContentType("text/html; charset=utf-8");//设置响应的MIME类型
    //response.sendRedirect("request.jsp");//请求重定向
    //request.getRequestDispatcher("request.jsp").forward(request, response);//请求转发
     %>
     <!--forward动作元素转发 -->
     <jsp:forward page="request.jsp"/>
  </body>
</html>

request.jsp代码

<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" import="java.util.*" pageEncoding="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 'request.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>
   <h1>request内置对象</h1>
   <%
   request.setCharacterEncoding("utf-8");//解决中文乱码问题,但是解决不了url传递中的中文乱码问题
   out.println("<h2>原来的username为:"+request.getParameter("username")+"</h2>");
    %>
    
      爱好:<% if(request.getParameterValues("favorite")!=null)
      {
          String []favorites=request.getParameterValues("favorite");
          for(int i=0;i<favorites.length;i++)
          {
              out.println(favorites[i]+"&nbsp;&nbsp;");
          }
      }
     %>
     <br>
  </body>
</html>

运行结果截图

JSP动作

JSP动作

3、param动作

param动作元素用于传递参数。我们还可以使用<jsp:param>将当前JSP页面的一个或多个参数传递给所包含的或是跳转的JSP页面。该动作元素必须和<jsp:include>、<jsp:plugin>/<jsp:forward>动作一起使用。

(1)param和<jsp:include>动作一起使用的语法如下:

<jsp:include page="相对的URL值"|"<%=表达式%>" flush="true">

<jsp:param name="参数名1" value="{参数值|<%=表达式%>}"/>

<jsp:param name="参数名1" value="{参数值|<%=表达式%>}"/>

</jsp:include>

(2)param和<jsp:forward >动作一起使用的语法如下:

<jsp:forward page="path"}>

<jsp:param name="paramname" value="paramvalue"/>

</jsp:forward>

代码示例:

index.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"
isErrorPage="true"  errorPage="error.jsp"
isThreadSafe="true"
session="true" buffer="8kb" autoFlush="true"  
info="..."
%>
<%
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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <body>
          <h1>用户注册</h1>
          <br>
          <form action="response.jsp" method="post">
              <table>
              <tr>
                 <td>用户名: </td>
                 <td><input type="text" name="username"/></td>
              </tr>
              <tr>
                 <td>爱好:</td>
                 <td>
                     <input type="checkbox" name="favorite" value="read"/>读书
                     <input type="checkbox" name="favorite" value="music"/>音乐
                     <input type="checkbox" name="favorite" value="movie"/>电影
                 </td>              
              </tr>
              <tr>
                  <td><input type="submit" value="提交"/></td>
              </tr>
              </table>    
          </form>
          <br>
          <!-- <a href="request.jsp?username=小帅">测试URL传参</a> -->
         
  </body>
</html>

response.jsp代码

<%@page import="java.io.PrintWriter"%>
<%@page import="java.net.*" %>
<%@ page language="java" import="java.util.*" pageEncoding="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 'response.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>
    <h1>response内置对象</h1>
    <%
    response.setContentType("text/html; charset=utf-8");//设置响应的MIME类型
    //response.sendRedirect("request.jsp");//请求重定向
    //request.getRequestDispatcher("request.jsp").forward(request, response);//请求转发
     %>
     <!--forward动作元素转发 -->
     <jsp:forward page="request.jsp">
     <jsp:param value="girl" name="gender"/>
     </jsp:forward>
  </body>
</html>

request.jsp代码

<%@page import="javax.xml.crypto.URIDereferencer"%>
<%@page import="java.net.*" %>
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" import="java.util.*" pageEncoding="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 'request.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>
   <h1>request内置对象</h1>
   <%
   request.setCharacterEncoding("utf-8");//解决中文乱码问题,但是解决不了url传递中的中文乱码问题
   out.println("<h2>原来的username为:"+request.getParameter("username")+"</h2>");
    %>
    性别:<%=request.getParameter("gender") %><br>
      爱好:<% if(request.getParameterValues("favorite")!=null)
      {
          String []favorites=request.getParameterValues("favorite");
          for(int i=0;i<favorites.length;i++)
          {
              out.println(favorites[i]+"&nbsp;&nbsp;");
          }
      }
     %>
     <br>
  </body>
</html>

运行结果截图:

JSP动作

JSP动作

4、plugin动作元素

 jsp:plugin动作用来根据浏览器的类型,通过Java插件运行Java Applet所必须的OBJECT或EMBED元素。

语法格式:

<jsp:plugin

type="bean|applet"

code="classFileName"

[name="instanceName"]

[align="bottom|top|middle|left|right"]

[height="displsyPixels"]

[width="displsyPixels"]

[hspace="leftRightPixels"]

[vspace="topButtomPixels"]

[jreversion="java版本"]

[<jsp:params>

[<jsp:param name="parameterName" value="参数的值"/]

</jsp:params>]

[<jsp:fallback> 这里是在不能启动插件的时候,显示给用户的文本信息</jsp:fallback>]

</jsp:plugin>

Plugin中的各个属性:

type="bean|applet":插件将执行的对象的类型,必须指定。

code="classFileName":插件将执行的java类文件的名称,在名称中必须包含扩展名,且此文件必须用"codebase"属性指明的目录下。

codebase="classFileDirectoryName":包含插件将运行的java类的目录或指相对这个目录的路径。

三、与JavaBean存取有关的动作元素

<jsp:useBean>   <jsp:setProperty>   <jsp:getProperty>

1、JavaBeans

(1)JavaBean简介

JavaBeans就是符合某种特定规范的Java类。使用JavaBeans的好处是解决了代码的重复编写,减少代码冗余,功能的区分明确,提高了代码的维护性。

(2)JavaBeans的设计原则

1)公有类(public修饰)

2)无参的公有构造函数

3)属性私有

4)getter和setter方法封装所有的属性。

(3)普通方式应用JavaBeans

 1 package Beans;
 2 /*
 3  *Students类的JavaBean
 4  */
 5 public class Students {
 6 
 7     private String name;
 8     private String gender;
 9     private String age;
10     
11     public Students(){
12         
13     }
14 
15     public Students(String name, String gender, String age) {
16         super();
17         this.name = name;
18         this.gender = gender;
19         this.age = age;
20     }
21 
22     public String getName() {
23         return name;
24     }
25 
26     public void setName(String name) {
27         this.name = name;
28     }
29 
30     public String getGender() {
31         return gender;
32     }
33 
34     public void setGender(String gender) {
35         this.gender = gender;
36     }
37 
38     public String getAge() {
39         return age;
40     }
41 
42     public void setAge(String age) {
43         this.age = age;
44     }
45     
46 }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="Beans.*" %>
<%
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 'TestJavaBean1.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>
   <h1>普通方式测试应用JavaBeans</h1>
   <%
   Students student=new Students("小帅哥","男","18");
   Students student1=new Students();
   student1.setName("女神");
   student1.setGender("女");
   student1.setAge("16");
    %>
    <h2>学生1的信息:</h2>
    姓名:<%=student.getName() %><br>
    性别:<%=student.getGender() %><br>
    年龄:<%=student.getAge() %><br>
    <h2>学生2的信息:</h2>
     姓名:<%=student1.getName() %><br>
     性别:<%=student1.getGender() %><br>
     年龄:<%=student1.getAge() %><br>
  </body>
</html>

运行结果截图:

JSP动作

2、三种存取JavaBean的动作元素

(1)<jsp:useBeans>

用来查找或实例化一个JSP页面,或在指定范围内使用JavaBean

<jsp:useBean />

注意以下几点:

1)id用来引用Bean实例的变量。如果能够找到id和scope相同的Bean实例,jsp:useBean动作将使用已有的Bean实例,而不是创建新的实例。

2)class指定Bean的完整包名,表明bean具体是对哪个类的实例化。

3)scope指定Bean的有效范围,可取四个值分别为:page、request、session和application。默认值为page。

4)type指定引用该对象的变量的类型,它必须是Bean类的名字,超类名字、该类所实现的接口名字之一。请记住变量的名字是由id属性指定的

5)beanName指定Bean的名字。如果提供了type属性和beanName属性,允许省略class属性。

个人觉得:scope属性是JavaBean解决了代码的重复编写,减少代码冗余,功能的区分明确,提高了代码的维护性的重要原因。

(2)<jsp:setProperty>

表示用来设置bean中的属性值。在JSP表达式或在Scriptlet中读取Bean属性通过调用相应的getXXX()方法事项的,或者更一般地,使用jsp:getProperty动作。

两种语法实现方式:

1)在jsp:usebea后使用jsp:setproperty:

<jsp:usebean />

<jsp:setproperty name="user" property="user"/>

在这种方式中,jsp:serproperty将被执行。

2)jsp:setproperty出现在jsp:usebean标签内:

<jsp:usebean >

<jsp:setproperty name="user" property="user"/>

</jsp:usebean>

在这种方式中,jsp:setproperty只会在新的对象被实例化是才将被执行。

注意以下几点:

1)name用来表明对哪个bean实例执行下面的动作,这个动作和动作<jsp:useBean>中定义的id必须对应起来,包括大小写必须一致。这个属性是必需的。

2)property用来表示要设置哪个属性。如果property的值是"*",表示用户在可见的JSP页面中输入的全部值,存储在匹配的bean属性中。匹配的方法是:bean的属性名称必须与输入框的名字相同。property属性是必须的。它表示要设置哪个属性。有一个特殊用法:如果property的值是“*”,表示所有的名字和Bean属性名字匹配的请求参数都将被传递给相应的属性set()方法,这个属性是必须的。

3)value属性是可选的,该属性用来指定Bean属性的值。

4)param属性是可选的,它指定用哪个请求参数作为Bean属性的值。如果当前请求没有参数,则什么事情也不做,系统不会把null传递给Bean属性的set方法。因此你可以让Bean自己提供默认属性值,只有当请求参数明确指定了新值是才会修改默认属性值。

代码示例:(四种不同的赋值方式)

Registr.jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="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 'Registr.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>
    <h1>请完善下面信息:</h1>
    <form action="DoRegistr.jsp?mypassword=122345" method="post">
    <table>
         <tr>
             <td>姓名:</td>
             <td><input name="name" type="text"></td>
         </tr>
         <tr>
             <td>性别:</td>
             <td><input name="gender" type="text"></td>
         </tr>
         <tr>
             <td>年龄:</td>
             <td><input name="age" type="text"></td>
         </tr>
         <tr>
             <td><input type="submit" value="提交"></td>
         </tr>
    </table>
    </form>
    
  </body>
</html>

DoRegistr.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="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 'DoRegistr.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>
  <%request.setCharacterEncoding("utf-8"); %>
  <!--第一种:根据表单自动匹配所有的属性 ,自动赋值 -->
    <jsp:useBean id="student" class="Beans.Students" scope="session"/>
    <jsp:setProperty property="*" name="student"/>
    
  <!--第二种:根据表单匹配部分属性,自动赋值  -->
  <jsp:useBean id="student1" class="Beans.Students" scope="session"/>
  <jsp:setProperty property="name" name="student1"/>
  <jsp:setProperty property="gender" name="student1"/>
  <jsp:setProperty property="age" name="student1"/>
  
  <!--第三种和表单无关,通过手工赋值给属性  -->
  <jsp:useBean id="student2" class="Beans.Students" scope="session"/>
  <jsp:setProperty property="name" name="student2" value="女神"/>
  <jsp:setProperty property="gender" name="student2" value="女"/>
  <jsp:setProperty property="age" name="student2" value="16"/>
  
  <!-- 第四种:通过URL传参给属性赋值: -->
  <jsp:setProperty property="password" name="student" param="mypassword"/>
  <jsp:setProperty property="password" name="student1" param="mypassword"/>
  <jsp:setProperty property="password" name="student2" param="mypassword"/>
  
  <!-- 遍历赋值结果: -->
  <h3>第一种赋值方式+第四种赋值方式的运行结果:</h3>
  <%
  out.println("姓名:"+student.getName()+"<br>性别:"+student.getGender()+"<br>年龄:"+student.getAge()+"<br>");
   %>
   
   <h3>第二种赋值方式+第四种赋值方式的运行结果:</h3>
  <%
  out.println("姓名:"+student1.getName()+"<br>性别:"+student1.getGender()+"<br>年龄:"+student1.getAge()+"<br>");
   %>
   
   <h3>第三种赋值方式+第四种赋值方式的运行结果:</h3>
  <%
  out.println("姓名:"+student2.getName()+"<br>性别:"+student2.getGender()+"<br>年龄:"+student2.getAge()+"<br>");
   %>
  </body>
</html>

运行结果截图:

JSP动作

JSP动作

(3)<jsp:getProperty>

<jsp:getproperty>标签标示获取了bean的属性值并将之转化为一个字符串,然后将其插入在输出的页面中。该动作实际是调用了bean的get()方法。

在使用<jsp:getproperty>之前,必须用<jsp:usebean>来创建它。不能使用<jsp:getproperty>来检索一个已经被索引了的属性。语法格式如下:

<jsp:getProperty name="beanInstanceName" property="propertyName"/>

jsp:getProperty有两个必须的属性:name表示Bean的名字;property表示要提取哪个属性的值。

<jsp:useBean ../>

<UL>

<LI>Number od items:

<jsp:getProperty name="itemBean" property="numIteams"/>

<LI>Cost of each:

<jsp:getProperty name="itemBean" property="unitCost"/>

</UL>

代码示例:

Registr.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="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 'Registr.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>
    <h1>请完善下面信息:</h1>
    <form action="DoRegistr.jsp?mypassword=122345" method="post">
    <table>
         <tr>
             <td>姓名:</td>
             <td><input name="name" type="text"></td>
         </tr>
         <tr>
             <td>性别:</td>
             <td><input name="gender" type="text"></td>
         </tr>
         <tr>
             <td>年龄:</td>
             <td><input name="age" type="text"></td>
         </tr>
         <tr>
             <td><input type="submit" value="提交"></td>
         </tr>
    </table>
    </form>
    
  </body>
</html>

DoRegistr.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="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 'DoRegistr.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>
  <%request.setCharacterEncoding("utf-8"); %>
  <!--第一种:根据表单自动匹配所有的属性 ,自动赋值 -->
    <jsp:useBean id="student" class="Beans.Students" scope="session"/>
    <jsp:setProperty property="*" name="student"/>
    
 <!--请求转发的动作元素: -->

<jsp:forward page="getRegistrInfo.jsp"/> </body> </html>

getRegistrInfo.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="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 'getRegistrInfo.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>
    <h1>这是通过getProperty动作元素获得javaBean信息的页面</h1>
        <jsp:useBean id="student" class="Beans.Students" scope="session"/>
       姓名:<jsp:getProperty property="name" name="student"/><br>
       性别:<jsp:getProperty property="gender" name="student"/><br>
       年龄:<jsp:getProperty property="age" name="student"/><br>
       密码:<jsp:getProperty property="password" name="student"/><br>
      
  </body>
</html>

运行结果截图:

JSP动作

JSP动作

注意:当设定Bean对象要在所有页面都可以使用,就需要在使用jsp:useBean动作时,标注好其scope值,相同name,相同class,相同scope的Bean对象是同一个对象,而且不是覆盖,而是直接使用第一个。