注意:只能在一次请求转发中使用。
package org.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
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;
@RestController
public class ParamController
{
@RequestMapping(value = "/test1") //获取请求行数据
public void test1(HttpServletRequest request)
{
System.out.println(request.getMethod()); //获取请求方式;
System.out.println(request.getContextPath()); //获取虚拟目录
System.out.println(request.getServletPath()); //获取servlet路径
System.out.println(request.getQueryString()); //获取请求参数
System.out.println(request.getRequestURI()); //获取请求URI,部分
System.out.println(request.getRequestURL()); //获取请求URI,全部
}
@RequestMapping(value = "/test2") //获取请求头方法一
public void test2(HttpServletRequest request)
{
System.out.println(request.getHeader("accept"));
}
@RequestMapping(value = "/test3") //获取请求头方法二
public void test3(HttpServletRequest request)
{
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) //读取请求消息头
{
String name = headerNames.nextElement();
String value = request.getHeader(name);
System.out.println(name + " "+value);
}
}
@RequestMapping(value = "/test4")
public void test4(HttpServletRequest request)
{
// http://localhost:8080/test4?a=1&b=2&c=3&c=4
//返回指定参数名的参数值。
String a = request.getParameter("a");
System.out.println(a);
System.out.println("---------------------------------");
//返回指定参数名的参数值。
String c = request.getParameter("c"); //只显示一个
System.out.println(c);
System.out.println("---------------------------------");
//以字符串数组的形式返回指定参数名的所有参数值(HTTP 请求中可以有多个相同参数名的参数)。
String[] as = request.getParameterValues("c");
for (String d : as)
{
System.out.println(d);
}
System.out.println("---------------------------------");
//以枚举集合的形式返回请求中所有参数名。
Enumeration<String> asd = request.getParameterNames();
while (asd.hasMoreElements())
{
String name = asd.nextElement();
System.out.println(name+" " + request.getParameter(name));
}
System.out.println("---------------------------------");
//用于将请求中的所有参数名和参数值装入一个 Map 对象中返回。
Map<String,String[]> map = request.getParameterMap();
Set<String> keySet = map.keySet();
for (String name : keySet)
{
String[] values= map.get(name);
System.out.println(name);
for (String value : values)
{
System.out.println(value);
}
}
}
@RequestMapping(value = "/test5") //请求转发
public void test5(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
request. getRequestDispatcher("/test1").forward(request,response);
}
@RequestMapping(value = "/test8") //获取请求体数据
public void test8(HttpServletRequest request) throws IOException {
BufferedReader rd = request.getReader();
String line = null;
while ((line = rd.readLine()) != null)
{
System.out.println(line);
}
}
@RequestMapping(value = "/test10") //请求转发
public void test10(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
request.setAttribute("msg","china");
request. getRequestDispatcher("/test11").forward(request,response);
}
@RequestMapping(value = "/test11") //请求转发
public void test11(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
System.out.println(request.getAttribute("msg"));
System.out.println("------------------------------1");
request.removeAttribute("msg");
System.out.println("------------------------------2");
System.out.println(request.getAttribute("msg"));
System.out.println("------------------------------3");
}
@RequestMapping(value = "/test15")
public String test15(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";
}
}