Request获取请求数据
1.请求行:GET/request-demo/req1?username=zhangsan HTTP/1.1
String getMethod():获取请求方式:GET
String getContextPath():获取虚拟目录(项目访问路径): /request-demo
StringBuffer getRequestURL():获取URL(统一资源定位符):http://localhost:8080/request-demo/req1
String getRequestURI():获取URI(统一资源标识符):/request-demo/req1
String getQueryString():获取请求参数(GT方式):username=zhangsan&password=123
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("/req1")
public class RequestDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getMethod();
System.out.println(method);//GET
String contextPath = req.getContextPath();
System.out.println(contextPath);
StringBuffer requestURL = req.getRequestURL();
System.out.println(requestURL);
String requestURI = req.getRequestURI();
System.out.println(requestURI);
String queryString = req.getQueryString();
System.out.println(queryString);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
2.获取请求头:User-Agent:Mozilla/5.0 Chrome/91.0.4472.106
String getHeader(String name):根据请求头名称,获取值
//获取请求头
//User-Agent获取浏览器版本信息
String header = req.getHeader("User-Agent");
System.out.println(header);
3.请求体:username=zhangsan&password=123
ServletInputStream getInputStream():获取字节输入流
BufferReader getReader():获取字符输入流
写一个Post请求的表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/1/req1" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
</form>
</body>
</html>
在doPost中获取请求体
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
Request通用获取请求方式
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//GET请求逻辑
System.out.println("get...");
//获取所有参数的map集合
Map<String, String[]> parameterMap = req.getParameterMap();
for (String key:parameterMap.keySet()){
System.out.print(key+":");
String[] strings = parameterMap.get(key);
for (String value:strings){
System.out.print(value+" ");
}
}
System.out.println("-----------");
//根据key获取参数值,数组
String[] hobbies = req.getParameterValues("hobby");
for (String hobby:hobbies){
System.out.println(hobby);
}
//根据key获取单个参数值
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
}
新建一个表单
GET方式:
修改表单提交方式为POST
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
请求参数中文乱码
1.post方式
request.setCharacterEncoding("UTF-8");//设置字符流的编码
编码与解码:
public static void main(String[] args) throws UnsupportedEncodingException {
String username="张三";//%E5%BC%A0%E4%B8%89
//1.URL编码
String encode=URLEncoder.encode(username,"UTF-8");
System.out.println(encode);
//2.URL解码
String decode = URLDecoder.decode(encode, "UTF-8");
System.out.println(decode);
}
原理:
public static void main(String[] args) throws UnsupportedEncodingException {
String username="张三";//%E5%BC%A0%E4%B8%89
//1.URL编码
String encode=URLEncoder.encode(username,"UTF-8");
System.out.println(encode);
//2.URL解码
String decode = URLDecoder.decode(encode, "ISO-8859-1");
System.out.println(decode);
//3.转换字节数据
byte[] bytes = decode.getBytes("ISO-8859-1");
for (byte b:bytes){
System.out.print(b+" ");
}
//4.字节数组转换为字符串
String s = new String(bytes, "UTF-8");
System.out.println(s);
}
GET和POST方法都可用:
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决乱码Post
//request.setCharacterEncoding("UTF-8");//设置字符流的编码
String username = request.getParameter("username");
username=new String(username.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
Request请求转发
req5:
@WebServlet("/req5")
public class RequestDemo5 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo5");
//请求转发
request.getRequestDispatcher("/req6").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
req6:
@WebServlet("/req6")
public class RequestDemo6 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo6");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
请求转发资源间共享数据:使用Request对象
void setAttribute(String name,Object o):存储数据到request域中
Object getAttribute(String name):根据key值,获取值
void removeAttribute(String name):根key值,删除该键值对
@WebServlet("/req5")
public class RequestDemo5 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo5");
//存储数据
request.setAttribute("msg","hello");
//请求转发
request.getRequestDispatcher("/req6").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
@WebServlet("/req6")
public class RequestDemo6 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo6");
//接受数据
Object msg = request.getAttribute("msg");
System.out.println(msg);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
请求转发的特点:
1.浏览器地址栏路径不发生变化
2.只能转发到当前服务器的内部资源
3.一次请求,可以在转发的资源间使用request共享数据