使用HttpServletResponse對象獲取請求行信息
HttpServletResponse對象獲取請求行信息
方法列表
String reqMethod = request.getMethod() String reqURI=request.getRequestURI(); String reqURI=request.getRequestURI(); StringBuffer reqURL=request.getRequestURL(); String reqPath=request.getContextPath(); String queryString=request.getQueryString(); String reqClient=request.getRemoteAddr();
實(shí)例
HTML
<!DOCTYPE html> <html> <head> <title>request獲取請求行內(nèi)容</title> <meta charset="utf-8"> </head> <body> <form action="/WEB/lineServlet" method="post" > 姓名:<input type="text" name="name"><br> 年齡:<input type="text" name="age"><br> <input type="submit" value="提交"> </form> </body> </html>
java
package servlet; 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.HttpServletResponse; public class LineServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設(shè)置編碼方式 response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); // 獲得PrintWriter輸出對象 PrintWriter writer = response.getWriter(); // 1、獲取請求方式 String reqMethod = request.getMethod(); writer.write("請求方式method: " + reqMethod); writer.print("<br>"); //2、獲得請求的資源相關(guān)的內(nèi)容 String reqURI=request.getRequestURI(); StringBuffer reqURL=request.getRequestURL(); writer.write("請求內(nèi)容URI: " + reqURI); writer.print("<br>"); writer.write("請求內(nèi)容URL: " + reqURL); writer.print("<br>"); //獲得web應(yīng)用的名稱 String reqPath=request.getContextPath(); writer.write("web應(yīng)用contextPath: " + reqPath); writer.print("<br>"); //地址后的參數(shù)的字符串 String queryString=request.getQueryString(); writer.write("參數(shù)的字符串queryString: " + queryString); writer.print("<br>"); //3、獲得客戶機(jī)的信息---獲得訪問者IP地址 String reqClient=request.getRemoteAddr(); writer.write("客戶機(jī)的信息RemoteAddr: " + reqClient); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
HttpServletResponse和HttpServletRequest解析
最近有這么一個(gè)訴求,我在A服務(wù)器中的java調(diào)用執(zhí)行一個(gè)python文件,并將其返回值返回給B服務(wù)器的客戶端。當(dāng)時(shí)在想A服務(wù)器暴露一個(gè)接口,然后使用runtime API調(diào)用即可,但是返回值這塊我卻有點(diǎn)苦惱。我的本意是直接返回IO流,因?yàn)樯傻臄?shù)據(jù)可大可小,無規(guī)律,但是我發(fā)現(xiàn)這樣是不可以的。
[org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class org.springframework.boot.loader.jar.JarURLConnection$2]
后來兜兜轉(zhuǎn)轉(zhuǎn)發(fā)現(xiàn)了HttpServletResponse 可以來解決這個(gè)問題。
HttpServletResponse和HttpServletRequest
Web服務(wù)器收到客戶端的http請求,會(huì)針對每一次請求,分別創(chuàng)建一個(gè)用于代表請求的request對象、和代表響應(yīng)的response對象。request和response對象即然代表請求和響應(yīng),那我們要獲取客戶機(jī)提交過來的數(shù)據(jù),只需要找request對象就行了。要向客戶機(jī)輸出數(shù)據(jù),只需要找response對象就行了。
本文主講HttpServletResponse,HttpServletRequest碰到合適的場景我會(huì)將他補(bǔ)全。
HttpServletResponse
HttpServletResponse對象代表服務(wù)器的響應(yīng)。這個(gè)對象中封裝了向客戶端發(fā)送數(shù)據(jù)、發(fā)送響應(yīng)頭,發(fā)送響應(yīng)狀態(tài)碼的方法。
他的作用如下
1、保存流對象
使用HttpServletResponse 我們可以將IO流讀出來在寫入HttpServletResponse中的OutputStream中。然后別的客戶端訪問的時(shí)候方便獲取
相關(guān)代碼
@RequestMapping(value = "/testHdf") @ResponseBody public void testHdfs(String csv, String sql, HttpServletResponse response) { BufferedInputStream bis = null; try { Process process = Runtime.getRuntime().exec("python /blazingsql/testData/zgh/aaa.py"); process.waitFor(); bis = new BufferedInputStream(process.getInputStream()); OutputStream os = null; byte[] buff = new byte[1024]; os = response.getOutputStream(); int i = bis.read(buff); while (i != -1) { os.write(buff, 0, buff.length); os.flush(); i = bis.read(buff); } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
2、下載文件
使用HttpServletResponse可以下載文件,很方便
下載文件是,設(shè)置這些參數(shù)很重要
response.setHeader(“Content-Disposition”, “attachment;filename=”+ URLEncoder.encode(“demo.csv”, “UTF-8”)); response.setHeader(“Connection”, “close”); response.setHeader(“Content-Type”, “application/octet-stream”);
相關(guān)代碼
@RequestMapping(value = "/download") @ResponseBody public void download(String path,HttpServletResponse response) throws UnsupportedEncodingException { response.reset(); response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("demo.csv", "UTF-8")); response.setHeader("Connection", "close"); response.setHeader("Content-Type", "application/octet-stream"); OutputStream ops = null; FileInputStream fis =null; byte[] buffer = new byte[8192]; int bytesRead = 0; try { ops = response.getOutputStream(); fis = new FileInputStream(path); while((bytesRead = fis.read(buffer, 0, 8192)) != -1){ ops.write(buffer, 0, bytesRead); } ops.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(fis != null){ fis.close(); } if(ops != null){ ops.close(); } } catch (IOException e) { e.printStackTrace(); } } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中Json與List、Map、entity的互相轉(zhuǎn)化
在開發(fā)中,Json轉(zhuǎn)換的場景往往也就是那么幾個(gè),本文主要介紹了Java中Json與List、Map、entity的互相轉(zhuǎn)化,具有一定的參考價(jià)值,感興趣的可以了解一下2022-07-07Java集合之Set、HashSet、LinkedHashSet和TreeSet深度解析
這篇文章主要介紹了Java集合之Set、HashSet、LinkedHashSet和TreeSet深度解析,List是有序集合的根接口,Set是無序集合的根接口,無序也就意味著元素不重復(fù),更嚴(yán)格地說,Set集合不包含一對元素e1和e2 ,使得e1.equals(e2) ,并且最多一個(gè)空元素,需要的朋友可以參考下2023-09-09在mybatis中使用mapper進(jìn)行if條件判斷
這篇文章主要介紹了在mybatis中使用mapper進(jìn)行if條件判斷,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Java中策略設(shè)計(jì)模式的實(shí)現(xiàn)及應(yīng)用場景
策略設(shè)計(jì)模式是Java中一種常用的設(shè)計(jì)模式,它通過定義一系列算法并將其封裝成獨(dú)立的策略類,從而使得算法可以在不影響客戶端的情況下隨時(shí)切換。策略設(shè)計(jì)模式主要應(yīng)用于系統(tǒng)中存在多種相似的算法、需要靈活調(diào)整算法邏輯或者需要擴(kuò)展新的算法等場景2023-04-04SpringBoot使用Feign調(diào)用其他服務(wù)接口
這篇文章主要介紹了SpringBoot使用Feign調(diào)用其他服務(wù)接口,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Java基礎(chǔ)之?dāng)?shù)組超詳細(xì)知識(shí)總結(jié)
這篇文章主要介紹了Java基礎(chǔ)之?dāng)?shù)組詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05