•
Request通用方式获取请求参数
○ 请求参数获取方式:
§ GET方式:
□ String getQueryString()
§ POST方式:
□ BufferedReader getReader()
○ 通用方式:
§ Map<String, String[]> getParameterMap()//获取所有参数Map集合
§ String[] getParameterValues(Stringname)//根据名称获取参数值(数组)
§ String getParameter(Stringname)//根据名称获取参数值(单个值)
/**
* Request获取请求数据
*/
@WebServlet("/req1")
public class RequestDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
* 获取请求行数据
*/
// □ String getMethod()//获取请求方法
String method = req.getMethod();
System.out.println(method);//GET
//□ String getContextPath()//获取虚拟目录(项目访问路径)/request-demo
String path = req.getContextPath();
System.out.println(path);
//□ StringBuffer getRequestURL()//获取URL(统一资源定位符):http://localhost:8080/request-demo/req1
StringBuffer url = req.getRequestURL();
System.out.println(url);
//□ String getRequestURI()//获取URI(统一资源标识符):/request-demo/req1
String uri = req.getRequestURI();
System.out.println(uri);
//□ String getQueryString()//获取请求参数(GET方式):username=zhangsan&password=123
String queryString = req.getQueryString();
System.out.println(queryString);
/**
* 获取请求头数据
*/
//获取请求头:user-agent:浏览器版本信息
String reqHeader = req.getHeader("user-agent");
System.out.println(reqHeader);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
*获取post请求体:请求参数
*/
//获取字符输入流
BufferedReader br = req.getReader();
//读取数据
String line = br.readLine();
System.out.println(line);
}
}
对应的post表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="//localhost:8080/request-demo/req4" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
</form>
</body>
</html>
○ 使用通用方式获取参数和,屏蔽了GET和POST的请求方式代码的不同,则代码可以定义为如下格式
@WebServlet("/req3")
publicclassRequestDemo2extendsHttpServlet{
@Override
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//GET请求逻辑
}
@Override
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//POST请求逻辑
this.doGet(req,resp);
}
}
执行流程大致同下面相同
创建 Servlet模板
可以使用Servlet模板创建Servlet更高效 ,具体步骤如下:
设置Servlet模板的步骤如下
:
○ Request请求参数中文乱码处理
§ 请求参数如果存在中文数据,会乱码
§ 解决方案:
□ POST:设置输入流的编码
® request.setCharacterEncoding("UTF-8");
□ GET:
□ URL编码:
® 将字符串按照编码方式转为二进制
® 每个字节转为2个16进制数并在前边加上%
/**
* 中文乱码问题的解决方案
*/
@WebServlet("/req4")
public class RequestDemo4 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* //1.解决乱码 POST getReader()
request.setCharacterEncoding("UTF-8");//设置字符输入流的编码
//2.获取username
String username = request.getParameter("username");
System.out.println(username);*/
//3.GET获取参数的方式底层还是,getQueryString()
request.setCharacterEncoding("UTF-8");//设置字符输入流的编码
//2.获取username
String username = request.getParameter("username");
System.out.println("乱码:" + username);
//乱码原因:tomcat进行URL解码时,默认字符集是ISO-8859-1
//3.1先对乱码数据进行编码,转为字节数组
//3.2字节数组解码
username = new String(username.getBytes("ISO-8859-1"), "utf-8");
System.out.println("解决乱码后:" + username);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
对应的post表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="//localhost:8080/request-demo/req4" method="get">
<input type="text" name="username"><br>
<input type="password" name="password"><br>
<input type="checkbox" name="hobby" value="1">游泳
<input type="checkbox" name="hobby" value="2">爬山<br>
<input type="submit">
</form>
</body>
</html>
执行步骤
1.运行tomcat服务器
2.进入链接
3.输入表单文件
4.在输入框中输入任意中文
5.回到idea控制台查看
§ Tomcat8.0之后,已将GET请求乱码问题解决,设置默认的解码方式为UTF-8