JAVA基礎之HttpServletRequest請求


   HttpServletRequest請求是獲取請求行、請求頭和請求體;可以通過這個方法設置防盜鏈,獲取地址。牢記解決亂碼的方式。

  怎么選擇是重定向還是轉發呢?通常情況下轉發更快,而且能保持request內的對象,所以他是第一選擇。但是由於在轉發之后,瀏覽器中URL仍然指向開始頁面,此時如果重載當前頁面,開始頁面將會被重新調用。如果你不想看到這樣的情況,則選擇轉發。詳見https://www.cnblogs.com/wenanbang/p/4142415.html

其實也可以單純的認為是若是帶數據的話就請求轉發了,若改地址的話就重定向了。

一、HttpServletRequest:

  概述和運行流程詳見HttpServletResponse!

二、獲取HTTP請求:

 三、獲取請求行:

1、獲得客戶端的請求方式:

getMethod()     獲得String類型;

2、獲得請求的資源:

getContextPath() 獲得String類型的 web應用的名稱(項目名)

getQueryString() 獲得 get提交url 地址后的參數字符串;

 getRequestURI() 獲取URI地址String類型

 getRequestURL() 獲取URL地址StringBuffer類型

request.getRemoteAddr() 獲得訪問的客戶端IP地址

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 獲取請求方式
        String method = request.getMethod();
        System.out.println("請求方式為:" + method);
        // 獲取請求URI
        String URI = request.getRequestURI();
        System.out.println("URI為" + URI);
        // 獲取請求URL
        StringBuffer URL = request.getRequestURL();
        System.out.println("URL為:" + URL);
        // URI為/WEB/LineServlet
        // URL為:http://localhost:8080/WEB/LineServlet
        // 獲取WEB項目名稱
        String name = request.getContextPath();
        System.out.println("WEB項目名稱為:" + name);
        // 獲取get請求后url后的字符串
        String query = request.getQueryString();
        System.out.println("get請求的參數為:" + query);
        // 獲取客戶端的ip地址
        String ip = request.getRemoteAddr();
        System.out.println("ip地址為:" + ip);
    }

四、獲取請求頭:

 getHeader(String name) 

referer 頭的作用:獲取該訪問的來源,做防盜鏈

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 獲取refere頭
        String refere = request.getHeader("Referer");
        String content = null;
        if(refere.startsWith("http://localhost:8080")){
            content="真的離婚了!";        
        }else{
            content="你是小偷!";
        }
        //解決亂碼
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(content);        
    }
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <a href="/WEB/RefereServlet">宋仲基和宋慧喬分手了</a>
</body>
</html>

五、獲得請求體:

1、解決post提交方式的亂碼:request.setCharacterEncoding("UTF-8");

2、解決get提交的方式的亂碼:

            parameter = new String(parameter.getbytes("iso8859-1"),"utf-8");

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解決中文亂碼問題(post請求)
//        request.setCharacterEncoding("UTF-8");
        //獲取請求參數
        //1.單個值的參數
        String name=request.getParameter("username");
        //get解決亂碼:得先將獲取到的信息用原來的編碼表進行解碼,然后再用后來的編碼表進行編碼
        name=new String(name.getBytes("ISO-8859-1"),"UTF=8");
        String sex=request.getParameter("sex");
        System.out.println(name+" ......"+sex);
        //2.獲取多個值的
        String[] hobbys=request.getParameterValues("hobby");
        //遍歷
        for(String s:hobbys){
            System.out.println(s);
        }
        //3.獲取所有的請求參數MAP
        Map<String,String[]> map=request.getParameterMap();
        //遍歷
        Set<String> set=map.keySet();
        for(String str:set){
            String [] value=map.get(str);
            for(String v:value){
                System.out.println(str+"..."+v);
            }
        }        

 

六、request的其他功能:

1、request 也是個域對象,也是具有以下功能:

  setAttribute(String name, Object o)

  getAttribute(String name)

  removeAttribute(String name)

2、request完成請求轉發:

  獲得請求轉發器----path是轉發的地址

RequestDispatcher getRequestDispatcher(String path)

  通過轉發器對象轉發

requestDispathcher.forward(ServletRequest request, ServletResponse response)

七、ServletContext域與Request域的生命周期比較

1、ServletContext

       創建:服務器啟動

       銷毀:服務器關閉

       域的作用范圍:整個web應用

2、request

      創建:訪問時創建request

      銷毀:響應結束request銷毀

      域的作用范圍:一次請求中

八、轉發和重定向的區別:

1、重定向兩次請求,轉發一次請求

2、重定向地址欄的地址變化,轉發地址不變

3、重新定向可以訪問外部網站 轉發只能訪問內部資源

4、轉發的性能要優於重定向

(需要帶數據的時候用請求轉發,由第一個Servlet01帶數據到Servlet02需要地址轉變的用重定向)

public class Servlet01 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //向request域中存值
        request.setAttribute("name", "張三" );
        //請求轉發
        request.getRequestDispatcher("/Servlet02").forward(request, response);
    
        
    }
public class Servlet02 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //從request 域中取值
        String name =(String)request.getAttribute("name");
        //解決中文亂碼
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("hello"+name);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM