超通俗易懂的Servlet入门教程
不怕千萬人阻擋,就怕自己投降。
文章目錄
- 01.Servlet快速入門
- 02.Servlet3.0注解配置
- 03.GenericServlet&HttpServlet(Serlvet的體系結(jié)構(gòu))
- 04.HTTP
- 05.Request對(duì)象
- 06.Request案例(登錄)
- 07.Response對(duì)象
- Response案例
- 路徑的分類
- 08.ServletContext對(duì)象
- 09.文件下載案例
概念:運(yùn)行在服務(wù)器端的小程序
servlet就是一個(gè)接口,定義了Java類被瀏覽器訪問到(tomcat識(shí)別)的規(guī)則。將來我們自定義一個(gè)類,實(shí)現(xiàn)servlet接口,復(fù)寫方法。 所以servlet就是實(shí)現(xiàn)了Servlet接口的類。
01.Servlet快速入門
1.創(chuàng)建javaEE項(xiàng)目
2.定義一個(gè)類,實(shí)現(xiàn)servlet接口
public class servletDemo1 implements servlet
3.實(shí)現(xiàn)接口中的抽象方法
4.配置Servlet,在xml中配置。
<servlet> <servlet-name>demo1</ servlet-name> <servlet-class>cn.itcast.web. servlet.ServletDemo1</servlet-class>< / servlet> <servlet-mapping> <servlet-name>demo1</ servlet-name><url-pattern>/demo1</url-pattern>< / servlet-mapping>執(zhí)行原理:
1,當(dāng)服務(wù)器接受到客戶端瀏覽器的請(qǐng)求后,會(huì)解析請(qǐng)求uRL路徑,獲取訪問的servlet的資源路徑2.查找web.xml文件,足否有對(duì)應(yīng)的<url-pattern>標(biāo)簽體內(nèi)容。
3.如果有,則在找到對(duì)應(yīng)的<servlet-class>全類名
4. tomcat會(huì)將字節(jié)碼文件加載進(jìn)內(nèi)存,并且創(chuàng)建其對(duì)象
5,調(diào)用其方法。
servlet中的生命周期方法∶
1.被創(chuàng)建:執(zhí)行init方法,只執(zhí)行一次
對(duì)象被創(chuàng)建后再執(zhí)行Servlet方法。
servlet什么時(shí)候被創(chuàng)建?
默認(rèn)情況下,第一次被訪問時(shí),servlet被創(chuàng)建。可以配置其執(zhí)行servlet的創(chuàng)建時(shí)機(jī)。
在<servlet>標(biāo)簽下配置
1,第一次被訪問時(shí),創(chuàng)建
<load-on-startup>的值為負(fù)數(shù)。
2.在服務(wù)器啟動(dòng)時(shí),創(chuàng)建
<load-on-startup>的值為0或正整數(shù)。
Servlet的init方法,只執(zhí)行一次,說明一個(gè)servlet在內(nèi)存中只存在一個(gè)對(duì)象,Servlet是單例的。
多個(gè)用戶同時(shí)訪問時(shí),可能存在線程安全問題。
解決∶盡量不要在servlet中定義成員變量。即使定義了成員變量,也不要對(duì)修改值。
2.提供服務(wù):執(zhí)行service方法,執(zhí)行多次。
每次訪問Servlet時(shí),Service方法都會(huì)被調(diào)用一次。
3.被銷毀:執(zhí)行destroy方法,只執(zhí)行一次。
servlet被銷毀時(shí)執(zhí)行,服務(wù)器關(guān)閉時(shí),Servlet被銷毀。
只有服務(wù)器正常關(guān)閉時(shí),才會(huì)執(zhí)行destroy方法。
destroy方法在servlet被銷毀之前執(zhí)行,一般用于釋放資源。
02.Servlet3.0注解配置
java6支持Serlet3.0.
java8支持Serlet4.0.
好處:支持注解配置。
可以不需要web.xml。
步驟:
1.創(chuàng)建JavaEE項(xiàng)目,選擇Servelt3.0以上的,可以不創(chuàng)建web.xml。
2.定義一個(gè)類,實(shí)現(xiàn)Servlet接口。
3.復(fù)寫方法。
4.在類上使用@WebServlet注解,進(jìn)行配置。
因?yàn)樽⒔馐羌釉陬惿系?#xff0c;所以不需要關(guān)心類名,只需要關(guān)心資源路徑。
@WebServlet(urlPatterns="/demo")//可以配置多個(gè)路徑 public class ServletDemo implement Servlet{ }或者
@WebServlet("/demo")//可以配置多個(gè)路徑 public class ServletDemo implement Servlet{ }IDEA與tomcat的相關(guān)配置
1.IDEA會(huì)為每一個(gè)tomcat部署的項(xiàng)目單獨(dú)建立一份配置文件.
查看控制臺(tái)的log : Using CATALINA_BASE:“c: \users\fay.Intelli]Idea2018.1\system\tomcatl_itcast"
2.工作空間項(xiàng)目和 tomcat部署的web項(xiàng)目。
tomcat真正訪問的是"“tomcat部署的web項(xiàng)目”",“tomcat部署的web項(xiàng)目"對(duì)應(yīng)著”"工作空間項(xiàng)目”的web目錄下的所有資源。
WEB-INF目錄下的資源不能被瀏覽器直接訪問。
03.GenericServlet&HttpServlet(Serlvet的體系結(jié)構(gòu))
GenericServlet是Servlet的子類(子);
HttpServlet是GenericServlet的子類(孫);
問題提出:有時(shí)候我們只需要重寫Servlet的service方法。然而我們繼承Servlet類必須實(shí)現(xiàn)Servlet的所有抽象方法。這樣就與我們意愿相左。有什么解決辦法呢?
【解決辦法】:GenericServlet為Servlet的子類,并且已經(jīng)(空)實(shí)現(xiàn)了Servlet的4個(gè)方法,只有service方法沒有實(shí)現(xiàn)。
因此我們可以寫一個(gè)類,繼承GenericServlet抽象類。實(shí)現(xiàn)service方法即可。
將來我們?cè)诙x自己的serlvet類時(shí),可以繼承抽象類GenericServlet即可,如果需要使用到其他方法,重些該方法即可。
可以看出,此時(shí)代碼書寫已經(jīng)很方便,但是我們以后開發(fā)并不使用這種方式。
那我們使用哪種方式呢?
【HttpServlet】:為什么要使用這個(gè)類呢?
那我們得問自己,我們實(shí)現(xiàn)service方法干嘛呢?
我們?cè)趯憇ervice方法體時(shí),需要判斷前端給后臺(tái)的請(qǐng)求方式,并根據(jù)其獲取數(shù)據(jù)。每次都需要判斷,有沒有一種方式,可以簡潔的達(dá)到效果呢?
HttpServlet抽象類,幫我們把這些事情都做好了,我們只需要重寫HttpServlet對(duì)應(yīng)得方法即可。
如:
因此我們將來需要屏蔽請(qǐng)求方式的處理邏輯,繼承HttpServlet就顯得格外好用。
HttpSerlvet:對(duì)http協(xié)議的一種封裝,簡化操作。
因此我們以后開發(fā):就不再定義類繼承GenericServlet實(shí)現(xiàn)service方法。而是定義類繼承HttpServlet重寫doGet(),或doPost()方法。
Servlet的urlpartten配置
因?yàn)閡rlpattten是一個(gè)數(shù)組,所以可以為其配置多個(gè)資源路徑
一個(gè)servlet可以設(shè)置多個(gè)訪問路徑。
路徑的定義規(guī)則
04.HTTP
概念:Hyper Text Transfer Protocol超文本協(xié)議。
傳輸協(xié)議:定義了,客戶端和服務(wù)器端通信超時(shí),發(fā)送數(shù)據(jù)的格式。
請(qǐng)求和響應(yīng)一一對(duì)應(yīng)。
無狀態(tài):每次請(qǐng)求之間相互獨(dú)立,不能交互數(shù)據(jù)。
歷史版本:
1.0:每一次請(qǐng)求響應(yīng)都會(huì)建立新的連接(http1.0)
1.1:復(fù)用連接(http1.1)。
請(qǐng)求消息數(shù)據(jù)格式:
1.請(qǐng)求行
請(qǐng)求方式 請(qǐng)求url 請(qǐng)求協(xié)議/版本
GET /index.html HTTP/1.1
HTTP協(xié)議有7種請(qǐng)求方式。
GET:
(1.請(qǐng)求參數(shù)在請(qǐng)求行中,在url后。
(2.請(qǐng)求的url的長度有限制。
(3.不安全
POST:
(1.請(qǐng)求參數(shù)在請(qǐng)求體中。
(2.請(qǐng)求的url的長度沒有限制。
(3.相對(duì)安全
2.請(qǐng)求頭
請(qǐng)求頭名稱:請(qǐng)求頭值
常見的請(qǐng)求頭:
主機(jī)
User-Agent:瀏覽器告訴服務(wù)器,我訪問你使用的瀏覽器版本信息,可以獲取其信息解決瀏覽器兼容性問題。
Accept:告訴服務(wù)器,我可以解析的格式。
Referer:告訴服務(wù)器,當(dāng)前請(qǐng)求從哪里來。
作用:方式其他人盜鏈。統(tǒng)計(jì)工作。
Connection:表示連接可以復(fù)用。
3.請(qǐng)求空行
空行(分割POST的請(qǐng)求頭和請(qǐng)求體)
4.請(qǐng)求體
POST才有請(qǐng)求體,封裝了POST的請(qǐng)求參數(shù)。
響應(yīng)消息數(shù)據(jù)格式:
1.響應(yīng)行
協(xié)議及版本 響應(yīng)狀態(tài)碼 狀態(tài)碼描述
狀態(tài)碼:
1xx:服務(wù)器接收客戶端消息,但沒有接收完成,等待一段時(shí)間后,發(fā)送1xx狀態(tài)碼。
2xx:成功
3xx:302(重定向),304訪問緩存
4xx:客戶端錯(cuò)誤,405沒有對(duì)應(yīng)的方法。(doGet,doPost)
5xx:服務(wù)器端錯(cuò)誤,500代碼錯(cuò)誤,505服務(wù)器不支持客戶端使用的HTTP版本。
2.響應(yīng)頭
頭名稱:值
常見的響應(yīng)頭:
Content-Type:服務(wù)器告訴客戶端本次響應(yīng)體數(shù)據(jù)格式以及編碼格式。
Content-disposition:服務(wù)器告訴客戶端以什么格式打開響應(yīng)體數(shù)據(jù)。
值:in-line默認(rèn)值,在當(dāng)前頁面內(nèi)打開。
attachment:以附件形式打開響應(yīng)體,文件下載。
3.響應(yīng)空行
4.響應(yīng)體
傳輸?shù)臄?shù)據(jù)。
05.Request對(duì)象
1.request對(duì)象繼承體系結(jié)構(gòu):
ServletRequest —接口
繼承
HttpServletRequest —接口
實(shí)現(xiàn)
org.apache.catalina.connector.RequestFacade 類(tomcat實(shí)現(xiàn)了HttpServletRequest接口)
2.request和response的原理
1.request和response對(duì)象是由服務(wù)器創(chuàng)建的。我們只是使用他們。
2.request對(duì)象是來獲取請(qǐng)求消息,response對(duì)象是來設(shè)置響應(yīng)消息。
3.request的功能:
3.1獲取請(qǐng)求消息數(shù)據(jù)
獲取請(qǐng)求行數(shù)據(jù)
**1.1獲取請(qǐng)求方式**String getMethod();//了解即可
**1.2獲取虛擬目錄**String getContextPath();//重要
**1.3獲取Servlet路徑:**String getServletPath();
**1.4獲取get方式請(qǐng)求參數(shù):**String getQueryString();//了解
**1.5獲取請(qǐng)求URI**String getRequestURI(); 如:/day1/demo2
**1.6獲取請(qǐng)求URL**StringBuffer getRequestURL(); 如: http://localhost/day1/demo2
**1.7獲取協(xié)議及版本**String getProtocol();
**1.8獲取客戶機(jī)的IP地址**String getRemoteAddr();
URI:統(tǒng)一資源標(biāo)識(shí)符。(范圍更大)
URL:統(tǒng)一資源定位符。
獲取請(qǐng)求頭數(shù)據(jù)
獲取請(qǐng)求體數(shù)據(jù)
步驟:
1.獲取流對(duì)象
2.再從流對(duì)象中拿數(shù)據(jù)
@WebServlet("/ServletTest3") public class ServletTest3 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//獲取請(qǐng)求參數(shù)//1.獲取字符流BufferedReader br = request.getReader();//讀取數(shù)據(jù)String line = null;while ((line=br.readLine())!=null){System.out.println(line);}}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {} }3.2.其他功能
1.獲取請(qǐng)求參數(shù)的通用方式(get和post都可以)
優(yōu)勢:屏蔽了get和post方法的不同,代碼只需要寫一份,再另一方法中調(diào)用另一個(gè)已經(jīng)實(shí)現(xiàn)的方法即可,滿足get和post請(qǐng)求。
獲取請(qǐng)求參數(shù)中文亂碼的問題處理:
get方式:tomcat8已經(jīng)將get方式亂碼問題解決了。
post方式:會(huì)亂碼。
解決方案:在獲取參數(shù)前,設(shè)置流的編碼。
request.setCharacterEncoding(“utf-8”);
2.請(qǐng)求轉(zhuǎn)發(fā)
一種在服務(wù)器內(nèi)資源跳轉(zhuǎn)的方式。
步驟:
特點(diǎn):
瀏覽器地址欄路徑?jīng)]有發(fā)生變化。
只能訪問當(dāng)前服務(wù)器內(nèi)部資源中。
轉(zhuǎn)發(fā)是一次請(qǐng)求,多個(gè)資源使用同一個(gè)請(qǐng)求。
3.共享數(shù)據(jù)
域?qū)ο?/strong>:一個(gè)有作用范圍的對(duì)象,可以在范圍內(nèi)共享數(shù)據(jù)。
request域:代表一次請(qǐng)求的范圍,一般用于請(qǐng)求轉(zhuǎn)發(fā)的多個(gè)資源中共享數(shù)據(jù)。
方法:
例子:
@WebServlet("/ServletTest4") public class ServletTest4 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("ServletTest4被訪問了");//存儲(chǔ)數(shù)據(jù)到request域中request.setAttribute("name","MengYangchen");RequestDispatcher getquestDispatcher = request.getRequestDispatcher("/ServletTest5");//沒有虛擬路徑(項(xiàng)目)getquestDispatcher.forward(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);} } @WebServlet("/ServletTest5") public class ServletTest5 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("ServletTest5被訪問了");//獲取數(shù)據(jù)Object name = request.getAttribute("name");System.out.println(name);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);} }結(jié)果:
4.獲取ServletContext(對(duì)象)
返回ServletContext對(duì)象
06.Request案例(登錄)
用戶登錄案例需求:
1.編寫login.html登錄頁面
username &password兩個(gè)輸入框
2.使用Druid數(shù)據(jù)庫連接池技術(shù),操作mysql,day14數(shù)據(jù)庫中user表
3.使用JdbcTemplate技術(shù)封裝JDBC
4.登錄成功跳轉(zhuǎn)到SuccessServlet展示:登錄成功!用戶名,歡迎您
5.登錄失敗跳轉(zhuǎn)到FailServlet展示:登錄失敗,用戶名或密碼錯(cuò)誤。
07.Response對(duì)象
1.請(qǐng)求消息:客戶端發(fā)送給服務(wù)器的數(shù)據(jù)。
2.服務(wù)器端發(fā)送給客戶端的數(shù)據(jù)。
功能:設(shè)置響應(yīng)消息。
1.設(shè)置響應(yīng)行。
2.設(shè)置響應(yīng)頭。
void setHeader(String name, String value)3.設(shè)置響應(yīng)體。
獲取輸出流
使用輸出流,將數(shù)據(jù)輸出到客戶端瀏覽器。
Response案例
1.完成重定向
轉(zhuǎn)發(fā)的特點(diǎn):
1.轉(zhuǎn)發(fā)地址欄路徑不變。
2.轉(zhuǎn)發(fā)只能訪問當(dāng)前服務(wù)器下的資源。
3.轉(zhuǎn)發(fā)是一次請(qǐng)求。
4.路徑不需要帶虛擬路徑。
重定向的特點(diǎn):
1.地址欄發(fā)生改變。
2.重定向可以訪問其他站點(diǎn)(服務(wù)器)的資源。
3.重定向是兩次請(qǐng)求。
4.路徑需要虛擬路徑。
2.服務(wù)器輸出字符數(shù)據(jù)到瀏覽器
亂碼原因:編碼解碼用的編碼不同。
2.1.獲取字符輸出流。
//設(shè)置輸出流編碼 response.setCharacterEncoding("utf-8");//告訴瀏覽器,服務(wù)器發(fā)送消息體數(shù)據(jù)的編碼,建議瀏覽器使用該編碼解碼 response.setHeader("content-type","text/html;charset=utf-8");//簡單形式,設(shè)置編碼 response.setContentType("text/html;charset=utf-8"); PrintWrite out = response.getWriter();//tomcat返回的對(duì)象[ISO-8859-1]。2.2輸出數(shù)據(jù)(不需要刷新)
out.write();
3.服務(wù)器輸出字節(jié)數(shù)據(jù)到瀏覽器
3.1獲取字節(jié)輸出流
3.2輸出數(shù)據(jù)
response.setContentType("text/html;charset=utf-8"); ServletOutputStream out = response.getOutputStream(); out.writer("你好".getBytes("utf-8"));4.驗(yàn)證碼
1.本質(zhì):圖片
2.目的:防止惡意表單注冊(cè)。
隨機(jī)生成。
后端:servlet:
前端頁面:
<body> <h1>賬號(hào)注冊(cè)</h1> <form action="/First/ServletTest3" method="post"><input type="text" placeholder="請(qǐng)輸入用戶名" name="username"><br><input type="password" placeholder="請(qǐng)輸入密碼" name="password"><br><input type="submit" value="注冊(cè)"><br><img id="checkCode" src="/First/IdentifyingCodeServlet"/><a id="change" href="">看不清換一張</a> </form><script>//1.給超鏈接或圖片綁定單擊事件//2.重新設(shè)置圖片的src屬性window.onload = function () {var a =document.getElementById("change");//1.獲取圖片對(duì)象var img = document.getElementById("checkCode");//2.綁定單擊事件img.onclick = function () {//加時(shí)間戳var date = new Date().getTime();img.src = "/First/IdentifyingCodeServlet?"+date;//欺騙緩存,因?yàn)槁窂讲蛔?#xff0c;瀏覽器會(huì)自動(dòng)去找緩存,而不是服務(wù)器}a.onclick = function () {var date = new Date().getTime();img.src = "/First/IdentifyingCodeServlet?"+date;//欺騙緩存,因?yàn)槁窂讲蛔?#xff0c;瀏覽器會(huì)自動(dòng)去找緩存,而不是服務(wù)器}}</script> </body>路徑的分類
1.相對(duì)路徑
如:./index.html
不以/開頭,以./開頭的路徑(可以省略不寫)。
規(guī)則:找到當(dāng)前資源和目標(biāo)資源之間的相對(duì)位置關(guān)系。
./表示當(dāng)前目錄;…/上一級(jí)目錄。
2.絕對(duì)路徑
如:http://localhost/First/ServletResponseTest2
簡略寫法:/First/ServletResponseTest2
以/開頭。
規(guī)則:判斷定義的路徑是給誰用的。
1.給客戶端瀏覽器使用:需要加虛擬目錄(超鏈接…項(xiàng)目的訪問路徑)
2.給服務(wù)器端使用(轉(zhuǎn)發(fā)…不需要加虛擬目錄)
【問題提出】在重定向?qū)懧窂绞俏覀儾捎玫氖侵苯訒鴮懧窂?#xff0c;以后一旦更改了虛擬目錄。所有代碼將需要該過來。
【解決方案】動(dòng)態(tài)獲取虛擬目錄。
//動(dòng)態(tài)獲取虛擬目 String contextPath = request.getContextPath();//簡單的重定向方法 response.sendRedirect(contextPath+"ServletResponseTest2");前端代碼采取jsp獲取虛擬目錄。
08.ServletContext對(duì)象
1.概念:代表整個(gè)web應(yīng)用,可以和程序的容器(服務(wù)器)來通信。
2.如何獲取ServletContext對(duì)象。
1.通過request獲取 request.getServletContext();2.通過HttpServlet獲取 this.getServletContext();//因?yàn)槲覀兝^承了HttpServlet兩者獲取的ServletContext是相等的。
2.功能:
獲取MIME類型。
MIME類型:在互聯(lián)網(wǎng)通信過程中定義的一種文件數(shù)據(jù)類型。
格式:大類型/小類型 text/html
域?qū)ο?#xff1a;共享數(shù)據(jù)。
1.setAttribute(String name,Object value)2.getAttribute(String name)3.removeAttribute(String name);ServletContext對(duì)象范圍:所有用戶所有請(qǐng)求的數(shù)據(jù)。
@WebServlet("/ServletContextTest") public class ServletContextTest extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//通過httpServlet獲取ServletContext context = this.getServletContext();//設(shè)置共享數(shù)據(jù)context.setAttribute("name","Is me!");}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);} } @WebServlet("/ServletContextTest2") public class ServletContextTest2 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//通過httpServlet獲取ServletContext context = this.getServletContext();//獲取共享數(shù)據(jù)Object a = context.getAttribute("name");response.setContentType("utf-8");PrintWriter out = response.getWriter();out.print(a);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);} }訪問http://localhost/First/ServletContextTest2得到結(jié)果:
獲取文件的真實(shí)路徑
在web服務(wù)器的真實(shí)路徑。
1.方法:String getRealPath(String path)
配置文件所在地方不同,參數(shù)書寫不同。
String realPath = context.getRealPath("/a.txt");//web目錄下資源訪問 String realPath = context.getRealPath("/WEB-INF/a.txt");//WEB-IN目錄下資源訪問String realPath = context.getRealPath("/WEB-INF/classes/a.txt");//src目錄下資源訪問 @WebServlet("/ServletContextTest3") public class ServletContextTest3 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ServletContext context = this.getServletContext();String realPath = context.getRealPath("/WEB-INF/classes/a.txt");System.out.println(realPath);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);} }09.文件下載案例
文件下載需求
1.頁面顯示超鏈接
2.點(diǎn)擊超鏈接后彈出下載提示框
3.完成圖片文件下載。
分析:
1.超鏈接指向的資源如果能過被瀏覽器解析,則在瀏覽器中顯示,如果不能解析,則彈出下載提示框。不滿足需求。
2.任何資源都必須彈出下載提示框。
3.使用響應(yīng)頭設(shè)置資源的打開方式。
步驟:
1.定義頁面,編輯超鏈接href屬性,指向servlet,傳遞資源名稱filename
2.定義servlet
2.1獲取文件名稱
2.2使用字節(jié)輸入流加載文件進(jìn)內(nèi)存.
2.3指定response的響應(yīng)頭:content-disposition:attachment;filename=xxx.2.4將數(shù)據(jù)寫出到response輸出流。
前端:
<body><p align="center">優(yōu)質(zhì)資源網(wǎng)站</p> <a href="/First/res/img/1.jpg">表情包(未處理)</a><br><a href="/First/ServletDownLoad?filename=1.jpg">表情包</a><br> <a href="/First/ServletDownLoad?filename=2.jpg">表情包2</a> </body> </html>后端:
@WebServlet("/ServletDownLoad") public class ServletDownLoad extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//1.獲取請(qǐng)求參數(shù),文件名稱String filename = request.getParameter("filename");//2.使用字節(jié)輸入流加載文件進(jìn)入內(nèi)存//2.1找到文件服務(wù)器路徑ServletContext context = this.getServletContext();String realPath = context.getRealPath("/res/img/"+filename);//2.2用字節(jié)流關(guān)聯(lián)FileInputStream fis = new FileInputStream(realPath);//3.設(shè)置reponse響應(yīng)頭//設(shè)置響應(yīng)數(shù)據(jù)類型:context-typeString mimeType = context.getMimeType(filename);response.setHeader("content-type",mimeType);//設(shè)置打開方式:content-dispositionresponse.setHeader("content-disposition","attachment;filename="+filename);//4.將輸入流的數(shù)據(jù)寫出到輸出流中ServletOutputStream sos = response.getOutputStream();byte[] buff = new byte[1024*4];int len =0;while ((len=fis.read(buff))!=-1){sos.write(buff,0,len);}fis.close();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);} }中文文件名的問題
【解決思路】
1.獲取客戶端使用的瀏覽器版本信息
2.根據(jù)不同的版本信息,設(shè)置filename不同的編碼方式。
別害怕顧慮,想到就去做,這世界就是這樣,當(dāng)你把不敢去實(shí)現(xiàn)夢想的時(shí)候夢想就會(huì)離你越來越遠(yuǎn),當(dāng)你勇敢地去追夢的時(shí)候,全世界都會(huì)來幫你。
總結(jié)
以上是生活随笔為你收集整理的超通俗易懂的Servlet入门教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opengles绘制可旋转的六角星
- 下一篇: JSPatch使用篇