JavaWeb應(yīng)用開(kāi)發(fā)實(shí)驗(yàn)報(bào)告-實(shí)驗(yàn)四-Servlet的創(chuàng)建和配置_第1頁(yè)
JavaWeb應(yīng)用開(kāi)發(fā)實(shí)驗(yàn)報(bào)告-實(shí)驗(yàn)四-Servlet的創(chuàng)建和配置_第2頁(yè)
JavaWeb應(yīng)用開(kāi)發(fā)實(shí)驗(yàn)報(bào)告-實(shí)驗(yàn)四-Servlet的創(chuàng)建和配置_第3頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余1頁(yè)可下載查看

下載本文檔

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

文檔簡(jiǎn)介

實(shí)驗(yàn)四Servlet的創(chuàng)建和配置一、實(shí)驗(yàn)?zāi)康?、掌握Eclipse下JavaWeb項(xiàng)目的結(jié)構(gòu)。2、掌握編寫(xiě)Servlet和兩種配置方式。3、掌握使用Servlet實(shí)現(xiàn)接收請(qǐng)求參數(shù)。二、實(shí)驗(yàn)環(huán)境PC系列微機(jī):CPU2G以上,內(nèi)存2G以上,硬盤(pán)可以空間:5G以上。操作系統(tǒng):Windows10。三、實(shí)驗(yàn)步驟具體內(nèi)容參考教材《JavaWeb應(yīng)用開(kāi)發(fā)基礎(chǔ)教程》第3章。1、在Eclipse中創(chuàng)建JavaWeb項(xiàng)目;在JavaWeb項(xiàng)目中創(chuàng)建HelloServlet并使用web.xml配置HelloServlet;部署JavaWeb項(xiàng)目到Tomcat,測(cè)試訪(fǎng)問(wèn)HelloServlet。2、在Eclipse中創(chuàng)建另一個(gè)JavaWeb項(xiàng)目;在JavaWeb項(xiàng)目中創(chuàng)建HelloServlet并使用注解配置HelloServlet;部署JavaWeb項(xiàng)目到Tomcat,測(cè)試訪(fǎng)問(wèn)HelloServlet。3、使用創(chuàng)建的第二個(gè)項(xiàng)目,編寫(xiě)RegisterServlet接收用戶(hù)注冊(cè)信息。四、實(shí)驗(yàn)報(bào)告1、請(qǐng)寫(xiě)出Eclipse中創(chuàng)建JavaWeb項(xiàng)目的詳細(xì)步驟。在Eclipse菜單欄單擊File—>New—>Project—>Web—>DynamicWebProject,單擊Next按鈕后,在彈出的NewDynamicWebProject窗口輸入名稱(chēng)ch03,單擊Finish按鈕完成項(xiàng)目創(chuàng)建工作。2、請(qǐng)寫(xiě)出HelloServlet的完整代碼和web.xml中配置該Servlet的關(guān)鍵代碼。①HelloServlet的完整代碼:packagecom.test.servlet.HelloServlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassHelloServletextendsHttpServlet{ protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doPost(request,response); } protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //設(shè)置響應(yīng)類(lèi)型 response.setContentType("text/html"); //通過(guò)response對(duì)象獲得PrintWriter的對(duì)象out PrintWriterout=response.getWriter(); //使用out輸出一個(gè)HTML頁(yè)面 out.println("<html><head><title>HelloWorld</title></head>"+"<body><h1>HelloWorld!</h1></body></html>"); }}②web.xml中配置該Servlet的關(guān)鍵代碼:<servlet><servlet-name>HelloServlet</servlet-name><servlet-class>com.test.servlet.HelloServlet.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>HelloServlet</servlet-name><url-pattern>/HelloServlet</url-pattern></servlet-mapping>3、請(qǐng)寫(xiě)出使用注解配置HelloServlet類(lèi)的完整代碼。packagecom.test.servlet.HelloServlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@WebServlet(name="HelloServlet",urlPatterns={"/HelloServlet"})publicclassHelloServletextendsHttpServlet{ privatestaticfinallongserialVersionUID=1L;publicHelloServlet(){super();} protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doPost(request,response); } protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //設(shè)置響應(yīng)類(lèi)型 response.setContentType("text/html"); //通過(guò)response對(duì)象獲得PrintWriter的對(duì)象out PrintWriterout=response.getWriter(); //使用out輸出一個(gè)HTML頁(yè)面 out.println("<html><head><title>HelloWorld</title></head>"+"<body><h1>HelloWorld!</h1></body></html>"); }}4、請(qǐng)寫(xiě)出用戶(hù)注冊(cè)頁(yè)面register.html的關(guān)鍵代碼和RegisterServlet的完整代碼。①用戶(hù)注冊(cè)頁(yè)面register.html的關(guān)鍵代碼: <center> <formaction="Register"method="post"> 用戶(hù)注冊(cè)<br> 姓名:<inputtype="text"name="name"><br> 密碼:<inputtype="password"name="password"><br> 年齡:<inputtype="text"name="age"><br> <inputtype="submit"value="注冊(cè)"> <inputtype="reset"value="重置"> </form> </center>②RegisterServlet的完整代碼:packageservlet;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@WebServlet("/register/Register")publicclassRegisterServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ request.setCharacterEncoding("UTF-8"); //接受register.html的注冊(cè)信息 Stringname=request.getParameter("name"); Stringpassword=request.getParameter("password"); Stringsage=request.getParameter("age"); //在控制臺(tái)打印輸出注冊(cè)信息 System.out.prin

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論