文章目录
- 一、请求方式
- (1)GET请求的三种方式
- (2)POST请求
- (3)GET和POST的区别
- GET和POST的区别Demo
- 二、客户端发起请求
- (1)HttpServletRequest对象的方法
- (2)接收中文乱码问题
- 解决GET请求乱码问题:
- 解决POST请求乱码问题
一、请求方式
(1)GET请求的三种方式
- ①在地址栏输入一个地址
- ②点击页面中的连接(超链接)
- ③表单的默认提交方式
(2)POST请求
- 将表单的Method属性设置为POST时,浏览器会发送POST请求
(3)GET和POST的区别
- GET的提交的参数会显示到地址栏上,而POST不会显示。
- GET往往是有大小限制的,而POST没有大小的限制。
- GET没有请求体,POST有请求体。
GET和POST的区别Demo
①在WebRoot的静态页面中创建form.html文件
<!DOCTYPE html>
<html>
<head>
<title>form.html</title>
<meta charset="utf-8">
</head>
<body>
<form action='hello' method='get'>
姓名:<input type="text" name="username"/><br/>
爱好:购物<input type="checkbox" name="habit" value="shooping" />
钓鱼<input type="checkbox" name="habit" value="fishing" />
游泳<input type="checkbox" name="habit" value="swiming" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
②在web项目中创建名为HelloServlet的servlet文件,并覆盖service方法。
package web;
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 HelloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
}
}
此时,可以发现web.xml中自动生成了Servlet配置和映射,但这并不重要。
③启动tomcat,打开浏览器发起请求。
Get方式
提交前:
提交后:
查看页面查看控制台:
POST方式
提交前:
提交后:
查看页面查看控制台:
二、客户端发起请求
HttpServletReques
t类是ServletRequest的子类,主要作用是浏览器发起请求,提交数据到服务器端。
(1)HttpServletRequest对象的方法
HttpServletRequest对象的方法 | 说明 |
String getParameter(String name); | 用于接收一 |
String[] getParamenterValues(String name); | 用于接收一 |
Map getParameterMap(); | 用于接收 |
Enumeration getParameterNames() | 用于获取表单中提交的 |
HttpServletRequest对象的方法Demo
package web;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 演示HttpServletRequest对象的一些方法
*
* @author QianliangGuo
*/
public class HelloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.根据参数名获取参数值
String username = request.getParameter("username");
System.out.println(username);
//2.根据参数名获取一组参数值
String[] habits = request.getParameterValues("habit");
for (String i : habits) {
System.out.println(i);
}
//3.接收表单中所有数据(键值对)
Map<String, String[]> map = request.getParameterMap();
Set<Entry<String, String[]>> entrySet = map.entrySet();
for (Entry<String, String[]> i : entrySet) {
System.out.println(i.getKey()+":"+i.getValue());
}
}
}
使用浏览器访问服务器:
服务器做出响应:
(2)接收中文乱码问题
- 产生乱码的原因:客户端编码与服务端解码不一致。
- 注意:Tomcat8之后,get请求默认编码为utf-8,不需要进行重新编码,只需setCharacterxxx
- 我个人的Tomcat是7版本,下面演示Tomcat7版本的乱码问题
解决GET请求乱码问题:
服务器端:
方法一
String username = request.getParameter("username");
username = new String(username.getBytes("iso-8859-1"),"UTF-8");
方法二(GET和POST通用方法)
在服务端代码开头加上下面两行代码
request.setCharacterEncoding("utf-8");//对客户端utf-8解码
response.setContentType("text/html;charset=utf-8");//设置客户端编码为utf-8
/*
setContentType()有两个作用:
1.通知容器,在调用out.println()方法时,使用指定字符集.
2.生成一个消息头(content-type),通知浏览器,服务器返回的数据类型和字符集
*/
客户端:
<meta http-equiv="content- type"content="text/html;charset=utf-8">
解决POST请求乱码问题
服务器端:
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
客户端:
<meta htp-equiv="content-type" content="text/html;charset=utf-8">