實驗三 JSP內(nèi)置對象_第1頁
實驗三 JSP內(nèi)置對象_第2頁
實驗三 JSP內(nèi)置對象_第3頁
實驗三 JSP內(nèi)置對象_第4頁
實驗三 JSP內(nèi)置對象_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、實驗三 JSP內(nèi)置對象一實驗?zāi)康?1) 了解JSP中9個內(nèi)置對象request、reponse、 out、 session、 application、 config、 pagecontext、 page、 exception的基本概念。(2) 理解JSP內(nèi)置對象的常用方法的功能。(3) 掌握J(rèn)SP內(nèi)置對象的應(yīng)用。二. 主要儀器設(shè)備及環(huán)境 (1)PC機 (2)操作系統(tǒng): (3)軟件:記事本, JDK、TOMCAT,IE三. 實驗內(nèi)容與步驟1用JSP的內(nèi)置對象request在前后臺頁面間傳遞表單數(shù)據(jù);用內(nèi)置對象前后臺頁面間的參數(shù)傳遞(1)客戶端主界面experiment3_1.jsp,表單封裝了三

2、個參數(shù):<% page language="java" import="java.util.*" pageEncoding="GBK"%><% page contentType="text/html;charset=GBK"%><html> <head><title>這是第一個JSP</title></head> <body> 大家好!恭喜你的第一個JSP <br> <form name="s

3、ubmitt" action="experiment3_2.jsp" method="post" > 第一個數(shù):<input type="text" name="a" value="0" ><br> 第二個數(shù):<input type="text" name="b" value="0" ><br> 第三個數(shù):<input type="text" na

4、me="c" value="0" ><br> <input type="submit" value="提交"> <input type="reset" value="重填"> </form></body></html>(2) 第一個后臺響應(yīng)experiment3_2.jsp,用request對象提取前臺傳過來的參數(shù),驗證輸入的數(shù)據(jù)a、b、c分別是西文、數(shù)字、中文:<% page languag

5、e="java" import="java.util.*" pageEncoding="GBK"%><%!String s1,s2,s3;%><% s1=request.getParameter("a"); s2=request.getParameter("b"); s3=request.getParameter("c"); %><html> <body> This is experiment3_2.jsp page<

6、;br> 前臺傳過來的a=<%=s1%><br>前臺傳過來的b=<%=s2%><br>前臺傳過來的c=<%=s3%><br> a+b+c=<%=s1+s2+s3%><br> <a href="Experiment3_1.jsp">返回</a><br> </body></html>思考l 請檢查結(jié)果都正確嗎?尤其注意傳輸中文字符時是否為亂碼?如果出現(xiàn)亂碼,如何修改代碼?可用書上介紹的方法,也可request.setC

7、haracterEncoding("GBK");答:沒有出現(xiàn)亂碼,只是experiment3_2.jsp的計算為字符串l s1+s2+s3是什么運算?答:字符串運算(3)第二個響應(yīng)界面experiment3_3.jsp,用request對象提取前臺傳過來的參數(shù),進行運算后將其結(jié)果顯示給客戶端:<% page language="java" import="java.util.*" pageEncoding="GBK"%><%!int x,y,z;%><%x=Integer.valueO

