控制器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package com.awaimai.web;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
@RestController
public class kzq
{
@RequestMapping (value = "/test1" )
public String test1(HttpServletRequest request)
{
System.out.println(request.getMethod());
System.out.println(request.getPathInfo());
System.out.println(request.getRequestURI());
System.out.println(request.getRequestURL());
System.out.println(request.getQueryString());
StringBuilder sb = new StringBuilder();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) //读取请求消息头
{
String name = headerNames.nextElement();
sb.append(name + ":" + request.getHeader(name) + "<br>" );
}
return sb.toString();
}
@RequestMapping (value = "/test2" )
public String test2(HttpServletResponse response)
{
response.setHeader( "content-type" , "text/html;charset=UTF-8" ); //告知浏览器使用什么码表
//告知客户端不缓存
response.setHeader( "pragma" , "no-cache" );
response.setHeader( "cache-control" , "no-cache" );
response.setDateHeader( "expires" , 0 );
//添加Cookie
response.addCookie( new Cookie( "cook-001-name" , "cookie-001-value" ));
response.addCookie( new Cookie( "cook-002-name" , "cookie-002-value" ));
response.setStatus( 404 ); //我们将状态设置成404
return "test2" ;
}
}
|
打印如下:
GET
null
/test1
http://localhost:8080/test1
null
web访问如下:
===========================================================================
===========================================================================
url中带参数: http://localhost:8080/test1?a=b&c=2
打印如下:
web访问如下:
=============================================================================
=============================================================================
post 访问如下:
user-agent:PostmanRuntime/7.28.2<br>
accept:*/*<br>
cache-control:no-cache<br>
postman-token:a67b445a-48b2-46b9-b850-299d9eec5641<br>
host:localhost:8080<br>
accept-encoding:gzip, deflate, br<br>
connection:keep-alive<br>
===================================================================
===================================================================
HttpServletRequest
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,
开发人员通过这个对象的方法,可以获得客户这些信息。
主要功能:参数解析,Cookie读取,http请求字段,文件上传
1.请求行
getMethod(); 获得请求方式。
getQueryString() ; 返回请求行中的参数部分。
getRequestURI(); 返回请求行中的资源名部分。
getRequestURL();返回客户端发出请求时的完整URL。
==============================================================================
==============================================================================
2.请求消息头
String getHeader(String name) 根据请求消息头名称称得到请求消息头信息值。
Enumeration getHeaderNames() 得到所有请求消息头名称。
Enumeration getHeaders(String name) 根据头名称得到相同名称请求头信息值。
String getCookies() 获取请求消息头中的cookie值。
package com.awaimai.web;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
@RestController
public class kzq
{
@RequestMapping(value = "/test1")
public String test1(HttpServletRequest request)
{
System.out.println(request.getMethod());
System.out.println(request.getPathInfo());
System.out.println(request.getRequestURI());
System.out.println(request.getRequestURL());
System.out.println(request.getQueryString());
StringBuilder sb = new StringBuilder();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) //读取请求消息头
{
String name = headerNames.nextElement();
sb.append(name + ":" + request.getHeader(name) + "<br>");
}
return sb.toString();
}
@RequestMapping(value = "/test2")
public String test2(HttpServletResponse response)
{
response.setHeader("content-type", "text/html;charset=UTF-8"); //告知浏览器使用什么码表
//告知客户端不缓存
response.setHeader("pragma", "no-cache");
response.setHeader("cache-control", "no-cache");
response.setDateHeader("expires", 0);
//添加Cookie
response.addCookie(new Cookie("cook-001-name" ,"cookie-001-value"));
response.addCookie(new Cookie("cook-002-name" ,"cookie-002-value"));
response.setStatus(404); //我们将状态设置成404
return "test2";
}
@RequestMapping(value = "/test3")
public String test3(HttpServletRequest request)
{
StringBuilder sb = new StringBuilder();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements())
{ //读取请求消息头
String name = headerNames.nextElement();
sb.append(name + ":" + request.getHeader(name) + "<br>");
}
return sb.toString();
}
}
=================================================================
=================================================================
http://localhost:8080/test2 获取浏览器设置的返回的cookie;
http://localhost:8080/test3 打开这个链接,发现访问这个url的时候,浏览器自动带上cookie,返回给服务器,服务器获取后进行打印
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package com.awaimai.web;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
@RestController
public class kzq
{
@RequestMapping (value = "/test1" )
public String test1(HttpServletRequest request)
{
System.out.println(request.getMethod());
System.out.println(request.getPathInfo());
System.out.println(request.getRequestURI());
System.out.println(request.getRequestURL());
System.out.println(request.getQueryString());
StringBuilder sb = new StringBuilder();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) //读取请求消息头
{
String name = headerNames.nextElement();
sb.append(name + ":" + request.getHeader(name) + "<br>" );
}
return sb.toString();
}
@RequestMapping (value = "/test2" )
public String test2(HttpServletResponse response)
{
response.setHeader( "content-type" , "text/html;charset=UTF-8" ); //告知浏览器使用什么码表
//告知客户端不缓存
response.setHeader( "pragma" , "no-cache" );
response.setHeader( "cache-control" , "no-cache" );
response.setDateHeader( "expires" , 0 );
//添加Cookie
response.addCookie( new Cookie( "cook-001-name" , "cookie-001-value" ));
response.addCookie( new Cookie( "cook-002-name" , "cookie-002-value" ));
response.setStatus( 404 ); //我们将状态设置成404
return "test2" ;
}
@RequestMapping (value = "/test3" )
public String test3(HttpServletRequest request)
{
StringBuilder sb = new StringBuilder();
if (request.getCookies()!= null ){
for (Cookie cookie :request.getCookies()){
sb.append( "Cookie: " + cookie.getName()+ " value: " + cookie.getValue());
}
}
return sb.toString();
}
}
|
==========================================================================
===========================================================================
getParameter(name)方法 获取请求中的参数,该参数是由name指定。
getParameterValues(String name)方法 获取指定名称参数的所有值数组。它适用于一个参数名对应多个值的情况。如页面表单中的复选框,多选列表提交的值。
getParameterNames() 方法返回一个包含请求消息中的所有参数名的Enumeration对象。通过遍历这个Enumeration对象,就可以获取请求消息中所有的参数名。
getCharacterEncoding() 返回请求的字符编码方式。
getAttributeNames()返回当前请求的所有属性的名字集合赋值:setAttribute()。
getAttribute(String name) 返回name指定的属性值。
getsession()返回和客户端相关的session,如果没有给客户端分配session,则返回null。
getParameterMap():返回一个保存了请求消息中的所有参数名和值的Map对象。
Map对象的key是字符串类型的参数名,value是这个参数所对应的Object类型的值数组
RequestDispatcher.forward() 方法的请求转发过程结束后,浏览器地址栏保持初始的URL地址不变。
方法在服务器端内部将请求转发给另外一个资源,浏览器只知道发出了请求并得到了响应结果,并不知道在服务器程序内部发生了转发行为。
setCharacterEncoding(“utf-8”);
getReader() 获取请求体的数据流
getInputStream() 获取请求的输入流中的数据
==========================================================================
==========================================================================
控制器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package com.awaimai.web;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
public class kzq
{
@RequestMapping (value = "/test2" )
public String test2(HttpServletResponse response)
{
response.setHeader( "content-type" , "text/html;charset=UTF-8" ); //告知浏览器使用什么码表
//告知客户端不缓存
response.setHeader( "pragma" , "no-cache" );
response.setHeader( "cache-control" , "no-cache" );
response.setDateHeader( "expires" , 0 );
//添加Cookie
response.addCookie( new Cookie( "cook-001-name" , "cookie-001-value" ));
response.addCookie( new Cookie( "cook-002-name" , "cookie-002-value" ));
response.setStatus( 404 ); //我们将状态设置成404
return "success" ;
}
}
|
post请求: