HttpServletRequest

HttpServletRequest概述:html

service方法中的request的類型是ServletRequest,而doGet/doPost方法的request的類型是HttpServletRequest,HttpServletRequest是ServletRequest的子接口。java

request的運行流程

 

經過request得到請求行:oracle

package com.oracle.demo01;

import java.io.IOException;
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 {
        //獲取請求方式
        System.out.println("請求方式爲"+request.getMethod());
        //獲取請求URI
        System.out.println("URI爲"+request.getRequestURI());
        //獲取請求URL
        System.out.println("URL爲"+request.getRequestURL());
        //獲取WEB應用名稱
        System.out.println("WEB應用名稱爲"+request.getContextPath());
        //獲取get請求後URL後的字符串
        System.out.println("get請求參數爲"+request.getQueryString());
        //獲取客戶端的ip地址
        System.out.println("ip地址爲"+request.getRemoteAddr());
        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

 運行結果:post

 

package com.oracle.demo01;

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

public class RefereServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取refer頭
        String refere=request.getHeader("Referer");
        String content=null;
        if(refere.startsWith("http://localhost:8080")){
            content="真的離婚了!";
        }else{
            content="你是小偷!";
        }
        //解決response中文亂碼
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(content);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
<%@ 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="/WEB06/RefereServlet">宋仲基和宋慧喬分手了</a>
</body>
</html>

 

經過request得到請求體:性能

package com.oracle.demo01;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BodyServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post請求解決中文亂碼問題
        //request.setCharacterEncoding("utf-8");
        //獲取請求參數
        //1.獲取單個值的參數
        String name=request.getParameter("username");
        name=new String(name.getBytes("ISO-8859-1"), "UTF-8");
        String sex=request.getParameter("sex");
        sex=new String(sex.getBytes("ISO-8859-1"), "UTF-8");
        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);
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
<%@ 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>
    <form action="/WEB06/BodyServlet" method="get">
        <input type="text" name="username"><br>
        <input type="radio" name="sex" value="man"><input type="radio" name="sex" value="woman"><br>
        <input type="checkbox" name="hobby" value="ppq">
        乒乓球
        <input type="checkbox" name="hobby" value="pq">
        排球
        <input type="checkbox" name="hobby" value="wq">
        網球
        <br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

 

 

 

request是一個域對象網站

package com.oracle.demo02;

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

public class Servlet01 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //向request域中存值
        request.setAttribute("name", "袁一鳴");
        //請求轉發
        request.getRequestDispatcher("/Servlet02").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
package com.oracle.demo02;

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


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);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

運行結果:ui

request生命週期:spa

建立:訪問時建立requestcode

銷燬:響應結束request銷燬orm

域的做用範圍:一次請求中

 

轉發與重定向的區別:

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

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

3)從新定向能夠訪問外部網站 轉發只能訪問內部資源

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

相關文章
相關標籤/搜索