最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaWeb入門:HttpResponse和HttpRequest詳解

 更新時(shí)間:2021年07月16日 16:35:18   作者:寧在春  
這篇文章主要介紹了Django的HttpRequest和HttpResponse對(duì)象,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下

HttpResponse 講解

HttpServletResponse概述:

​ 在創(chuàng)建Servlet時(shí)會(huì)覆蓋service()方法,或doGet()/doPost(),這些方法都有兩個(gè)參數(shù),一個(gè)為代表請(qǐng)求的request和代表響應(yīng)response。service方法中的response的類型是ServletResponse,而doGet/doPost方法的response的類型是HttpServletResponse,HttpServletResponse是ServletResponse的子接口,功能和方法更加強(qiáng)大。

Response運(yùn)行流程

img

響應(yīng)頭有很多這里只介紹常用的。

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-bAvaDBGw-1620739367741)(C:\Users\ASUS\Desktop\JavaWeb_study\JavaWeb\JavaWeb.assets\image-20210511163256716.png)]

在瀏覽器可以按F12 抓包看響應(yīng)頭、請(qǐng)求頭、具體的可以再查。

設(shè)置響應(yīng)行

設(shè)置響應(yīng)的狀態(tài)碼

/**
 * @Author: crush
 * @Date: 2021-05-09 19:35
 * version 1.0
 */
@WebServlet("/test3")
public class HttpResponseTest3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("設(shè)置狀態(tài)碼,前臺(tái)通過(guò)判斷狀態(tài)碼,來(lái)判斷請(qǐng)求是否成功");
        resp.setStatus(404);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

測(cè)試

在這里插入圖片描述

設(shè)置響應(yīng)頭

刷新 跳轉(zhuǎn)頁(yè)面

/**
 * @Author: crush
 * @Date: 2021-05-09 19:35
 * version 1.0
 */
@WebServlet("/test4")
public class HttpResponseTest4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 設(shè)置響應(yīng)頭 每1秒自動(dòng)刷新
        System.out.println("設(shè)置響應(yīng)頭 每1秒自動(dòng)刷新");
        resp.setHeader("Refresh", "1");
        //定時(shí)跳轉(zhuǎn) 3秒后將自動(dòng)跳轉(zhuǎn)
//        resp.setHeader("Refresh","3;URL=hello.jsp");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

測(cè)試

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-LGJQhKor-1620739367743)(C:\Users\ASUS\Desktop\JavaWeb_study\JavaWeb\JavaWeb.assets\image-20210511162434487.png)]

一個(gè)小demo 實(shí)現(xiàn)刷新 頁(yè)面累加

/**
 * response
 * @author Adimi
 */
@WebServlet("/test4")
public class ResponseTest4 extends HttpServlet {
    private static Integer id=1;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Refresh","1");
        PrintWriter writer = response.getWriter();
        id++;
        writer.print("id==>"+id);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

設(shè)置字符編碼 解決中文亂碼問(wèn)題

/**
 * @Author: crush
 * @Date: 2021-05-09 19:35
 * version 1.0
 */
@WebServlet("/test1")
public class HttpResponseTest1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解決中文亂碼問(wèn)題
        // 設(shè)置響應(yīng)頭
        // 設(shè)置字符編碼 resp.setCharacterEncoding("UTF-8");
        // 設(shè)置響應(yīng)內(nèi)容以什么格式展示到頁(yè)面 什么編碼格式 包含了設(shè)置字符編碼
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print("中國(guó),你好?。。?);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

測(cè)試

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-5VCR2LeF-1620739367746)(C:\Users\ASUS\Desktop\JavaWeb_study\JavaWeb\JavaWeb.assets\image-20210511162823603.png)]

重定向 redirect

/**
 * 重定向
 * @Author: crush
 * @Date: 2021-05-09 19:35
 * version 1.0
 */
@WebServlet("/test5")
public class HttpResponseTest5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.sendRedirect("hello.jsp");
//        resp.setHeader("location","www.baidu.com"); 通過(guò)設(shè)置響應(yīng)頭轉(zhuǎn)發(fā)
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

request轉(zhuǎn)發(fā)

/**
 * 重定向
 * @Author: crush
 * @Date: 2021-05-09 19:35
 * version 1.0
 */
@WebServlet("/test5")
public class HttpResponseTest5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.sendRedirect("hello.jsp");
        req.getRequestDispatcher("hello.jsp").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

重定向和轉(zhuǎn)發(fā)的區(qū)別

文件下載

獲取路徑資源

String path=this.getServletContext().getRealPath("WEB-INF\\classes\\8.jpg");

讀取資源

FileInputStream fileInputStream=new FileInputStream(path);

獲取到文件名,路徑在電腦上保存的形式是 \ \

String fileName=path.substring(path.lastIndexOf("\\")+1);

