1 / 49

JSP 簡介

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> <%

aneko
Télécharger la présentation

JSP 簡介

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JSP簡介 1 第一個JSP程式 2 JSP的基本語法 3 字串的運算

  2. 1 第一個JSP程式 • Hello World <html> <head> <title>hello word</title> </head> <body> <% out.write("Hello World!"); %> </body> </html>

  3. 1 第一個JSP程式 • 您好 <html> <head> <title>hello word</title> </head> <body> <% out.write(“您好!"); %> </body> </html>

  4. 1 第一個JSP程式 • 您好 <%@ page contentType="text/html; charset=Big5" %> <html> <head> <title>hello word</title> </head> <body> <% out.write(“您好!"); %> </body> </html>

  5. 2 JSP的基本語法 記錄國文成績 記錄英文成績 加總

  6. 跟記憶體要一塊空間用來存放國文成績 • 用一個變數表示這個空間 Chinese

  7. 2 JSP的基本語法 2-1 JSP的定值與變數 • 資料型別 • 整數(integer) • 浮點數(floating) • 倍精度(double) • 字元(character) • 字串(string) • 布林(boolean)

  8. Chinese 2-1-2 整數類 • 整數有四類 • byte 字元組 • int 整數 • short 短整數 • long 長整數 • 數值長度與範圍 • byte大小為1byte,範圍-128~127 • int大小為2bytes,範圍-32768~32767 • short大小為4bytes,範圍-2147483648~2147483647 • long大小為8bytes,範圍-9233372036854775808~9233372036854775807 -2(K-1)~2(K-1)-1

  9. Chinese 2-1 JSP的定值與變數 • 所有使用的變數都要宣告 • 變數是一個存放數值的空間 • 變數命名原則 • 大小寫會區別 • 不可用保留字 (for、if、while …) • 變數必須是字母開頭或是底線“_”或是 “$“作為開頭 • 開頭字母有後可以使用數字與字元來命名 • 命名要用英文字母與相關符號

  10. 2-1 JSP的定值與變數 • 變數範例 • Taxi_ii • int_NewType • $MyName • _hello_world • 變數宣告方法 變數型態關鍵字 識別字(=初值); 變數型態關鍵字 識別字1 (=初值1),識別字2 (=初值2),…..;

  11. Chinese Math JSP English ■ 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>

  12. ■ 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 Math JSP English total

  13. ■ 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 Math JSP English total

  14. ■ 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>

  15. 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>

  16. 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>

  17. 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>

  18. int X = 100; int Y =

  19. A[0] A[1] A[2] A[5] A[3] A[4] 2-1-7 陣列 • 陣列是可以存放多個變數的序列資料結構 • 透過註標(index)來指定特定變數的位置(由0開始) • Array宣告(一維) • 資料型態[]; • 識別字 = new 資料型態[陣列大小n]; • 或是 • 資料型態 識別字[]={初值1,初值2,初值3…} • 範例十一(一維陣列)

  20. <%@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>

  21. ■ 二維陣列到多為陣列 • Array宣告(二維) • 資料型態[][]; • 識別字 = new 資料型態[陣列一維大小M] [陣列二維大小N]; • 或是 • 資料型態 識別字[]={{初值11,初值12,初值13…,初值1N}, • {初值21,初值22,初值23…,初值2N}, • {初值31,初值32,初值33…,初值3N}, • …………………………………… • {初值M1,初值M2,初值M3…,初值MN}} • 範例十二(二維陣列固定N值) • 二維陣列最多可以放置M(列數) x N(欄數),N值可以是不固定.

  22. <%@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>

  23. ■ 二維陣列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>

  24. 2-1-8 型態轉換 • 自動型態轉換的規則 • 所有運算的資料型態都相同可以進行運算 • 在同一大類的型態運算中運算結果的資料長度大於運算資料資料型態則可以進行運算 • 跟字串進行運算的其他型態會先轉換成字串 • 見下面範例十四

  25. <%@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>

  26. ■不相容型態轉換 • 不相容型態轉換宣告方式 (欲轉換的資料型態) 變數或數值; • 範例十五 <%@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>

  27. 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”);

  28. ■ 範例 <%@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>

  29. ■ 範例 邏輯運算式: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>

  30. 2-2-2 運算子 • 定義:運算子(operator)是指運算的符號 • 分類: • 算術運算子 • 指定運算子 • 邏輯運算子 • 比較運算子 • 逐位元運算子 • 特殊運算子

  31. ■ 算術運算子 • 包含:加(+),減(-),乘(*),除(/),求餘數(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>

  32. ■ 算術運算子 • 範例十八(遞增算子) <%@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>

  33. ■ 指定運算子 • 指定運算子 • 包含: =,+=,-=,*=/=,%=,<< = ,>>= ,>>>=,&=,|=,^= • 範例十九 <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;

  34. 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>

  35. ■ 邏輯運算子 • 邏輯運算子 • 包含:&&(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>

  36. ■ 比較運算子 • 比較運算子 • 包含:>(大於),< (小於),>= (大於等於),<= (小於等於),!=(不等於),==(等於) • 範例二十一 <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>

  37. ■ 逐位元運算子 • 逐位元運算子 • 包含:&(逐位元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>

  38. ■ 特殊運算子 • 特殊運算子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";

  39. 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>

  40. 特殊運算子:?運算子 • 範例二十四 • (運算式1) ? 運算式2 : 運算式3 <html> <head> <title>?運算子的應用</title> </head> <body> <% int JSP_Grade; String comment; JSP_Grade = 84; comment = (JSP_Grade >=60) ? "Pass" : "Flunk"; %> 你的JSP成績結果是<%= comment %> </body> </html>

  41. ■ 一元,二元,三元運算子 • 一元運算子(unary operator):只需一個運算元 • 二元運算子(binary operator):需要兩個運算元 • 三元運算子(ternary):需要三個運算元 運算子 運算元; 或是 運算元 運算子 運算元 運算子運算元; (運算式1) ? 運算式2 : 運算式

  42. ■ 運算子的優先順序 • 運算子優先順序(號數越大順序越高) • 1.指定運算子(=,+=,-=,*=等) • 2.條件運算子(? :) • 3.邏輯OR(||) • 4.邏輯AND(&&) • 5.逐位元或(OR) • 6.逐位元互斥或(XOR) • 7.逐位元且(AND) • 8.比較運算子(==,!=) • 9.比較運算子(<,>,<=,>=) • 10.逐位元移動(<<,>>,>>>) • 11.加減(+,-) • 12.乘除(*,/) • 13.遞增,異減(++,--) • 14.成員運算子((),[],.)

  43. 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++) {

  44. 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>

  45. 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++) {

  46. 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++) {

  47. 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++) {

  48. 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++) {

  49. 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++) {

More Related