Request

1、Request原理和繼承體系java

1. request對象和response對象的原理
    1. request和response對象是由服務器建立的。咱們來使用它們
    2. request對象是來獲取請求消息,response對象是來設置響應消息

2. request對象繼承體系結構:    
    ServletRequest        --    接口
        |    繼承
    HttpServletRequest    -- 接口
        |    實現
    org.apache.catalina.connector.RequestFacade 類(tomcat)


2、Request功能web

一、獲取請求行數據apache

1. 獲取請求消息數據
    1. 獲取請求行數據
        * GET /day14/demo1?name=zhangsan HTTP/1.1
        * 方法:
            1. 獲取請求方式 :GET
                * String getMethod()  
            2. (*)獲取虛擬目錄:/day14
                * String getContextPath()
            3. 獲取Servlet路徑: /demo1
                * String getServletPath()
            4. 獲取get方式請求參數:name=zhangsan
                * String getQueryString()
            5. (*)獲取請求URI:/day14/demo1
                * String getRequestURI():        /day14/demo1
                * StringBuffer getRequestURL()  :http://localhost/day14/demo1

                * URL:統一資源定位符 : http://localhost/day14/demo1    中華人民共和國
                * URI:統一資源標識符 : /day14/demo1                    共和國
            
            6. 獲取協議及版本:HTTP/1.1
                * String getProtocol()

            7. 獲取客戶機的IP地址:
                * String getRemoteAddr()


---------------------------------
package cn.itcast.web.request;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取請求方式
        String method = request.getMethod();
        System.out.println(method);
        //獲取虛擬目錄
        String contextPath = request.getContextPath();
        System.out.println(contextPath);
        //獲取Servlet路徑
        String servletPath = request.getServletPath();
        System.out.println(servletPath);
        //獲取get方式請求參數
        String queryString = request.getQueryString();
        System.out.println(queryString);
        //獲取請求URI
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        StringBuffer requestURL = request.getRequestURL();
        System.out.println(requestURL);
        //獲取協議及版本
        String protocol = request.getProtocol();
        System.out.println(protocol);
        //獲取客戶機的IP地址
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);
    }
}


二、獲取請求頭數據數組

* 方法:
    * (*)String getHeader(String name):經過請求頭的名稱獲取請求頭的值
    * Enumeration<String> getHeaderNames():獲取全部的請求頭名稱

----------------------------------
@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //演示獲取請求頭數據:user-agent
        String agent = request.getHeader("user-agent");
        //判斷agent的瀏覽器版本
        if(agent.contains("Chrome")) {
            //谷歌
            System.out.println("谷歌...");
        } else if(agent.contains("Firefox")) {
            System.out.println("火狐...");
        }

    }
}


----------------------------------
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //演示獲取請求頭數據:referer
        String referer = request.getHeader("referer");
        System.out.println(referer);

        //防盜鏈
        if(referer != null) {
            if(referer.contains("/day14")) {
                System.out.println("正常的");
            }else {
                System.out.println("盜鏈");
            }
        }
    }
}


三、獲取請求體數據瀏覽器

* 請求體:只有POST請求方式,纔有請求體,在請求體中封裝了POST請求的請求參數
    * 步驟:
        1. 獲取流對象
            *  BufferedReader getReader():獲取字符輸入流,只能操做字符數據
            *  ServletInputStream getInputStream():獲取字節輸入流,能夠操做全部類型數據
                * 在文件上傳知識點後講解

        2. 再從流對象中拿數據


-----------------------------------
@WebServlet("/requestDemo5")
public class RequestDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取請求消息體--請求參數

        //1.獲取字符流
        BufferedReader br = request.getReader();
        //2.讀取數據
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}


四、獲取請求參數tomcat

1. 獲取請求參數通用方式:不論get仍是post請求方式均可以使用下列方法來獲取請求參數
    1. String getParameter(String name):根據參數名稱獲取參數值    username=zs&password=123
    2. String[] getParameterValues(String name):根據參數名稱獲取參數值的數組  hobby=xx&hobby=game
    3. Enumeration<String> getParameterNames():獲取全部請求的參數名稱
    4. Map<String,String[]> getParameterMap():獲取全部參數的map集合

--------------------------------
package cn.itcast.web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post 獲取請求參數

        //根據參數名稱獲取參數值
        String username = request.getParameter("username");
        System.out.println("post");
        System.out.println(username);

        //根據參數名稱獲取參數值的數組
        String[] hobbies  = request.getParameterValues("hobby");
        for(String hobby : hobbies) {
            System.out.println(hobby);
        }

        //獲取全部請求的參數名稱
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            System.out.println(name);
            String value = request.getParameter(name);
            System.out.println(value);
            System.out.println("--------------");
        }

        //獲取全部參數的map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //遍歷
        Set<String> keyset = parameterMap.keySet();
        for(String name : keyset) {
            String[] values = parameterMap.get(name);
            System.out.println(name);
            for(String value: values) {
                System.out.println(value);
            }
            System.out.println("-------------");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get 獲取請求參數

        //根據參數名稱獲取參數值
//        String username = request.getParameter("username");
//        System.out.println("post");
//        System.out.println(username);
        this.doPost(request,response);
    }
}


五、中文亂碼問題服務器

* get方式:tomcat 8 已經將get方式亂碼問題解決了

* post方式:會亂碼
    * 解決:在獲取參數前,設置request的編碼request.setCharacterEncoding("utf-8");


六、請求轉發post

請求轉發:一種在服務器內部的資源跳轉方式
    1. 步驟:
        1. 經過request對象獲取請求轉發器對象:RequestDispatcher getRequestDispatcher(String path)
        2. 使用RequestDispatcher對象來進行轉發:forward(ServletRequest request, ServletResponse response) 

    2. 特色:
        1. 瀏覽器地址欄路徑不發生變化
        2. 只能轉發到當前服務器內部資源中。
        3. 轉發是一次請求
        
    3. 共享數據:
        * 域對象:一個有做用範圍的對象,能夠在範圍內共享數據
        * request域:表明一次請求的範圍,通常用於請求轉發的多個資源中共享數據
        * 方法:
            1. void setAttribute(String name,Object obj):存儲數據
            2. Object getAttitude(String name):經過鍵獲取值
            3. void removeAttribute(String name):經過鍵移除鍵值對

    4. 獲取ServletContext:
        * ServletContext getServletContext()


------------------------------------
@WebServlet("/requestDemo8")
public class RequestDemo8 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo8888被訪問了。。。");
        //轉發到demo9資源
/*
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo9");
        requestDispatcher.forward(request,response);
        */

        //存儲數據到request域中
        request.setAttribute("msg","hello");

        request.getRequestDispatcher("/requestDemo9").forward(request,response);
        //request.getRequestDispatcher("http://www.itcast.cn").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }
}



@WebServlet("/requestDemo9")
public class RequestDemo9 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //獲取數據
        Object msg = request.getAttribute("msg");
        System.out.println(msg);

        System.out.println("demo9999被訪問了。。。");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }
}


---------------------------
獲取ServletContext

@WebServlet("/requestDemo10")
public class RequestDemo10 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        ServletContext servletContext = request.getServletContext();

        System.out.println(servletContext);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }
}
相關文章
相關標籤/搜索