




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
webhttphttpHttpServletRequest對HttpServletResponsehttpURLservletservice方serviceHttpServletResponse對象作為響應serviceweb容器的緩沖區(qū)。web容器會將緩沖區(qū)中的響應消息會送給web客戶端。如最經(jīng)典的404狀態(tài)碼:http1.1404HttpServletResponse提供了兩種寫入消息正文的方式。PrintWriterPrintWriteroutresponse.getWriter();ISO8859-1ISO8859-1并不包含中文編碼,因此如果向消息1、voidsetStatus(int過voidsetStatus(intcodeservlet沒有使用或者不支持輸出緩沖區(qū),那么就一定要在調(diào)用其他可產(chǎn)生客戶端輸出setStrutsservlet使用了輸出緩沖區(qū),也必須在輸出緩沖區(qū)的內(nèi)容尚被實際發(fā)送到客戶端之前調(diào)用setStruts方法設置狀態(tài)碼。2、sendErrorvoidsendError(intvoidsendError(intcode,StringServletResponseHttpServletResponsehttp響應消息中的setIntHeader、setDateHeader等方法。用來設置一些常用的或者日期、整數(shù)數(shù)據(jù)的響應頭。addHeadersetHeader1、addHeardersetHeaderaddHearer與setHeardervoidvoidaddHeader(Stringname,Stringvalue);voidsetHeader(Stringname,Stringvalue);2、addHeadersetHeader點擊Add按鈕彈出如下窗口:packagepackagecn.taotao.servlet;importimportjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;initParams={@WebInitParam(name"name",value陶陶{(diào)ServletException,ServletException,{}}@WebInitParam(name"name",value陶陶其中:urlPatterns@WebInitParam(name"name",value陶陶protectedvoidservice(HttpServletRequestrequest,HttpServletResponseprotectedvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringname=this.getInitParameter("name");}Stringname=this.getInitParameter("name");Stringname=this.getInitParameter("name");這等同于使用response.redirect方法:response.redirect(“newpath/newpage.jsp”)response.setDateHeader("expires",0);response.setDateHeader("expires",0);voidsetIntHeadeR(Stringname,intvalue)publicvoidsetDateHeader(Stringname,long不需調(diào)用此函數(shù)來設置響應實體的大小。Servlet引擎會在向客戶端輸出響應內(nèi)容時自動設setContentTypeservletMIME(multipurposeInternetMailExtension,多用途internet郵件擴展)類型。法來替換setHeader方法的第一參數(shù)為“content-type”的調(diào)用。據(jù)不同的MIME調(diào)用瀏覽器內(nèi)不同的程序嵌入模塊來處理相應的數(shù)據(jù)。webMIMEGIFMIMEjsonxml <atype="application/pdf"href="032016年秋季交叉課課表.pdf">pdftest.htmlWebContent路徑下存儲一個“03信息=if{=if{=}{byte[]b=newbyte[1024];intc= resp.setHeader("Content-Disposition","attachment;filename*=UTF-8''"} }out.write(b,0,}}chengzhendibao.xls來保存或打開響應正文中的response實現(xiàn)文件下載”這一節(jié)中講解。setCharacterEncodinghttp協(xié)議來說,Content-TypeSetvletResponse.getWriter方法返回setContentTypesetContentType設置的字符我們可以利用消息響應頭來指定瀏覽器采用何種字符集顯示響應消息正文。Servlet引口中定義了兩個方法:getOutputStream與getWrite方法。getWriterServlet引擎創(chuàng)建的字符輸出流對象,Servlet程序可以按字符形getOutputStreamServletOutputStream,它可以直write(byte[]b,intbegin,int返回,PrintWriter對象可以直接輸出字符文本內(nèi)容。Servlet程序向ServletOutputStreamPrintWriterServlet引擎獲正文。盡管如此,servletServletOutputStreamPrintWriter對象Content-type頭字段。Serlvetservice方法結束后,ServletgetWritergetOutputStream方法返close方法,如果沒有,Servletclose方法關閉ServletOutputStreamPrintWriter對象之后,調(diào)用close方法,以便盡快釋放相關資源。ServletgetWritePrintWrite對象。同樣{{ServletException,IOException{//TODOAuto-generatedmethodstubPrintWriterout1=response.getWriter();PrintWriterout2=response.getWriter();if(out1=={}{}}}servletHTTP234reset戶端輸出過部分內(nèi)容,這個方法將會拋出IllegalStateException異常。URL地址指向一個文件,用戶單擊該servlet程序僅僅將文件內(nèi)容傳遞給瀏覽器是不HTML文件,而是一個要保存到本地的下載文件。即將內(nèi)容的MIME類型設置為“application/x-msdownload”。代碼如下:2HttpServletResponse.setHeader()方法設置content-disposition頭的值為“attachment:filename=文件名”content-dispositionfileName=.URLEncoder.encode(fileName,"UTF-8");fileName=.URLEncoder.encode(fileName,"UTF-8"); } }Filefile=newFile(filePath);//打開文件FileInputStreamfisnewFileInputStream(file);byte[]b=newbyte[1024];intFilefile=newFile(filePath);//打開文件FileInputStreamfisnewFileInputStream(file);byte[]b=newbyte[1024];intc=fileName=.URLEncoder.encode(fileName,"UTF- } resp.setHeader("content-disposition","attachment;filename="+}{out.write(b,0,}{privatestaticfinallongserialVersionUID={privatestaticfinallongserialVersionUID=ServletException,IOException{=ifif{}{byte[]b=newbyte[1024];intc=法.doc","UTF-8"); response.setHeader("Content-Disposition","attachment;8''"+} }{out.write(b,0,}}}protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,}}(1)不論是文件還是目錄都使用File類操作;的屬性;比如文件名。下面讓我們開始File的學習。File文件名/newFile("文字路徑字符串Java中提供了幾種創(chuàng)建文件及目錄的構造方法,但大體上都是用參數(shù)中的文字路徑字判斷File在StringStringStringStringlonglongpublicvoiddirectory{publicvoiddirectory{FileFilefilenewFile("D:\\我代的課");if(file.isDirectory()){}}publicvoidpublicvoid{FilefilenewFile("D:\\我代的課\\031320123.doc");if(file.isFile()){}}boolean{FilefilenewFile("D:\\我代的課\\{FilefilenewFile("D:\\我代的課\\中國萬歲.doc");if{}{}}newFile(文件名或文件夾名)File對象。然后需要判publicvoid{if(file.exists()){}}String[]File[]publicvoid{FilefilenewFile("D:\\我代的課\\信息工程學院\\計算機系publicvoid{FilefilenewFile("D:\\我代的課\\信息工程學院\\計算機系\\K0315415");}publicpublicvoid{FilefilenewFile("D:\\我代的課");if(file.isDirectory()){for(Stringname:names){}}}listFiles()File對象數(shù)組。這個函數(shù)用的最多。publicpublicvoid{FilefilenewFile("D:\\我代的課");File[]files=file.listFiles();{if{}elseif{Stringfilename=f.getName();}}}publicvoid{Filefile=newFile(string);Scannersin=newSystem.out.println("你確定要刪除string1intc=sin.nextInt();if(c!=1){}}publicvoiddeleteFile(File{if{if{{}elseif{for(Filef:files){}}}}底層操作系統(tǒng)的默認編碼方式,例如GBK等。importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;publicclass{publicstaticvoidmain(String[]{FilereadFile=newFile("D:\\湖北民族學院教材建設管理辦{}FilewriteFilenewFile("D:\\新文件.doc");}catch(IOException{}{
FileInputStreamfIn=newFileInputStream(readFile);FileOutputStreamfOut=newFileOutputStream(writeFile);byte[]b=newbyte[1024];intcount=while((count=fIn.read(b))!=-{fOut.write(b,0,}}catch(IOException{//TODOAuto-generatedcatchblock}}}listimages(包含了頁面中所需要的圖片)文件夾拷貝到項目的webContent文件夾下。再刷新頁面,則會看到正常的頁面了。2)添加處理登錄頁面提交的Servlet""""""""=""=""/>publicpublicclassLoginServletextends ServletException,{ //TODOAuto-generatedmethodStringpassword=request.getParameter("password");application.setAttribute("username",userName);RequestDispatcherrDispatcher=}}webContentpublic文件夾(必須自己創(chuàng)建)Stringpath=Stringpath=StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; noresize="noresize"id="topFrame"title="topFrame"/> noresize="noresize"id="leftFrame"title="leftFrame" title="rightFrame"/>StringStringpath=StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";;getServerPortbasePath的最終rul前都加上<%=basePath%>,以構成一個絕對路徑。避免因為路徑問題而出現(xiàn)資源不存在的404錯誤。另外,可以看到,main.jspframeSetStringpath=StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; $(".navli$(".navli<div title="系統(tǒng)首頁"/></a><ul src="<%=basePath%>images/icon01.png"title="工作臺"/><h2>工作臺</h2></a></li> src="<%=basePath%>images/icon02.png"title="模型管理"/><h2>模型管理</h2></a></li> src="<%=basePath%>images/icon03.png"title="模塊設計"/><h2>模塊設計</h2></a></li> src="<%=basePath%>images/icon04.png"title="常用工具"/><h2>常用工具</h2></a></li> src="<%=basePath%>images/icon05.png"title="文件管理"/><h2>文件管理</h2></a></li> src="<%=basePath%>images/icon06.png"title="系統(tǒng)設置"/><h2>系統(tǒng)設置</h2></a></li><div <divStringpath=StringbasePath=request.getScheme()+"://"+path+"/"; var$ul=}<body<dl<div<ul target="rightFrame">2017年秋上課文件</a><i></i></li><liclass="active"><cite></cite><ahref="right.htmltarget="rightFrame">數(shù)據(jù)列表<li><cite></cite><ahref="<%=basePath%>imglist.html"target="rightFrame">圖片數(shù)" target="rightFrame">圖片列 自定義" 信息管<div<ul編輯器<ul日期管理<ulpackagecn.taotao.pojo;packagecn.taotao.pojo;{publicStringgetName(){return}{=}{return}}{this.type=}}當前路徑為:文檔<ahref="GetFilePathServlet?openFlodeh=${}&curPath=${curPath}"><ahref="GetFilePathServlet?openFlodeh=${}&curPath=${curPath}">根目錄:由web.xml設置得到importjava.io.File;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importcn.taotao.pojo.FileInfomation;publicclassGetFilePathServletextends{privatestaticfinallongserialVersionUID=protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ServletContextapplicationthis.getServletContext();StringrootPath=(String)application.getInitParameter("rootDirectory");Stringextension=(String)application.getInitParameter("extension");StringcurPathrequest.getParameter("curPath");StringopenFloderrequest.getParameter("openFloder");if(null!=curPath){filePath= +}if{filePath=filePath+"\\"+openFloder;if(null==curPath){curPath=}{curPath=curPath+"\\"+}}request.setAttribute("curPath",curPath);Filefile=newFile(filePath);//創(chuàng)建目錄File[]listF=file.listFiles();List<FileInfomation>dl=newArrayList<FileInfomation>();List<FileInfomation>fl=newArrayList<FileInfomation>();if(listF==null||listF.length==0){}{
for(Filef1:listF){"+}StringfileName=f1.getName();Stringprefix=fileName.substring(fileName.lastIndexOf(".")+1);FileInfomationff=newFileInfomation();{}}}request.setAttribute("fl",}}protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,response);}}FILEFileInfomationlistFileInfomationnew。因為list集合中實際上存儲只是這些對象的引用,即對象的地址。jsp的標簽,所以需要將jstl-1.2.jarjarWEB-INFlib目錄下。StringStringpath=StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";<div<ul<li><ahref="#">首頁<li><ahref="#">模塊設計<li><ahref="#">圖片<div<div<ul<ul<ul<li
<div<ul<liclass="paginItemmore"><a<div<div<span><imgsrc="images/ticon.png"<div<div<div<inputname=""type="button"class="sure"value="確定<inputname=""type="button"class="cancel"value="取消importjava.io.File;importjava.io.IOException;importjavax.servlet.RequestDispatcher;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;{privatestaticfinallongserialVersionUID=StringfilePath=rootPath;if{filePath=filePath+curPath+"\\"+}{filePath=filePath+}FilefilenewFile(filePath);//打開文件if(!file.exists()){}{
rDispatcher.forward(request,response);FileInputStreamfis=newFileInputStream(file);byte[]b=newbyte[1024];intc= } }out.write(b,0,}}}doGet(request,response);}}文件路徑,根據(jù)文件路徑和文件名。讀出文件,寫入到ServletOouputStream流中。servlet程序中,有時需要調(diào)用另一個資源來對瀏覽器的請求進行響應,這可以通過HttpServletResponse.sendRedirect方法來實現(xiàn)請求重定向。RequestDispatcherServlet引擎創(chuàng)建,用于包裝一個要被其他資源調(diào)用的資源,include兩種)將請求轉(zhuǎn)發(fā)給所包裝的資源。include(ServletRequestrequest,ServletResponseServletResponseinclude(ServletRequestrequest,ServletResponse序的根目錄。WEB-INFRequestDispatcher也是可見的。所以傳遞給RequestDispatcherwebforwardweb應用程序中每個頁面都有相同的導航菜單,相同的頁面底部。在htmljsp頁面中,然后在其他頁面中用include包含資源來實現(xiàn)的。注意:使用include于導航條的叫head.html的文件。讓另一個名叫IncludeServletservlet包含它。<title>Inserttitle/*Nav.NavM{width:100%;.NavMConulli{height:29px;float:left;padding:11px10px010px;background:url(http://p1.if/ifeng/ifeng-index/20160122/spritesize47_w1000_h2000.png)no-repeat0-100px;}/*160420a*/.NavMConulli.TV{padding:11px0px012px;.NavMConulli.PTV{padding:12px0px012px;background:none; index/20160122/spritesize47_w1000_h2000.png)no-repeat-300px-212px;display:block;.NavMConulli.PTVa{background:url(/ifeng/ifeng-index/20160520/spritesize47_w1000_h2000.png)no-repeat-465px-214px;display:block;padding-left:23px;font-family:Arial;font-size:14px}/*160520a*//*Nav<div<divclass="wrap<ul<!--160420abegin--<!--160420aend--""<!--160520abegin--<liclass="PTV"><ahref="/english.shtml"target="_blank">PHOENIX<!--160520aend--packageimportimportjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importpackageimportimportjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;**ServletimplementationclasspublicclassIncludeServletextends*@seeHttpServlet#service(HttpServletRequestrequest,HttpServletResponseServletException,IOException{//TODOAuto-generatedmethodstubdispatcher.include(request,response);}}htm
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 個人裝修委托書
- 雙方設備合作合同范本
- 南瓜訂貨合同范例
- 斷層解剖復習題+答案
- 合伙買車合作協(xié)議合同范例
- 七年級下學期語文總結
- 兼職游泳教練合同范本
- 保潔合同范本(完美版)
- 廠里員工租房合同范本
- 《贈劉景文》和《山行》古詩的教學反思
- 《老年人權益保障法》
- 2025屆上海市(春秋考)高考英語考綱詞匯對照表清單
- 2025-2030年中國pcb行業(yè)競爭格局及未來投資趨勢分析報告新版
- 2025年年食堂工作總結和年工作計劃例文
- 船舶制造設施安全生產(chǎn)培訓
- 全國駕駛員考試(科目一)考試題庫下載1500道題(中英文對照版本)
- TSG 07-2019電梯安裝修理維護質(zhì)量保證手冊程序文件制度文件表單一整套
- 2025深圳勞動合同下載
- 標準和計量管理制度范文(2篇)
- 孕前口腔護理保健
- 《民航服務與溝通學》課件-第1講 服務與民航服務的概念
評論
0/150
提交評論