1. JSP椤甸?㈢???烘??缁???
? JSP椤甸?㈠????绉???绱?缁???????锛??? ??????HTML??璁扮?????JSP??璁帮?濡???浠ゆ??璁般???ㄤ???璁帮??????????????规?锛???Java绋?搴???锛???Java琛ㄨ揪寮??
澶???杩?绋?锛?
? 1.??JSP椤甸??腑??????HTML??璁扮???凤?浜ょ?瀹㈡?风??娴?瑙??ㄦ?ц??剧ず??
? 2.JSP??璁般???版?????规?澹版????Java绋?搴?????omcat???″?ㄨ?璐f?ц?锛?灏???瑕??剧ず??缁???????缁?瀹㈡?风??娴?瑙??ㄣ??
? 3. Java琛ㄨ揪寮???omcat???″?ㄨ?璐h?$??锛?灏?缁???杞???涓哄??绗?覆锛?浜ょ?瀹㈡?风??娴?瑙??ㄨ?璐f?剧ず???
2. JSP椤甸?㈢?????????????规??
? 1.??SP椤甸?㈢????璁扮????<%!??????锛?>??涔??村0????????绉颁?JSP椤甸?㈢?????????? .
? 2. JSP椤甸?㈠?ㄢ??<%!??????锛?>??涔??村0??瀹?涔? ??规? .
3. Java绋?搴????
?ㄢ??<%??????%>??涔??存????ava绋?搴????
绋?搴???涓?澹版????????绉颁负JSP椤甸?㈢??灞??ㄥ?????
澶?涓?瀹㈡?疯?锋?涓?涓?JSP椤甸?㈡??, Java绋?搴???灏?琚??ц?澶?娆★??????ㄤ?????绾跨?涓??ц????
4. Java琛ㄨ揪寮??
??<%=??????%>??涔??村??浠ユ??涓?涓?Java琛ㄨ揪寮? .
琛ㄨ揪寮????肩?辨???″?ㄨ?璐h?$??锛?骞跺?璁$??缁????ㄥ??绗?覆褰㈠??????板?㈡?风???剧ず?? . ?
5.JSP涓???娉ㄩ???
?HTML娉ㄩ???煎?:
? ? ? <!-- ? ?娉ㄩ????瀹?? ? -->?
?JSP娉ㄩ???煎?:
? ? ? <%-- ?娉ㄩ????瀹??--%>?
6. JSP ??浠ゆ??璁奥?
page ??浠ゆ??璁?
? page ??浠ょ?ㄦ?ュ??涔??翠釜JSP椤甸?㈢??涓?浜?灞??у??杩?浜?灞??х???硷?灞??у?肩?ㄥ??寮??锋????寮??锋??璧锋?ャ????浠ョ?ㄤ?涓?page??浠ゆ??瀹?澶?涓?灞??х????,涔???浠ヤ娇?ㄥ?涓?page??浠ゅ????涓烘??涓?灞??ф??瀹???.
? page??浠ょ??浣??ㄥ?规?翠釜JSP椤甸?㈡????锛?涓??朵功????浣?缃????筹?涔?????page??浠ゅ????SP椤甸?㈢??????????渚?濡?:
? <%@ page ? 灞???=??灞??????尖?? ?灞???= ??灞??????尖?? ?????>?
??
? <%@ page ? 灞???="灞??????? ?%>
? <%@ page ? 灞???="灞??????? ?%>
? ????
? <%@ page ? 灞???="灞??????? ?%>?
page ??浠ゆ??璁板??浠ユ??瀹?濡?涓?灞??х???硷?
contentType??import??language??session??buffer??auotFlush ??isThreadSafe??pageEncoding?
contentType灞????
? contentType 灞??у?肩‘瀹?JSP椤甸?㈠??搴???MIME锛?Multipurpose Internet Mail Extention锛?绫诲????JSP椤甸?㈠??绗???缂?????
? ?渚?濡?:
<%@ page contentType="text/html;charset=GB2312" %>?
? ?涓???璁镐袱娆′娇??age ??浠ょ?contentType灞??ф??瀹?涓?????灞??у??
import 灞???
璇ュ??х??浣??ㄦ??涓?SP椤甸?㈠???ava杩?琛???澧???渚?????涓???绫伙?杩??峰氨??浠ュ??SP椤甸?㈢??绋?搴????ㄥ???????????芥?板0???ㄥ????琛ㄨ揪寮??ㄥ??浣跨?ㄥ??涓???绫汇??
渚?濡?:
<%@ page ?import="java.io.*", "java.util.Date" %>
language灞???
瀹?涔?JSP椤甸??娇?ㄧ??????璇?瑷?锛?璇ュ??х???肩???????藉??"java"??
涓?anguage灞??ф??瀹??肩???煎?锛?
<%@ page ?language="java" %>
session ?灞???
?ㄤ?璁剧疆??????瑕?浣跨?ㄥ??缃???session瀵硅薄??session??灞??у?煎??浠ユ??true??false??session灞??ч?璁ょ??灞??у?兼??true??
buffer 灞???
??缃?杈??烘?瀵硅薄out璐?璐e????″?ㄧ????浜?淇℃????杩?琛?缁????????板?㈡?风???剧ず锛?buffer灞??х?ㄦ?ユ??瀹?out璁剧疆??缂??插?虹??澶у???涓?浣跨?ㄧ??插?恒??渚?濡?锛?
<%@ page buffer= ??24kb?? %>buffer灞??х??榛?璁ゅ?兼??8kb ??buffer灞??у??浠ュ???? none"锛?璁剧疆out涓?浣跨?ㄧ??插?恒??
autoFlush 灞???
??瀹?out??缂??插?鸿?濉?婊℃?讹?缂??插?烘???????ㄥ?锋?般??
isThreadSafe 灞???
?ㄦ?ヨ?剧疆JSP椤甸?㈡??????澶?绾跨?璁块?????
include ??浠ゆ??璁奥?
濡?????瑕???SP椤甸?㈠????澶??翠?宓??ヤ?涓???浠讹?灏卞??浠ヨ????浣跨??nclude??浠ゆ??璁帮??惰??娉??煎?濡?涓?锛?
<%@ include file= "??浠剁??URL " %>?
濡???璇ユ??浠跺??褰???JSP椤甸?㈠?ㄥ??涓?Web???$??褰?涓?锛??d?????浠剁??URL??灏辨????浠剁????瀛?锛?濡???璇ユ??浠跺??SP椤甸?㈡???ㄧ??Web???$??褰???涓?涓?瀛???褰?涓?锛?姣?濡?fileDir瀛???褰?涓?锛??d?????浠剁??URL??灏辨????fileDir/??浠剁????瀛??????
???????ワ?灏辨??褰???JSP椤甸?㈠?????ョ????浠跺??骞舵??涓?涓??扮??JSP椤甸????跺??JSP寮?????灏?杩?涓??扮??JSP椤甸?㈣浆璇???Java??浠躲???
7.JSP ?ㄤ???璁奥?
include ?ㄤ???璁奥?
<jsp:include page= "??浠剁??URL"/>
??
<jsp:include page= "??浠剁??URL">
? ?param瀛???璁?
</jsp:include>
?include ?ㄤ???璁版????SP椤甸?㈣?琛??舵??澶?????浠讹?琚?澶???????浠跺?ㄩ?昏???璇?娉?涓???绔?浜?褰???JSP椤甸?? 瑕?涓?include ??浠ゆ??璁扮?稿?哄????
param?ㄤ???璁奥?
param??璁颁互????瀛????尖??瀵圭??褰㈠?涓哄?跺????璁版??渚?????淇℃??.
param??璁颁??界??绔?浣跨?????浣?涓?sp:include??jsp:forward??jsp:plugin??璁扮??瀛???璁版?ヤ娇?ㄣ??
param?ㄤ???璁拌??娉??煎?锛?
<jsp:param ?name= ????瀛??? ?value= ????瀹?缁?param???尖??>
褰?璇ユ??璁颁?jsp:include?ㄤ???璁颁?璧蜂娇?ㄦ?讹???浠ュ?param??璁颁腑???间?????nclude?ㄤ???璁拌???杞界????浠朵腑?伙?琚???杞界??JSP??浠跺??浠ヤ娇??omcat???″?ㄦ??渚???request??缃?瀵硅薄?峰??include?ㄤ???璁扮??param瀛???璁颁腑name灞??ф????渚????悸?
forward ?ㄤ???璁?
? 璇ユ??浠ょ??浣??ㄦ??锛?浠?璇ユ??浠ゅ???姝㈠???椤甸?㈢??缁х画?ц?锛???杞????ц?page灞??ф??瀹???JSP椤甸?????
<jsp:forward page="瑕?杞?????椤甸?? />
??
<jsp:forward page="瑕?杞?????椤甸?? >
? ?param瀛???璁?
</jsp:forward>?
useBean ?ㄤ???璁奥?
? ?璇ユ??璁扮?ㄦ?ュ??寤哄苟浣跨?ㄤ?涓?Javabean锛?????甯搁??瑕???涓?涓??ㄤ???璁帮???浠?灏??ㄧ??4璇捐???璁ㄨ?恒??Sun???哥???″?兼??锛???TML瀹???JSP椤甸?㈢???????ㄥ??锛???avabean瀹????ㄦ???ㄥ??锛?瀹??扮??姝f??涔?涓??????????ㄦ????绂汇???
渚?瀛?锛?
example2_1.jsp
<%@ page contentType="text/html;charset=GB2312" %> ?<!-- jsp??浠ゆ??璁?-->
<%@ page import="java.util.Date" ?%> ? ? ? ? ? ? ? ?<!-- jsp??浠ゆ??璁?-->
<%! ? Date date; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // ?版??澹版??
? ? ? int start,end,sum;
? ? ? public int continueSum(int start,int end) ? ?// ?规?澹版???
? ? ? { ?for(int i=start;i<=end;i++) ?
? ? ? ? ? ? sum=sum+i;?
? ? ? ? ?return sum;
? ? ? }
? %>
<HTML><BODY bgcolor=cyan> ? ? ? ? ? ? ? ? ? ? ? ? <!??html??璁?-->?
<FONT size=4><P>绋?搴?????寤?ate瀵硅薄锛?
? ?<% date=new Date(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//java绋?搴????
? ? ? out.println("<BR>"+date);
? ? ? start=1;
? ? ? end=100;
? ? ? sum=continueSum(start,end);
? ?%>
<BR>浠?
? ?<%= start %> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!-- Java琛ㄨ揪寮? -->?
? ???
? ?<%= end %>?
? ???杩?缁?????
? ?<%= sum %>?
</FONT></BODY></HTML>
渚?瀛?锛?
example2_2.jsp锛?
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY BGCOLOR=cyan><FONT size=3>
?<%! int i=0;
?%>
?<% i++;
?%>
<P>?ㄦ??绗?
? <%= i %>
?涓?璁块????绔???瀹㈡?枫??
</BODY></HTML> ?
渚?瀛?3
example2_3.jsp锛?
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY bgcolor=cyan>
? ?<%! double x,y;?
? ? ? ?double multi(double x,double y)
? ? ? ?{ ?return x*y;
? ? ? ?}
? ? ? ?double div(double x,double y)?
? ? ? ?{ ?return x/y;
? ? ? ?}
? ?%>
? ?<% ?x=127;
? ? ? ?y=18;
? ? ? ?out.println("璋???ulti?规?璁$??"+x+"涓?"+y+"涔?绉?锛?");
? ? ? ?double a=multi(x,y);
? ? ? ?out.println(a);
? ? ? ?x=a;
? ? ? ?out.println("<BR>璋???iv?规?璁$??"+x+"?や互"+y+"????锛?");
? ? ? ?double b=div(x,y);
? ? ? ?out.println(b);?
? ?%>
</BODY></HTML>
渚?瀛?4
example2_4.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<HTML><BODY>
? ?<%! int count=0; ? ? ? ? ? ? ? ?//琚?瀹㈡?峰?变韩??count
? ? ? ?synchronized void setCount() ?//synchronized淇?楗扮???规?
? ? ? ?{ ? count++;
? ? ? ?}
? ?%>
? ?<% ?setCount();
? ? ? ? out.println("?ㄦ??绗?"+count+"涓?璁块????绔???瀹㈡??); ??
? ?%>
</BODY></HTML>
渚?瀛?5
example2_5.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<HTML><BODY bgcolor=cyan>
?<P> 杈??鸿?辨??瀛?姣?.
?<% ?char start='A',end='M',c;
? ? ? int size=2;
?%>
?<BR> ??lt;%=size %>纾?瀛??疯???
?<Font size=<%=size%>>
?<% ?for(c=start;c<=end;c++)
? ? ? {
?%> ? ? <%= c %>,
?<% ?}
?%>
?</Font>
?<% ?size=4;
? ? ?start=(char)(end+1);
? ? ?end='Z';
?%>
<BR> ??lt;%=size %>纾?瀛??疯???
?<Font size=<%=size%>>
?<% ?for(c=start;c<=end;c++)
? ? ? {
?%> ? ? <%= c %>,
?<% ?}
?%>
?</Font></BODY></HTML>
渚?瀛?6
example2_6.jsp锛?
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY ?bgcolor=cyan><FONT size=3>
<% ?double a=12.89;
? ?int x=12,y=10;
%>
璁$??琛ㄨ揪寮?a+x*y-100/x+x%5?????
?<%= a+x*y-100/x+x%5 %>
<BR>璁$??琛ㄨ揪寮?x>y&&a>0????
?<%=x>y&&a>0%>
<BR>璁$??琛ㄨ揪寮?sin(x)+cos(y):
?<%=Math.sin(x)+Math.cos(y)%>
<BR> 姹?8??骞虫?规??
?<%=Math.sqrt(8)%>
</FONT></BODY></HTML>
渚?瀛?7
example2_7.jsp锛?
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY>
?<!-- 浠ヤ?瀛?浣???棰??蹭负????-->
?<FONT size=3 color=blue>?藉??瀛?绗?覆"C:\myfile\jspfile\example.jsp"涓???"example.jsp"?
</FONT>
? <%-- 涓??㈡????????????澹版?? --%>
?<%! ?String s="C:\\myfile\\jspfile\\example.jsp";
?%>
?<%-- 涓??㈡??Java绋?搴??? --%>
?<% ?int index=s.lastIndexOf("\\");?
? ? ?String str=s.substring(index+1);
?%>
?<BR><%-- 涓??㈡??Java琛ㄨ揪寮? --%>?
?<%= str %>
</BODY></HTML> ?
渚?瀛?8
first.jsp
<%@ page contentType="text/html;Charset=GB2312" %>?
<HTML><BODY BGCOLOR=cyan> ? ? ? ? ? ? ? ? ? ??
<FONT Size=3> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
<P>浣跨??tml澶??????ュ???扮??淇℃??.
? <input type=text size=10> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</FONT></BODY></HTML>
second.jsp
<%@ page contentType="application/vnd.ms-powerpoint" %>?
<HTML><BODY BGCOLOR=cyan> ? ? ? ? ? ? ? ? ? ??
<FONT Size=8> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
<P>????owerpoint搴??ㄧ?搴?澶??????ュ???扮??淇℃??.
<input type=text size=10> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</FONT></BODY></HTML>
third.jsp
<%@ page contentType="image/x-xbitmap" %>?
<%@ page import="java.awt.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="java.awt.geom.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<% ? int width=260, height=260;
? ? BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
? ? Graphics g = image.getGraphics();
? ? g.setColor(Color.white);
? ? g.fillRect(0, 0, width, height);
? ? Graphics2D g_2d=(Graphics2D)g;?
? ? Ellipse2D ellipse=new Ellipse2D. Double (40,50,100,40);
? ? g_2d.setColor(Color.blue);
? ? g_2d.draw(ellipse);
? ? Polygon polygon=new Polygon();
? ? polygon.addPoint(12,34);
? ? polygon.addPoint(51,134);
? ? polygon.addPoint(102,34);
? ? g_2d.drawPolygon(polygon);
? ? g_2d.dispose();
? ? OutputStream outClient= response.getOutputStream(); ? //?峰??????瀹㈡?风????杈??烘?
? ? JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(outClient);
? ? encoder.encode(image);
%>
渚?瀛?9
example2_9.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page info="娓???澶у?? %>
<% ?String s=getServletInfo();
%>
<head> ?<%= s %></head>
<HTML><BODY ?bgcolor=cyan><FONT Size=4>
<BR><%=s %>?虹??绀炬??涓??借?????虹??绀?
<BR><%=s %>???ㄥ?借??????楂?绛?瀛??
</BODY><HTML>
渚?瀛?10
firstExample2_10.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ include file="myfile/ok.txt" %>
<HTMLl><BODY bgcolor=cyan>
<P>????firstExample.jsp椤甸??
</BODY></HTML>
secondExample2_10.jsp锛?????濡???.9锛?b锛???绀猴?
<%@ page contentType="text/html;charset=GB2312" %>
<%@ include file="myfile/ok.txt" %>
<HTML><BODY bgcolor=yellow>
<P>????secondExample.jsp椤甸??
</BODY></HTML>
涓?杩?irstExample2_10.jsp绛?浠蜂?涓??㈢??JSP??浠?ame.jsp锛?
same.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<A href="firstExample2_10.jsp" target="_blank" rel="external nofollow" >娆㈣???irstExample2_10.jsp椤甸??lt;/A>
<BR>
<A href="secondExample2_10.jsp" target="_blank" rel="external nofollow" >娆㈣???econdExample2_10.jsp椤甸??lt;/A>
<HTML><BODY bgcolor=cyan>
<P>????firstExample.jsp椤甸??
</BODY></HTML>
渚?瀛?11
example2_11.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY ?bgcolor=yellow >
<% double a=3,b=4,c=5;
%>
<BR>??杞?rangle.jsp璁$??涓?杈逛负<%=a%>,<%=b%>,<%=c%>??涓?瑙?褰㈤?㈢Н锛?
? <jsp:include page="myfile/trangle.jsp">
? ? <jsp:param name="sideA" value="<%=a%>"/>
? ? <jsp:param name="sideB" value="<%=b%>"/>
? ? <jsp:param name="sideC" value="<%=c%>"/>
? </jsp:include>
</BODY></HTML>
trangle.jsp
<%@ page contentType="text/html;charset=GB2312" %>?
<%! public String getArea(double a,double b,double c)
? ? { if(a+b>c&&a+c>b&&c+b>a)?
? ? ? ?{ ? double p=(a+b+c)/2.0;
? ? ? ? ? ?double area=Math.sqrt(p*(p-a)*(p-b)*(p-c)) ;
? ? ? ? ? ?return ""+area;
? ? ? ?}
? ? ?else?
? ? ? ?{ ?return(""+a+","+b+","+c+"涓??芥????涓?涓?涓?瑙?褰???娉?璁$???㈢Н");
? ? ? ?}
? ?}
%>
<% ? String sideA=request.getParameter("sideA");
? ? ? String sideB=request.getParameter("sideB");
? ? ? String sideC=request.getParameter("sideC");
? ? ? double a=Double.parseDouble(sideA);
? ? ? double b=Double.parseDouble(sideB);
? ? ? double c=Double.parseDouble(sideC);
%>
<Font color=blue size=4>
<P>????琚???杞界????浠?璐?璐h?$??涓?瑙?褰㈢???㈢Н<BR>
? ?缁???浼?????涓?杈规??:<%=sideA%>,<%=sideB%>,<%=sideC%>
<BR>涓?瑙?褰㈢???㈢Н:<%= getArea(a,b,c)%>
</Font>
渚?瀛?12
example2_12.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY>
<% ?double i=Math.random();
? ?if(i>0.5)
? ?{
%> ?<jsp:forward page="frontSide.jsp" >
? ? ? ? ?<jsp:param name="number" value="<%= i %>" />
? ? </jsp:forward>?
<% ?}
? else
? ?{
%> ?<jsp:forward page="reverseSide.jsp" >
? ? ? ? ?<jsp:param name="number" value="<%= i %>" />
? ? ?</jsp:forward>?
<% }
%>
</BODY></HTML>
frontSide.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY bgcolor=cyan><Font size=3>
<% String s=request.getParameter("number");
? ?out.println("<BR>浼???杩??ョ???兼??"+s);
%>
<BR><img src=image/a.JPG width=120 height=120 >?卞??惧??</img>
</FONT></BODY></HTML>
reverseSide.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY bgcolor=cyan><Font size=3>
<% String s=request.getParameter("number");
? ?out.println("浼???杩??ョ???兼??"+s);
%>
<BR> <img src=image/b.JPG width=120 height=120 >?卞??惧??</img>
</FONT></BODY></HTML>
渚?瀛?13
example2_13.jsp锛?
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY>
?<jsp:plugin type="applet" code="B.class" jreversion="1.2" width="200" height="260">
? ? <jsp:fallback>
? ? ? ?Plugin tag OBJECT or EMBED not supported by browser.?
? ? </jsp:fallback>
</jsp:plugin>
</BODY></HTML>