設(shè)置消息頭告訴瀏覽器,我要下載1.png這個(gè)圖片 設(shè)置編碼

resp.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

把讀取到的內(nèi)容回送給瀏覽器

 int len=0;
byte[] bytes=new byte[1024];
// ServletOutputStream 提供用于將二進(jìn)制數(shù)據(jù)發(fā)送到客戶端的輸出流
ServletOutputStream servletOutputStream=resp.getOutputStream();
while((len=fileInputStream.read(bytes))>0) {
    servletOutputStream.write(bytes,0,len);
}

關(guān)閉資源

servletOutputStream.close();
        fileInputStream.close();

注:8.jpg 放在我的resources 文件夾下 但是這里需要寫的是編譯完8.jpg存放的位置

具體代碼

/**
 * @Author: crush
 * @Date: 2021-05-09 19:40
 * version 1.0
 */
@WebServlet("/down")
public class ResponseDownFile extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //獲取路徑資源
        String path=this.getServletContext().getRealPath("WEB-INF\\classes\\8.jpg");
        //讀取資源
        FileInputStream fileInputStream=new FileInputStream(path);
        //獲取到文件名,路徑在電腦上保存的形式是\\
        String fileName=path.substring(path.lastIndexOf("\\")+1);
        //設(shè)置消息頭告訴瀏覽器,我要下載1.png這個(gè)圖片
        // 該方式文件名為中文時(shí)會(huì)亂碼
        //防止中文亂碼
        resp.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
        //把讀取到的內(nèi)容回送給瀏覽器
        int len=0;
        byte[] bytes=new byte[1024];
        ServletOutputStream servletOutputStream=resp.getOutputStream();
        while((len=fileInputStream.read(bytes))>0) {
            servletOutputStream.write(bytes,0,len);
        }
        // 關(guān)閉資源
        servletOutputStream.close();
        fileInputStream.close();
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

HttpRequest 講解

HttpServletRequest概述

​ 我們?cè)趧?chuàng)建Servlet時(shí)會(huì)覆蓋service()方法,或doGet()/doPost(),這些方法都有兩個(gè)參數(shù),一個(gè)為代表請(qǐng)求的request和代表響應(yīng)response。service()方法中寫了根據(jù)請(qǐng)求方式的不同調(diào)用doget()和dopost().

service方法中的request的類型是ServletRequest,而doGet/doPost方法的request類型HttpServletRequest,HttpServletRequest是ServletRequest的子接口,功能和方法更加強(qiáng)大.

Request 運(yùn)行流程

img

獲取請(qǐng)求攜帶的參數(shù)

/**
 * @Author: crush
 * @Date: 2021-05-11 16:52
 * version 1.0
 */
@WebServlet("/request1")
public class RequestTest1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 這里是請(qǐng)求的參數(shù)名  必須是同名的 
        String username = req.getParameter("username"); 
        String password = req.getParameter("password");
        PrintWriter writer = resp.getWriter();
        writer.print("<h1>"+username+":"+password+"</h1>");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-2zxGslkb-1620739367750)(C:\Users\ASUS\Desktop\JavaWeb_study\JavaWeb\JavaWeb.assets\image-20210511170056042.png)]

獲取多個(gè)參數(shù)的值

  @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 設(shè)置請(qǐng)求的編碼  不然會(huì)亂碼
        req.setCharacterEncoding("utf-8");
        Enumeration<String>  names = req.getParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String[]  values = req.getParameterValues(name);
            System.out.println(name+":"+ Arrays.toString(values));
        }
    }

獲得請(qǐng)求行的信息

/**
 * @Author: crush
 * @Date: 2021-05-11 16:52
 * version 1.0
 */
@WebServlet("/request3")
public class RequestTest3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 獲得請(qǐng)求的url
        StringBuffer requestURL = req.getRequestURL();
        System.out.println("請(qǐng)求的URL===>"+requestURL);
        // 獲得請(qǐng)求的Servlet的路徑
        String path = req.getServletPath();
        System.out.println("請(qǐng)求的Servlet的路徑===>"+path);
        //返回發(fā)出此請(qǐng)求的HTTP方法的名稱,例如GET,POST或PUT
        String method = req.getMethod();
        System.out.println("返回發(fā)出此請(qǐng)求的HTTP方法的名稱==>"+method);
        //返回發(fā)送請(qǐng)求的客戶端或最后一個(gè)代理的Internet協(xié)議(IP)地址
        String remoteAddr = req.getRemoteAddr();
        System.out.println("remoteAddr==>"+remoteAddr);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

request實(shí)現(xiàn)轉(zhuǎn)發(fā)

/**
 * @Author: crush
 * @Date: 2021-05-11 16:52
 * version 1.0
 */
@WebServlet("/request2")
public class RequestTest2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 訪問(wèn)request2 轉(zhuǎn)發(fā)到 request4去
        req.getRequestDispatcher("/request4").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

