java web 開發實戰經典(一)

來源:互聯網
上載者:User

標籤:

一、jsp三種Scriptlet(指令碼小程式)

1、<% %>  :定義局部變數、編寫語句等。

<%  String str = "hello world!";//定義局部變數  out.println(str);//編寫語句%>

 

2、<%! %>  :定義全域變數、方法和類。(雖然此方可以編寫類,但不建議使用。我們一般通過JavaBean的形式調用類)

<%!  public static final String INFO = "JAVA Web";//定義全域變數%><%!  public int add(int x, int y){//定義方法     return x+y;  }%><%!   class Person{//定義類   }%><%//編寫普通指令碼 out.println("INFO");//調用全域變數 out.println(add(3,5));//調用方法%>

 

3、<%= %> :輸一個變數或一個具體內容

<%   String str = "java";%><h1><%=str%><h1>//使用運算式輸出變數<h2><%="java web"%></h2>//使用運算式輸出常量

二、包含

1、靜態包含
<%@ include file="a.html" %>

<%@ include file="a.jsp" %>

2、動態包含

2.1不帶參數

<jsp:include page="a.html"/>

<jsp:include page="a.jsp"/>

2.2帶參數

<jsp:include  page="a.jsp">//向被包含頁面傳入兩個參數

<jsp:param name="name" value="<%=username%>"/>

<jsp:param name="info" value="java web"/>

</jsp:include>

 靜態是先包含再處理,動態是行處理後包含,所以我們應該多用動態包含。

例:a.jsp檔案

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><%@ page contentType="text/html;charset=GBK" pageEncoding="GBK" %><%  int i = 100;%><html>  <head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title>page</title>  </head>  <body class="tundra">   <h1>jsp-<%=i%></h1>  </body></html>

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><%@ page language="java" pageEncoding="GBK"%><%@ include file="a.jsp"%>//靜態包含<%   int i=10;%><html>  <head>    <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>    <title>My Web</title>      </head>  <body class="tundra">    <div><h1>jsp-<%=i%></h1></div>      </body></html>

如果用靜態載入會報錯,因為靜態是先合并再處理,當合并後就定義了兩個相同的i變數,因此顯示時報錯。

但如果是動態是則是先處理後合并,因此可以正常顯示兩條結果。

java web 開發實戰經典(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.