一、Java Bean行文
1.重点说明
Java Bean行为是一组与Java Bean相关的行为,包括useBean行为、setProperty行为、getProperty行为等。Java Bean就是普通的Java类。Java Bean类非常简单,只有私有属性与对应的getter方法和setter方法。(注意:对于boolean类型的属性,习惯上一般把getter方法写成isXxx(),而不是getXxx())
useBean行为属性 属性名 | 取值范围 | 描述 |
id | 合法的Java变量名 | 指明Java Bean对象的名称。 JSP中可以使用该名称引用该Java Bean对象。 |
class | Java Bean类的全名 | Java Bean类的全名。 |
scope | page, request, session, application | 该Java Bean对象的范围。 当为page时,只在该JSP内有效。 当为request时,只在当前的request中有效。 当为session时,对当前用户有效。 当为application时,当前Web应用程序内有效。默认为page。 |
2.代码实践
scope实现各种计数器
Counter.java
1 package com.bean; 2 3 public class Counter { 4 5 private int count; //计数器 6 7 public int getCount(){ //每访问一次,计数器自加1 8 return ++count; 9 }10 public void setCount(int count){11 this.count = count;12 }13 }
Counter.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head>10 <base href="<%=basePath%>">11 12 <title>My JSP 'Counter.jsp' starting page</title>13 14 <meta http-equiv="pragma" content="no-cache">15 <meta http-equiv="cache-control" content="no-cache">16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">18 <meta http-equiv="description" content="This is my page">19 <!--20 <link rel="stylesheet" type="text/css" href="styles.css">21 -->22 23 </head>24 25 <body>26 <!-- 定义一个session范围内的计数器,记录个人访问信息 -->27 <jsp:useBean id="personCount" class="com.bean.Counter" scope="session" />28 <!-- 定义一个application范围内的计数器,记录所有人的反问信息 -->29 <jsp:useBean id="totalCount" class="com.bean.Counter" scope="application" />30 <div align="center">31 <form action="page/Counter.jsp" method="get">32 <fieldset style="width:300">33 <legend>计数器</legend>34 <table align="center" width="400">35 <tr>36 <td>您的访问次数:37 </td>38 <td>39 <!-- 获取个人的访问次数 -->40 <jsp:getProperty property="count" name="personCount"/>41 </td>42 </tr>43 <tr>44 <td>总共的访问次数:45 </td>46 <td>47 <!-- 获取所有人的访问次数 -->48 <jsp:getProperty property="count" name="totalCount"/>49 </td>50 </tr>51 <tr>52 <td colspan="2">53 <input type="submit" value="刷新">54 </td>55 </tr>56 </table>57 </fieldset>58 </form>59 </div>60 </body>61 </html>
3.效果截图
二、<jsp:plugin/>嵌入Applet
1.重点说明
Java Applet是运行在客户端浏览器里的Java小程序。JSP提供了一组plugin行为简化嵌入Applet的操作。plugin行为包括<jsp:plugin/><jsp:params/><jsp:param/><jsp:fallback/>。其中<jsp:plugin/>用于在JSP中定义Java Applet,并声明Applet的宽度和高度等属性;<jsp:params/>用于定义一组一对多的参数;<jsp:param/>用于定义单个的参数;<jsp:fallback/>用于定义不支持Applet时的替代信息。
2.程序实践
Plugin.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head>10 <base href="<%=basePath%>">11 12 <title>My JSP 'Plugin.jsp' starting page</title>13 14 <meta http-equiv="pragma" content="no-cache">15 <meta http-equiv="cache-control" content="no-cache">16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">18 <meta http-equiv="description" content="This is my page">19 <!--20 <link rel="stylesheet" type="text/css" href="styles.css">21 -->22 23 </head>24 25 <body>26 <table align="center" bgcolor="#999999" cellpadding="1">27 <tr>28 <td bgcolor="#FFFFFF">29 <jsp:plugin 30 code="Graph.class" 31 codebase="http://java.sun.com/applets/jdk/1.4/demo/applets/GraphLayout/" 32 type="applet" width="500" height="400">33 <jsp:params>34 <jsp:param value="joe-food,joe-dog,joe-tea,table-plate/50"35 name="edges"/>36 </jsp:params>37 <jsp:fallback>您的浏览器不支持Java Applet</jsp:fallback>38 </jsp:plugin>39 40 </td>41 </tr>42 </table>43 </body>44 </html>
3.效果截图
这里不知道怎么被阻止了。
三、<jsp:directive/>行为
<jsp:directice/>行为相当于JSP指令。<jsp:directive.page/>行为相当于<%@ page%>指令,<jsp:directive.include/>行为相当于<%@ include%>指令,<jsp:directive.taglib/>行为相当于<%@ taglib%>指令。
注意:现在的Java Web开发平台更倾向于使用directive行为而不使用JSP指令。这是因为<jsp:directive/>行为比JSP指令更整洁,可读性更好。
原标题:Java开发之JSP行为
关键词:JS