request是一個(gè)域?qū)ο?/h3>

request對(duì)象也是一個(gè)存儲(chǔ)數(shù)據(jù)的區(qū)域?qū)ο?,所以也具有如下方法?/p>

setAttribute(String name, Object o)

getAttribute(String name)

removeAttribute(String name)

ServletContext 作用域:

創(chuàng)建:?jiǎn)?dòng)web應(yīng)用程序的時(shí)候創(chuàng)建

銷毀:關(guān)閉web應(yīng)用程序的時(shí)候銷毀

域的作用范圍:整個(gè)web應(yīng)用的啟動(dòng)周期

request作用域:

創(chuàng)建:訪問(wèn)時(shí)創(chuàng)建request

銷毀:響應(yīng)結(jié)束request銷毀

域的作用范圍:一次請(qǐng)求中

總結(jié)

本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • Java后端傳時(shí)間戳給前端的三種方式

    Java后端傳時(shí)間戳給前端的三種方式

    時(shí)間戳是一份能夠表示一份數(shù)據(jù)在一個(gè)特定時(shí)間點(diǎn)已經(jīng)存在的完整的可驗(yàn)證的數(shù)據(jù),本文給大家介紹了Java后端傳時(shí)間戳給前端的三種方式,并通過(guò)代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2024-12-12
  • java如何使用zip壓縮實(shí)現(xiàn)讀取寫入

    java如何使用zip壓縮實(shí)現(xiàn)讀取寫入

    這篇文章主要為大家介紹了java如何使用zip壓縮實(shí)現(xiàn)讀取寫入示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Java中IO流之字符流與字節(jié)流的轉(zhuǎn)換方式

    Java中IO流之字符流與字節(jié)流的轉(zhuǎn)換方式

    在Java中,字節(jié)流與字符流是處理數(shù)據(jù)的兩種方式,字節(jié)流適用于處理各種數(shù)據(jù)類型,如圖片、音頻等非文本數(shù)據(jù),而字符流專門用于處理文本數(shù)據(jù),Java提供了InputStreamReader和OutputStreamWriter這兩個(gè)類來(lái)實(shí)現(xiàn)字節(jié)流向字符流的轉(zhuǎn)換
    2024-10-10
  • MyBatis不用@Param傳遞多個(gè)參數(shù)的操作

    MyBatis不用@Param傳遞多個(gè)參數(shù)的操作

    這篇文章主要介紹了MyBatis不用@Param傳遞多個(gè)參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • Java對(duì)文件的隨機(jī)讀寫以及壓縮處理操作

    Java對(duì)文件的隨機(jī)讀寫以及壓縮處理操作

    這篇文章主要介紹了Java對(duì)文件的隨機(jī)讀寫以及壓縮處理操作,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-10-10
  • 如何解決HttpServletRequest.getInputStream()多次讀取問(wèn)題

    如何解決HttpServletRequest.getInputStream()多次讀取問(wèn)題

    這篇文章主要介紹了如何解決HttpServletRequest.getInputStream()多次讀取問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • mybatis-plus條件構(gòu)造器的操作代碼

    mybatis-plus條件構(gòu)造器的操作代碼

    mybatis-plus提供了AbstractWrapper抽象類,提供了很多sql語(yǔ)法支持的方法,比如模糊查詢,比較,區(qū)間,分組查詢,排序,判斷空,子查詢等等,方便我們用面向?qū)ο蟮姆绞饺?shí)現(xiàn)sql語(yǔ)句,本文重點(diǎn)給大家介紹mybatis-plus條件構(gòu)造器的操作代碼,感興趣的朋友一起看看吧
    2022-03-03
  • Java如何使用Set接口存儲(chǔ)沒有重復(fù)元素的數(shù)組

    Java如何使用Set接口存儲(chǔ)沒有重復(fù)元素的數(shù)組

    Set是一個(gè)繼承于Collection的接口,即Set也是集合中的一種。Set是沒有重復(fù)元素的集合,本篇我們就用它存儲(chǔ)一個(gè)沒有重復(fù)元素的數(shù)組
    2022-04-04
  • 最新評(píng)論

    红河县| 华容县| 临安市| 云阳县| 行唐县| 绥德县| 游戏| 清原| 广汉市| 石城县| 霞浦县| 广安市| 章丘市| 尉犁县| 永川市| 许昌县| 莱芜市| 宣武区| 夏津县| 苏尼特右旗| 大渡口区| 正蓝旗| 黔西县| 濮阳县| 沅江市| 翁源县| 谢通门县| 江津市| 格尔木市| 孟州市| 渝中区| 桓台县| 都昌县| 大余县| 喀喇| 游戏| 安阳县| 原阳县| 长顺县| 南昌县| 仙游县|