Java開發之JSP行為,java開發jsp

來源:互聯網
上載者:User

Java開發之JSP行為,java開發jsp

一、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指令更整潔,可讀性更好。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.