Upload
aneko
View
22
Download
0
Embed Size (px)
DESCRIPTION
JSP 簡介. 1 第一個 JSP 程式 2 JSP 的基本語法 3 字串的運算. 1 第一個 JSP 程式. Hello World. hello word . 1 第一個 JSP 程式. 您好. hello word
Citation preview
JSP 簡介1 第一個 JSP 程式2 JSP 的基本語法3 字串的運算
1 第一個 JSP 程式
• Hello World
<html><head><title>hello word</title></head><body><% out.write("Hello World!");%></body></html>
1 第一個 JSP 程式
• 您好<html><head><title>hello word</title></head><body><% out.write(“ 您好 !");%></body></html>
1 第一個 JSP 程式
• 您好<%@ page contentType="text/html; charset=Big5" %><html><head><title>hello word</title></head><body><% out.write(“ 您好 !");%></body></html>
2 JSP 的基本語法
記錄國文成績
記錄英文成績
加總
跟記憶體要一塊空間用來存放國文成績用一個變數表示這個空間
記憶體
Chinese
88
2 JSP 的基本語法
2-1 JSP 的定值與變數
資料型別 整數 (integer)
浮點數 (floating)
倍精度 (double)
字元 (character)
字串 (string)
布林 (boolean)
2-1-2 整數類
整數有四類 byte 字元組 int 整數 short 短整數 long 長整數
數值長度與範圍 byte 大小為 1byte, 範圍 -128~127
int 大小為 2bytes, 範圍 -32768~32767
short 大小為 4bytes, 範圍 -2147483648~2147483647
long 大小為 8bytes, 範圍 -9233372036854775808~9233372036854775807
記憶體
Chinese88
-2(K-1)~2(K-1)-1
2-1 JSP 的定值與變數
所有使用的變數都要宣告 變數是一個存放數值的空間 變數命名原則
大小寫會區別 不可用保留字 (for 、 if 、 while …)
變數必須是字母開頭或是底線“ _” 或是 “ $“ 作為開頭
開頭字母有後可以使用數字與字元來命名 命名要用英文字母與相關符號
記憶體
Chinese88
2-1 JSP 的定值與變數
變數範例 Taxi_ii
int_NewType
$MyName
_hello_world
變數宣告方法變數型態關鍵字 識別字 (= 初值 );
變數型態關鍵字 識別字 1 (= 初值 1), 識別字 2 (= 初值 2),
…..;
■ int
<%@page contentType="text/html; charset=Big5" %><html><head><title> 四則運算 </title></head><body><% int Chinese=88, English=91; int Math=76, JSP=99; %>國文成績 <% out.write(Chinese+" 分 "); %> <br>英文成績 <% out.write(English+" 分 "); %> <br>數學成績 <%=Math +" 分 " %> <br>計概成績 <%=JSP+" 分 " %> <br><hr>總成績 <%= Chinese + English + Math + JSP %></body></html>
記憶體
Chinese
88
English
91
Math
76JSP
99
■ int
<%@page contentType="text/html;charset=Big5" %><html><head><title> 四則運算 </title></head><body><% int Chinese=50,English=91,Math=80,JSP=95; int total = Chinese + English + Math + JSP; %>國文成績 <%=Chinese %> <br>英文成績 <%=English %> <br>數學成績 <%=Math %> <br>JSP 成績 <%=JSP %> <br><hr>總成績 <%= total %> <br>平 均 <%= total/4 %></body></html>
記憶體
Chinese
88
English
91
Math
76JSP
99
total
■ double
<%@page contentType="text/html;charset=Big5" %><html><head><title> 四則運算 </title></head><body><% int Chinese=50,English=91,Math=80,JSP=95; double total = Chinese +English +Math +JSP; %>國文成績 <%=Chinese %> <br>英文成績 <%=English %> <br>數學成績 <%=Math %> <br>JSP 成績 <%=JSP %> <br><hr>總成績 <%= total %> <br>平 均 <%= total/4.0 %></body></html> 記憶體
Chinese
88
English
91
Math
76JSP
99
total
■ double
• double
<%@ page contentType="text/html; charset=Big5" %><html><head><title> double 型態 </title></head><body><% double PI = 3.14159265358979323d; double r = 50; double circle = 2 * PI * r;
%>圓的周長為 <%= circle %> 公尺</body></html>
2-1-4 字元類• char 宣告
- char 識別字 (= 初值 );
- char 識別字 1 (= 初值 1), 識別字 2 (= 初值 2),..;
<%@page contentType="text/html;charset=Big5" %><html><head><title> 字元 </title></head><body><% char ch1,ch2; ch1 = 65; ch2 = 'A';%>第一個字元 <%= ch1%> <br> 第二個字元 <%= ch2%></body></html>
2-1-5 布林類• boolean
- boolean 識別字 (= 初值 );
- boolean 識別字 1 (= 初值 1), 識別字 2 (= 初值 2),..;
<%@page contentType="text/html;charset=Big5" %><html><head><title> 布林值 </title></head><body><% boolean T = 11 > 10; boolean F = false;%>11 > 10 為 <%= T %>, <br>F 的內容為 <%= F %>, <br>11 > 12 為 <%= (11>12) %></body></html>
2-1-6 字串• String
<%@page contentType="text/html;charset=Big5" %><html><head><title> 字串 </title></head><body>
字串 <br><% String S1 = "Hi. My name is Ann."; String S2 = "How are you today?"; String S3 = S1 + " " + S2 ; out.write(S3);%></body></html>
int X = 100; int Y =
X
100
記憶體
2-1-7 陣列
陣列是可以存放多個變數的序列資料結構 透過註標 (index) 來指定特定變數的位置 ( 由 0 開始 )
• Array 宣告 ( 一維 )
資料型態 [];
識別字 = new 資料型態 [ 陣列大小 n];
或是
資料型態 識別字 []={ 初值 1, 初值 2, 初值 3…}
• 範例十一 ( 一維陣列 )
A[0]
A[1]
A[2]
A[3]
A[4]
A[5]
<%@page contentType="text/html;charset=Big5" %><html><head><title> 陣列的使用 </title></head><body>五個學生的成績分別是<%int JSP_score[]={88,92,87,77,53};int i;float average=0,total=0;for(i=0;i<=4;i++){ out.write(JSP_score[i] + " "); total = total + JSP_score[i];} average = total/5;%><hr>總分為 <%=total %> 平均分為 <%= average %></body></html>
■二維陣列到多為陣列
二維陣列最多可以放置 M( 列數 ) x N( 欄數 ),N 值可以是不固定 .
• Array 宣告 ( 二維 )
資料型態 [][];
識別字 = new 資料型態 [ 陣列一維大小 M] [ 陣列二維大小 N];
或是
資料型態 識別字 []={{ 初值 11, 初值 12, 初值 13…, 初值 1N},
{ 初值 21, 初值 22, 初值 23…, 初值 2N},
{ 初值 31, 初值 32, 初值 33…, 初值 3N},
……………………………………
{ 初值 M1, 初值 M2, 初值 M3…, 初值 MN}}
• 範例十二 ( 二維陣列固定 N 值 )
<%@page contentType="text/html;charset=Big5" %><html><head><title> 二維陣列的使用 </title></head><body><% int score[][]={{88,76,98}, // 第一個學生的成績 {92,89,82}, // 第二個學生的成績 {87,68,75}, // 第三個學生的成績 {77,84,83}, // 第四個學生的成績 {53,54,66}}; // 第五個學生的成績int i,j;String subject[] = {"JSP","English","Math"};float average[]={0,0,0},total[]={0,0,0}; %><table border="1"><tr><td> 科目 / 學生 <td> 1 <td> 2 <td> 3 <td> 4 <td> 5 <td> 總分 <td> 平均 </tr><% for(j=0;j<=2;j++){ out.write("<tr><td>" + subject[j]); for(i=0;i<=4;i++) { out.write("<td>" + score[i][j] ); total[j] = total[j] + score[i][j]; } average[j] = total[j]/5; out.write("<td>" + total[j] + "<td>" + average[j]); out.write("</tr>"); } %></table></body></html>
■ 二維陣列 N 值不固定
<html><head><title>JSP Code Example</title></head><body><%int test[][] = new int[2][];test[0] = new int[3];test[1] = new int[5];int i,j;for(i=0;i<=1;i++){ for(j=0;j<=2+i*2;j++) { test[i][j] = i + j; out.write(test[i][j] + " "); } out.write("<br>");}%></body></html>
2-1-8 型態轉換
自動型態轉換的規則 所有運算的資料型態都相同可以進行運算 在同一大類的型態運算中運算結果的資料長度大於運算資料
資料型態則可以進行運算 跟字串進行運算的其他型態會先轉換成字串
見下面範例十四
<%@page contentType="text/html;charset=Big5" %><html><head><title> 自動型態轉換 </title></head><body><%byte a=66;float b=212.1f;double c;String d="Type Conversion Test ";short e = 188;int f;c = a + b;d = d + c;f = a + e;%>byte 型態整數 <%=a %> <br>float 型態數字 <%=b %> <br>short 型態整數 <%=e %> <br>byte 與 float 相加結果 <%=c %> <br>byte 與 short 相加結果 <%=f %> <br>字串與 double 型態相加結果 <%=d %></body></html>
■不相容型態轉換
不相容型態轉換宣告方式( 欲轉換的資料型態 ) 變數或數值 ;
範例十五<%@page contentType="text/html;charset=Big5" %><html><head><title> 不相容型態轉換 </title></head><body><% int a = 121 , b= 102345;short c,d;double f=12.96567d;double i=12.9656789123456789d;float g,h;c = (short) a;d = (short) b;g = (float) f;h = (float) i; %>把 int 型態 121 換成 short 型態後數值為 <%= c %> <br>把 int 型態 102345 換成 short 型態後數值為 <%= d %> <br>把 double 型態 12.96567 換成 float 型態後數值為 <%= g %> <br>把 double 型態 12.9656789123456789 換成 float 型態後數值為 <%= i %> <br></body></html>
2-2 運算式與運算子
2-2-1 運算式 定義 : 運算式 (expression) 是由字義、變數與運算子所
組成 分類 ( 以運算的結果型態來分 ):
算術運算式Ex: StandardWeight = ( Height – 100 ) * 0.9;
字串運算式Ex: StringNew = “JSP” + “ easy to use”;
邏輯運算式Ex: DoILoveYou = GoofToMe && FaithToMe;
條件運算式 : ( 條件式 ) ? 數值 1: 數值 2;
Ex: Judge = ((12>10) ? “Yes”:”No”);
■ 範例
<%@page contentType="text/html;charset=Big5" %><html><head><title> 運算式的應用 </title></head><body>算術運算式 : StandardWeight = ( Height - 100 ) * 0.9;<br>當 Height=172 時 StandardWeight 的結果為<%int Height=173;double StandardWeight;StandardWeight = ( Height - 100 ) * 0.9;out.write(StandardWeight + "<br>");%><hr>字串運算式 :StringNew = "JSP " + " easy to use!";<br>輸出結果為 :<%String StringNew;StringNew = "JSP " + " easy to use!";out.write(StringNew + "<br>");%><hr>
■ 範例
邏輯運算式 :DoILoveYou = GoodToMe && FaithToMe;<br>當 GoodToMe 為 true 與 FaithToMe 為 true 時 DoILoveYou 的結果為 :<%boolean GoodToMe = true,FaithToMe=true,DoILoveYou;DoILoveYou = GoodToMe && FaithToMe;out.write(DoILoveYou + "<br>");%><hr>條件運算式 :judge = ((12>10) ? "Yes" : "No");<br>輸出結果為 :<%String judge;judge = ((12>10) ? "Yes" : "No");out.write(judge);%></body></html>
2-2-2 運算子
定義 : 運算子 (operator) 是指運算的符號
分類 :
算術運算子
指定運算子
邏輯運算子
比較運算子
逐位元運算子
特殊運算子
■算術運算子
• 包含 : 加 (+), 減 (-), 乘 (*), 除 (/), 求餘數 (mod), 遞增 (+
+), 遞減 (--) 與取負值 (-) 等八個
• 範例十七<%@page contentType="text/html;charset=Big5" %><html><head><title> 算數運算子的應用 </title></head><body><% int X=10,Y=6;%>X=10,Y=6 兩個數的四則運算結果為 <br>X + Y = <%=X+Y %> <br>X - Y = <%=X-Y %> <br>X * Y = <%=X*Y %> <br>X / Y 的商數為 <%=X/Y %> <br>X % Y 的餘數為 <%=X%Y %> <br><hr>++X, 其中的 "++" 指的是遞增運算子 ++X 代表的是 X = X + 1, 結果為 <%=++X %> <br>--X, 其中的 "--" 指的是遞減運算子 --X 代表的是 X = X - 1, 結果為 <%=--X %> <br>-X 其中的 "-" 指的是負數運算子將 X 值取負號 , 結果為 <%=-X %> <br></body></html>
■算術運算子
• 範例十八 ( 遞增算子 )<%@page contentType="text/html;charset=Big5" %><html><head><title> 遞增運算子的應用 </title></head><body><% int X=10,Y=6;%>X =<%=X %> , Y=<%=Y %><br>X++ 後 ,X 等於 <%=X++ %><br>X+Y 等於 <%=X+Y %><br>X 等於 <%=X %><br><hr><% X=10; Y=6;%>X =<%=X %> , Y=<%=Y %><br>++X 後 ,X 等於 <%=++X %><br>X+Y 等於 <%=X+Y %><br>X 等於 <%=X %><br></body></html>
■指定運算子
• 指定運算子
包含 : =,+=,-=,*=/=,%=,<< = ,>>= ,>>>=,&=,|=,^=
• 範例十九<html><head><title> 指定運算子的運用 </title></head><body><% int x=0; x = 44; out.write(" x =44 , x=" + x +"<br>"); x +=3; out.write(" x +=3 , x=" + x +"<br>"); x -=3; out.write(" x -=3 , x=" + x +"<br>"); x *=3; out.write(" x *=3 , x=" + x +"<br>"); x /=3;
out.write(" x /=3 , x=" + x +"<br>"); x %=47; out.write(" x %=47 , x=" + x +"<br>"); x <<=3; out.write(" x <<=3 , x=" + x +"<br>"); x >>=3; out.write(" x >>=3 , x=" + x +"<br>"); x >>>=3; out.write(" x >>>=3 , x=" + x +"<br>"); x &=3; out.write(" x &=3 , x=" + x +"<br>"); x |=3; out.write(" x |=3 , x=" + x +"<br>"); x ^=3; out.write(" x ^=3 , x=" + x +"<br>");%></body></html>
■邏輯運算子
• 邏輯運算子
包含 :&&(AND),||(OR),!(NOT)
• 範例二十<html><head><title> 邏輯運算子的運用 </title></head><body><% int a=10,b=11,c=12,d=13; boolean e; out.write("a = " + a + ",b=" + b + ",c=" + c + ",d=" + d + "<br>"); e = a > b; out.write("a > b is " + e +"<br>"); e = c < d; out.write("c < d is " + e +"<br>"); e = (a > b) && (c < d); out.write("(a > b) && (c < d) is " + e +"<br>"); e = (a > b) || (c < d); out.write("(a > b) || (c < d) is " + e +"<br>"); e = !(a > b); out.write("!(a > b) is " + e +"<br>");%></body></html>
■比較運算子
• 比較運算子包含 :>( 大於 ),< ( 小於 ),>= ( 大於等於 ),<= ( 小於等於 ),!=( 不等於 ),==
( 等於 )
• 範例二十一<html><head><title> 比較運算子的運用 </title></head><body><% boolean c; c = 12 > 13; out.write("c = 12 > 13, c is " + c +"<br>"); c = 12 < 13; out.write("c = 12 < 13, c is " + c +"<br>"); c = 12 >= 13; out.write("c = 12 >= 13, c is " + c +"<br>"); c = 12 <= 13; out.write("c = 12 <= 13, c is " + c +"<br>"); c = 12 != 13; out.write("c = 12 != 13, c is " + c +"<br>"); c = 12 == 13; out.write("c = 12 == 13, c is " + c +"<br>");%></body></html>
■逐位元運算子• 逐位元運算子
包含 :&( 逐位元 AND),| ( 逐位元 OR),^ ( 逐位元 XOR),~ ( 逐位元 NOT),<
< ( 逐位元左移 ),>> ( 逐位元右移 ),>>> ( 逐位元右移且補零 )
• 範例二十二<html><head><title> 逐位元運算子的運用 </title></head><body><% int a=10,b=111,c=-10,d=0; out.write("a = " + a + ",b=" + b + ",c=" + c + ",d=" + d + "<br>"); d = a & b; out.write("a & b is " + d +"<br>"); d = a | b; out.write("a | b is " + d +"<br>"); d = a ^ b; out.write("a ^ b is " + d +"<br>"); d = ~c; out.write("~c is " + d +"<br>"); d = a << 3; out.write("a << 3 is " + d +"<br>"); d = b >> 3; out.write("b >> 3 is " + d +"<br>"); d = c >>> 3; out.write("c >>> 3 is " + d +"<br>");%></body></html>
■特殊運算子• 特殊運算子 new: 建立物件型態之實體• 範例二十三<%@page contentType="text/html" import = "java.util.*" %><html><head><title> new 運算子的運用 ( 進站時間與數字時鐘 )</title></head><body><% Date today = new Date(); String week = "week"+ today.getDay() + ".gif"; String Y1 = (today.getYear()+1900)/1000 + ".gif"; String Y2 = ((today.getYear()+1900) % 1000 )/100 + ".gif"; String Y3 = ((today.getYear()+1900) % 100 )/10 + ".gif"; String Y4 = ((today.getYear()+1900) % 10 ) + ".gif"; String M1 = (today.getMonth()+1) /10 + ".gif"; String M2 = (today.getMonth()+1) % 10 + ".gif"; String D1 = (today.getDate()) /10 + ".gif"; String D2 = (today.getDate()) %10 + ".gif"; String H1 = (today.getHours()) /10 + ".gif"; String H2 = (today.getHours()) %10 + ".gif"; String m1 = (today.getMinutes()) /10 + ".gif";
String m2 = (today.getMinutes()) %10 + ".gif"; String S1 = (today.getSeconds()) /10 + ".gif"; String S2 = (today.getSeconds()) %10 + ".gif";%>您進入本站的時間是<hr><img src=<%=Y1 %>><img src=<%=Y2 %>><img src=<%=Y3 %>><img src=<%=Y4 %>><img src="year.gif"><img src=<%=M1 %>><img src=<%=M2 %>><img src="month.gif"><img src=<%=D1 %>><img src=<%=D2 %>><img src="day.gif"><img src=<%=week%>><br><img src=<%=H1 %>><img src=<%=H2 %>><img src="comm.gif"><img src=<%=m1 %>><img src=<%=m2 %>><img src="comm.gif"><img src=<%=S1 %>><img src=<%=S2 %>><br></body></html>
• 特殊運算子 :? 運算子
• 範例二十四
<html><head><title>? 運算子的應用 </title></head><body><% int JSP_Grade; String comment; JSP_Grade = 84; comment = (JSP_Grade >=60) ? "Pass" : "Flunk";%>你的 JSP 成績結果是 <%= comment %></body></html>
( 運算式 1) ? 運算式 2 : 運算式 3
■一元 , 二元 , 三元運算子
• 一元運算子 (unary operator): 只需一個運算元
• 二元運算子 (binary operator): 需要兩個運算元
• 三元運算子 (ternary): 需要三個運算元
運算子 運算元 ; 或是 運算元 運算子
運算元 運算子運算元 ;
( 運算式 1) ? 運算式 2 : 運算式
■運算子的優先順序
運算子優先順序 ( 號數越大順序越高 ) 1. 指定運算子 (=,+=,-=,*= 等 ) 2. 條件運算子 (? :) 3. 邏輯 OR(||) 4. 邏輯 AND(&&) 5. 逐位元或 (OR) 6. 逐位元互斥或 (XOR) 7. 逐位元且 (AND) 8. 比較運算子 (==,!=) 9. 比較運算子 (<,>,<=,>=) 10. 逐位元移動 (<<,>>,>>>) 11. 加減 (+,-) 12. 乘除 (*,/) 13. 遞增 , 異減 (++,--) 14. 成員運算子 ((),[],.)
2-3 控制敘述
2-1 for 敘述 for 是用來建立重複執行的廻圈 宣告
範例 :
for([ 起始運算式 ];[ 條件式 ];[ 遞增或遞減運算式 ]);
<html> <head><title>For 迴圈範例 ( 九九乘法表 )</title></head><body><table border=1 bgcolor=#11CCFF align=center><tr><td colspan=9 align=center><font size=5><b> 九九乘法表 </b></font></td></tr><% int i,j; for(i=1;i<=9;i++) {
out.write("<tr>"); for(j=1;j<=9;j++) { out.write( "<td>"+ i + "*" + j + " = " + i * j + "</td>"); } out.write("</tr>"); } out.write("</table></p>");%></body></html>
2-3 控制敘述
2-1 for 敘述 for 是用來建立重複執行的廻圈 宣告
範例 :
for([ 起始運算式 ];[ 條件式 ];[ 遞增或遞減運算式 ]);
<html> <head><title>For 迴圈範例 ( 九九乘法表 )</title></head><body><table border=1 bgcolor=#11CCFF align=center><tr><td colspan=9 align=center><font size=5><b> 九九乘法表 </b></font></td></tr><% int i,j; for(i=1;i<=9;i++) {
2-3 控制敘述
2-1 for 敘述 for 是用來建立重複執行的廻圈 宣告
範例 :
for([ 起始運算式 ];[ 條件式 ];[ 遞增或遞減運算式 ]);
<html> <head><title>For 迴圈範例 ( 九九乘法表 )</title></head><body><table border=1 bgcolor=#11CCFF align=center><tr><td colspan=9 align=center><font size=5><b> 九九乘法表 </b></font></td></tr><% int i,j; for(i=1;i<=9;i++) {
2-3 控制敘述
2-1 for 敘述 for 是用來建立重複執行的廻圈 宣告
範例 :
for([ 起始運算式 ];[ 條件式 ];[ 遞增或遞減運算式 ]);
<html> <head><title>For 迴圈範例 ( 九九乘法表 )</title></head><body><table border=1 bgcolor=#11CCFF align=center><tr><td colspan=9 align=center><font size=5><b> 九九乘法表 </b></font></td></tr><% int i,j; for(i=1;i<=9;i++) {
2-3 控制敘述
2-1 for 敘述 for 是用來建立重複執行的廻圈 宣告
範例 :
for([ 起始運算式 ];[ 條件式 ];[ 遞增或遞減運算式 ]);
<html> <head><title>For 迴圈範例 ( 九九乘法表 )</title></head><body><table border=1 bgcolor=#11CCFF align=center><tr><td colspan=9 align=center><font size=5><b> 九九乘法表 </b></font></td></tr><% int i,j; for(i=1;i<=9;i++) {
2-3 控制敘述
2-1 for 敘述 for 是用來建立重複執行的廻圈 宣告
範例 :
for([ 起始運算式 ];[ 條件式 ];[ 遞增或遞減運算式 ]);
<html> <head><title>For 迴圈範例 ( 九九乘法表 )</title></head><body><table border=1 bgcolor=#11CCFF align=center><tr><td colspan=9 align=center><font size=5><b> 九九乘法表 </b></font></td></tr><% int i,j; for(i=1;i<=9;i++) {