8、f(request.getParameter("a").intValue(); y=Integer.valueOf(request.getParameter("b").intValue(); z=Integer.valueOf(request.getParameter("c").intValue(); %><html> <body>從Experiment3_1.jsp傳過來的參數(shù)求得體積: <br> 用request:<%=x*y*z%><br> <a href=

9、"Experiment3_1.jsp">返回</a><br> </body></html>思考l 修改Experiment3_1.jsp什么地方才能響應(yīng)experiment3_3.jsp?答:在action那里修改,如下圖所示:l 為什么三個數(shù)據(jù)都用Integer.valueOf(request.getParameter("a").intValue();語句處理? 答:因為三個數(shù)據(jù)都要轉(zhuǎn)換成整數(shù)l 請在前臺輸入含有非數(shù)字字符數(shù)據(jù),你看到什么結(jié)果?為什么? 答:會出現(xiàn)一下圖的結(jié)果,因為強制轉(zhuǎn)換為數(shù)字類型

10、,不能為字符型進行計算。購物車使用session對象模擬購物車(1)寫一個experiment3_4.jsp文件,用表單輸入用戶姓名。如下圖: 代碼:<% page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%String path = request.getContextPath();String basePath = request.getScheme()+":/"+request.ge

11、tServerName()+":"+request.getServerPort()+path+"/"%><%session.invalidate();%> <%-銷毀所有session對象-%><!DOCTYPE html PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN" "/TR/html4/loose.dtd"><html><head><meta http-equiv=

12、"Content-Type" content="text/html; charset=GBK"><title>用戶登錄</title></head><body><table width="380" border="0" align="center" background="#FFFF00" action="expriment3_5.jsp"> <tr> <td width=

13、"200" height="100">歡迎來到本頁面,請輸入你的姓名</td> </tr> <tr> <td height="50"><input type="text" name="realname"></td> <td width="100"><input type="submit" name="Submit" value="送

14、出" ></td> </tr></table></body></html>(2)另寫experiment3_5.jsp,用表單讓用戶選擇書籍。如下圖代碼:<% page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%><%String path = request.getContextPath();String basePath = reques

15、t.getScheme()+":/"+request.getServerName()+":"+request.getServerPort()+path+"/"%><!DOCTYPE html PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN" "/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type

16、" content="text/html; charset=GBK"><title>購物車</title></head><body><table name="table1" method="post" action="experiment3_6.jsp" ><% request.setCharacterEncoding("gb2312"); String name = request.getParameter(&q

17、uot;name"); if(name != null) session.setAttribute("sessionname",name); out.println(name+",歡迎您的進入!"); %><tr> <td width="800" height="50">點擊超鏈接,連接到main.jsp的頁面,去修改姓名。 <a href="experiment3_4.jsp">歡迎去main.jsp!</a></td>

18、; </tr> <tr> <td width="200" height="50">請選擇你要購買的書籍:</td> </tr> <tr> <td height="50" width="400"> <input name="book1" type="checkbox" value="Java教程" >Java教程 <input name="book

19、2" type="checkbox" value="數(shù)據(jù)庫原理">數(shù)據(jù)庫原理 <input name="book3" type="checkbox" value="操作系統(tǒng)">操作系統(tǒng) <input name="book4" type="checkbox" value="C語言教程">C語言教程</td> </tr> <tr> <td width=&quo

20、t;100"><input type="submit" name="Submit" value="提交"></td> </tr> <tr> <td width="200" height="50">去結(jié)賬: <a href="experiment3_6.jsp">歡迎去count.jsp!</a></td> </tr></table></

21、body></html>(3)最后寫experiment3_6.jsp顯示用戶名和選擇的書籍。如下圖代碼:<% page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%><!DOCTYPE html PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN" "/TR/html4/loose.dtd"&

22、gt;<html><head><meta http-equiv="Content-Type" content="text/html; charset=GBK"><title>顯示購物信息</title></head><body> <table > <tr> <td width="500" height="50">這里是結(jié)賬處,你的姓名以及你選擇的書籍:</td> </tr>

23、 <tr> <% request.setCharacterEncoding("gb2312"); String realname = request.getParameter("realname"); if( null !=session.getAttribute("realname") realname=(String)session.getAttribute("realname"); session.setAttribute("realname",realname); ou

24、t.println("你的姓名:"+realname); String book = request.getParameter("book"); if( null !=session.getAttribute("book") book=(String)session.getAttribute("book"); session.setAttribute("book",book); out.println("購物車中的商品:"+book); %> </tr> &

25、lt;tr> <td width="500" height="50">鏈接到book.jsp頁面,繼續(xù)購買書籍:<a href="experiment3_5.jsp">歡迎到book.jsp!</a></td></tr> <tr> <td width="500" height="50">鏈接到main.jsp頁面,繼續(xù)購買書籍:<a href="experiment3_4.jsp"

26、>歡迎到main.jsp!</a></td></tr></table></body></html>留言板使用application對象。P67(1) 編寫一個experiment3_7.jsp 頁面,用于提交客戶的姓名,留言標(biāo)題和留言內(nèi)容。如下圖:代碼:<% page contentType="text/html;charset=GB2312" %> <% page import="application.MessBoard"%> <jsp:use

27、Bean id="board" class="application.MessBoard" scope="application"/> <HTML> <body> <form action="" method="post" name="form"> 輸入您的名字:<br><input type="text" name="name"> <br>輸入您的留言標(biāo)題:<br><input type="text" name="title"><br>輸入您的留言:<br> <textarea name="content" rows="10" cols=36 wrap=&quo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論