![實(shí)驗(yàn)2創(chuàng)建和發(fā)布Web應(yīng)用程序_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/4/fd659545-28fa-473a-b976-37a74b4795b7/fd659545-28fa-473a-b976-37a74b4795b71.gif)
![實(shí)驗(yàn)2創(chuàng)建和發(fā)布Web應(yīng)用程序_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/4/fd659545-28fa-473a-b976-37a74b4795b7/fd659545-28fa-473a-b976-37a74b4795b72.gif)
![實(shí)驗(yàn)2創(chuàng)建和發(fā)布Web應(yīng)用程序_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/4/fd659545-28fa-473a-b976-37a74b4795b7/fd659545-28fa-473a-b976-37a74b4795b73.gif)
![實(shí)驗(yàn)2創(chuàng)建和發(fā)布Web應(yīng)用程序_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/4/fd659545-28fa-473a-b976-37a74b4795b7/fd659545-28fa-473a-b976-37a74b4795b74.gif)
![實(shí)驗(yàn)2創(chuàng)建和發(fā)布Web應(yīng)用程序_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-11/4/fd659545-28fa-473a-b976-37a74b4795b7/fd659545-28fa-473a-b976-37a74b4795b75.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)二 創(chuàng)建和發(fā)布Web應(yīng)用程序一、實(shí)驗(yàn)?zāi)康膭?dòng)態(tài)Web很重要的一部分內(nèi)容就是前臺(tái)實(shí)現(xiàn)人機(jī)交互,在后臺(tái)完成業(yè)務(wù)邏輯的運(yùn)算。本實(shí)驗(yàn)驗(yàn)證前后臺(tái)數(shù)據(jù)傳遞的兩種常用機(jī)制,進(jìn)一步熟悉JSP結(jié)構(gòu),掌握后臺(tái)響應(yīng)進(jìn)行動(dòng)態(tài)處理的方法。更常用的后臺(tái)響應(yīng)程序用Servlet。Servlet是純Java程序,但也可以輸出HTML靜態(tài)代碼。通過本實(shí)驗(yàn),掌握Servlet的編程結(jié)構(gòu),理解Servlet生命周期。有關(guān)Servlet的更高級(jí)應(yīng)用請(qǐng)參閱相關(guān)教材。以下選做:JDBC是Java訪問數(shù)據(jù)庫的基礎(chǔ)組件。通過本實(shí)驗(yàn),掌握J(rèn)DBC連接數(shù)據(jù)庫的方法和步驟;掌握數(shù)據(jù)訪問的方法;了解不同數(shù)據(jù)庫和不同提供商提供的驅(qū)動(dòng)程序;二、實(shí)驗(yàn)內(nèi)
2、容1用JSP的內(nèi)置對(duì)象request在前后臺(tái)頁面間傳遞表單數(shù)據(jù);2用置對(duì)象JavaBean在前后臺(tái)頁面間傳遞表單數(shù)據(jù);3驗(yàn)證是否對(duì)數(shù)值、西文、中文字符都能正常傳遞;4用Servlet作后臺(tái)響應(yīng)程序,注意與JSP程序結(jié)構(gòu)有何不同?5掌握在MyEclipse開發(fā)環(huán)境下正確創(chuàng)建Servlet程序的過程;6理解部署描述文件web.xml的作用;以下選做:1以MS SQL Server2005為數(shù)據(jù)庫,用微軟提供的JDBC驅(qū)動(dòng)建立連接。 2先用Application程序驗(yàn)證將數(shù)據(jù)插入數(shù)據(jù)庫表,然后從數(shù)據(jù)庫表中讀回?cái)?shù)據(jù)驗(yàn)證剛插入的數(shù)據(jù)是否正確;3然后用Web程序?qū)崿F(xiàn)form表單界面輸入的數(shù)據(jù)插入數(shù)據(jù)庫表,
3、然后讀回?cái)?shù)據(jù)驗(yàn)證插入數(shù)據(jù)是否正確;三、實(shí)驗(yàn)方法和步驟=第一部分:利用JSP+BEAN實(shí)現(xiàn)如下圖所示的留言板:1用值對(duì)象JavaBean 實(shí)現(xiàn)頁面間的參數(shù)傳遞 (1)創(chuàng)建值對(duì)象JavaBean ,用以存儲(chǔ)單條留言,包括標(biāo)題、留言內(nèi)容:/WordBean.javapackage examples;public class WordBean private String wordTitle;private String wordContent;public String getWordTitle() return wordTitle;public void setWordTitle(String w
4、ordTitle) this.wordTitle = wordTitle;public String getWordContent() return wordContent;public void setWordContent(String wordContent) this.wordContent = wordContent;(2)創(chuàng)建值對(duì)象JavaBean,用以存儲(chǔ)多條留言,使用ArrayList存儲(chǔ) : /WordsListBean.javapackage examples;import java.util.ArrayList;import java.util.List;public c
5、lass WordsListBean private List words;public WordsListBean()words=new ArrayList();public boolean addWord(WordBean word)words.add(word);return true;public List getWords() return words;public void setWords(List words) this.words = words;(3)創(chuàng)建前臺(tái)主界面WordUseBean.jsp<% page language="java" pag
6、eEncoding="UTF-8"%><jsp:useBean id="wordbean" class="examples.WordBean" scope="page"></jsp:useBean><jsp:useBean id="wordsbean" class="examples.WordsListBean" scope="application"></jsp:useBean><% reque
7、st.setCharacterEncoding("UTF-8"); %><jsp:setProperty name="wordbean" property="*" /> <%String path = request.getContextPath();String basePath = request.getScheme()+":/"+request.getServerName()+":"+request.getServerPort()+path+"/"
8、;%><!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN"><html> <head> <base href="<%=basePath%>"> <% if(wordbean.getWordTitle()!=null&&wordbean.getWordContent()!=null) wordsbean.getWords().add(wordbean); %> <table border=&q
9、uot;1" cellspacing="0"> <tr> <th>留言標(biāo)題</th><th>留言內(nèi)容</th></tr> <% for(int i=0;i<wordsbean.getWords().size();i+) examples.WordBean word=(examples.WordBean)wordsbean.getWords().get(i); %> <tr> <td><%=word.getWordTitle() %>&
10、lt;/td><td><%=word.getWordContent() %></td> </tr> <% %> </table> <body> <form name="f1" id="f1" action="" method="post" onSubmit="return checkForm();"> <table border="0"> <tr> &
11、lt;td>標(biāo)題:</td> <td><input type="text" name="wordTitle" id="login"></td> </tr> <tr> <td>內(nèi)容:</td> <td><textarea name="wordContent" id="password"></textarea></td> </tr> <
12、;tr> <td colspan="2" align="center"><input type="Submit" value="提交留言內(nèi)容"></td> </tr> </table> </form> </body> <script language="Javascript"> function checkForm()if (document.all.wordTitle.value='&
13、#39;)alert("對(duì)不起,留言標(biāo)題不能為空!");return false;else if (document.all.wordContent.value='')alert("對(duì)不起,留言內(nèi)容不能為空!");return false;elsereturn true; </script></html>【在線思考】與通過內(nèi)置對(duì)象request獲取請(qǐng)求數(shù)據(jù)相比,WordUseBean.jsp頁面增加了什么類型的標(biāo)簽?分析標(biāo)簽里的內(nèi)容和作用。=第二部分:利用JSP+BEAN+SERVLET實(shí)現(xiàn)如下圖所示的留言板:2用
14、Servlet實(shí)現(xiàn)頁面間的參數(shù)傳遞 (1)創(chuàng)建WordUseServlet.jsp,內(nèi)容如下:<% page language="java" import="java.util.*" pageEncoding="UTF-8"%><%page import="examples.WordsListBean"%><jsp:useBean id="wordsbean" class="examples.WordsListBean" scope="
15、application"></jsp:useBean><%String path = request.getContextPath();String basePath = request.getScheme()+":/"+request.getServerName()+":"+request.getServerPort()+path+"/"%><!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN">&
16、lt;html> <head> <base href="<%=basePath%>"> <table border="1" cellspacing="0"> <tr> <th>留言標(biāo)題</th><th>留言內(nèi)容</th></tr> <% for(int i=0;i<wordsbean.getWords().size();i+) examples.WordBean word=(examples.Wor
17、dBean)wordsbean.getWords().get(i); %> <tr> <td><%=word.getWordTitle() %></td><td><%=word.getWordContent() %></td> </tr> <% %> </table> <body> word added:<br> $word.wordTitle <br> $word.wordContent <br> <form na
18、me="f1" id="f1" action="/servlet/AddWordServlet" method="post" onSubmit="return checkForm();"> <table border="0"> <tr> <td>標(biāo)題:</td> <td><input type="text" name="wordTitle" id="log
19、in"></td> </tr> <tr> <td>內(nèi)容:</td> <td><textarea name="wordContent" id="password"></textarea></td> </tr> <tr> <td colspan="2" align="center"><input type="Submit" >&l
20、t;/td> </tr> </table> </form> </body> <script language="Javascript"> function checkForm()if (document.all.wordTitle.value='')alert("對(duì)不起,留言標(biāo)題不能為空!");return false;else if (document.all.wordContent.value='')alert("對(duì)不起,留言內(nèi)容不能為空!&q
21、uot;);return false;elsereturn true; </script></html>(2)MyEclipse創(chuàng)建Servlet文件向?qū)нx定當(dāng)前工程擊右鍵,“新建”“Servlet”進(jìn)入圖4.1窗口,按提示輸入文件名,選定要?jiǎng)?chuàng)建的方法。點(diǎn)擊“下一步”進(jìn)入圖4.2 確定web.xml描述文件中對(duì)Servlet的描述,請(qǐng)見web.xml。這里的Servlet jsp mapping URL是確定Servlet網(wǎng)絡(luò)訪問的映射名字,字符要盡量簡單。按“完成”生成Servlet代碼框架。業(yè)務(wù)邏輯代碼在doPost()方法中。圖4.1 創(chuàng)建Servlet窗口圖4.
22、2 確定Servlet 在web.xml中的描述(3)驗(yàn)證Servlet能正確地得到Form數(shù)據(jù)和輸出HTML文檔。 AddWordServlet.java如下:package examples;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.H
23、ttpServletResponse;public class AddWordServlet extends HttpServlet /* * Constructor of the object. */public AddWordServlet() super();/* * Destruction of the servlet. <br> */public void destroy() super.destroy(); / Just puts "destroy" string in log/ Put your code herepublic void doGet
24、(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException doPost(request,response);public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException request.setCharacterEncoding("UTF-8");WordBean word=new W
25、ordBean();word.setWordTitle(request.getParameter("wordTitle");word.setWordContent(request.getParameter("wordContent");WordsListBean wordsbean=(WordsListBean)getServletContext().getAttribute("wordsbean");/在com.un.web.MyListener中初始化wordsbean。if(wordsbean!=null)wordsbean.a
26、ddWord(word);request.setAttribute("word", word);getServletContext().getRequestDispatcher("/WordUseServlet.jsp").forward(request,response);public void init() throws ServletException / Put your code here(4)修改部署描述文件web.xml,添加如下內(nèi)容: <servlet> <servlet-name>AddWordServlet&l
27、t;/servlet-name> <servlet-class>examples.AddWordServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AddWordServlet</servlet-name> <url-pattern>/servlet/AddWordServlet</url-pattern> </servlet-mapping>【在線思考】l 請(qǐng)?jiān)谇芭_(tái)分別輸入數(shù)值、西文、中文字符串,后臺(tái)是否得到正確的結(jié)果?l 請(qǐng)注釋掉語句request.setCharacterEncoding("UTF-8");重復(fù)上面的輸入,你會(huì)看到什么現(xiàn)象?你會(huì)得到什么結(jié)論?3(選做)使用JDBC連接數(shù)據(jù)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 10816-2024紫砂陶器
- TAT-PEG-Cy3-生命科學(xué)試劑-MCE-8780
- O-Methylcassythine-生命科學(xué)試劑-MCE-5707
- 1-2-Distearoyl-3-palmitoyl-rac-glycerol-1-2-Stearin-3-palmitin-生命科學(xué)試劑-MCE-3544
- 2025年度解除競業(yè)限制協(xié)議通知范本及注意事項(xiàng)
- 二零二五年度版果園承包合同:果業(yè)人才培養(yǎng)與引進(jìn)合作協(xié)議
- 二零二五年度2025年度自愿調(diào)解協(xié)議書-知識(shí)產(chǎn)權(quán)侵權(quán)糾紛調(diào)解協(xié)議書
- 2025年度共享汽車使用權(quán)授權(quán)管理協(xié)議
- 二零二五年度房屋租賃合同終止及換房新約
- 施工現(xiàn)場(chǎng)管理的方法
- 輸變電工程監(jiān)督檢查標(biāo)準(zhǔn)化清單-質(zhì)監(jiān)站檢查
- 2024-2025學(xué)年北京海淀區(qū)高二(上)期末生物試卷(含答案)
- 【超星學(xué)習(xí)通】馬克思主義基本原理(南開大學(xué))爾雅章節(jié)測(cè)試網(wǎng)課答案
- 2024年中國工業(yè)涂料行業(yè)發(fā)展現(xiàn)狀、市場(chǎng)前景、投資方向分析報(bào)告(智研咨詢發(fā)布)
- 化工企業(yè)重大事故隱患判定標(biāo)準(zhǔn)培訓(xùn)考試卷(后附答案)
- 工傷賠償授權(quán)委托書范例
- 食堂餐具炊具供貨服務(wù)方案
- 員工安全健康手冊(cè)
- 自然科學(xué)基礎(chǔ)(小學(xué)教育專業(yè))全套教學(xué)課件
- 華為客服制度
- 醫(yī)美面部抗衰老注射項(xiàng)目培訓(xùn)課件
評(píng)論
0/150
提交評(píng)論