一、HTTP概念
Hyper Text Transfer Protocol 超文本传输协议
传输协议: 定义了在客户端和服务器端通信时,发送数据的格式。
特点:
- 基于TCP / IP的高级协议
- 默认端口号:80
- 基于请求 / 响应模型,一次请求对应一次响应
- 无状态的:每次请求之间相互独立,不能交互数据
历史版本: 1.0:每次请求响应都会建立新的链接 1.1:节省资源,会复用链接
就例如访问百度,会产生多次请求,多次响应。 1.0版本会请求一次资源就建立一个链接。 1.1版本会请求一个资源,传输完成后,用该链接等待下一个资源过来。
二、请求消息数据格式
2.1 请求行
格式:
请求方式 请求url 请求协议/版本
GET /login.html HTTP/1.1
请求方式: HTTP协议中有七种请求方式,常用的有两种。
GET:
1.请求参数在请求行中,在url链接的后面。
2.请求的url链接长度是有限制的。
3.不太安全,可以直接在url链接看到信息。
POST:
1.请求参数在请求体中。
2.请求的url长度没有限制。
3.相对安全。
2.2 请求头
客户端浏览器告诉服务器一些信息。
常见的请求头:
- User-Agent:浏览器告诉服务器,我访问你使用的浏览器版本信息。
用于解决浏览器的兼容性问题。 - Referer:告诉服务器,当前请求来源。
用于进行统计工作、防盗链(盗版网站的由来)作用。
2.3 请求空行
就是一段空行,用于分割POST请求的请求头和请求体的。
2.4 请求体(正文)
封装POST请求信息的请求参数。
2.5 请求消息格式
火狐浏览器的请求消息格式:
(请求行)POST /login.html HTTP/1.1
(请求头)
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101
Firefox/60.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: http://localhost/login.html
Connection: keep-alive
Upgrade-Insecure-Requests: 1
(请求空行)
(请求体)
username=zhangsan
三、Request对象
在我们简化到最后的servlet中,还有个方法,其中有两个参数request和response。
3.1 request对象和response对象的原理
- request和response对象是由服务器创建的。我们来使用它们。
- request对象是来获取请求消息,response对象是来设置响应消息。
3.2 request对象继承体系结构
ServletRequest -- 接口
| 继承
HttpServletRequest -- 接口
| 实现
org.apache.catalina.connector.RequestFacade类(tomcat)
3.3 request功能
3.3.1获取请求消息数据
①获取请求行数据
假设请求行数据为:
GET /day14/demo1?name=zhangsan HTTP/1.1
方法:
1.获取请求方式:GET
String getMethod()
*2.获取虚拟目录:/day14
String getContextPath()
3.获取Servlet路径:/demo1
String getServletPath()
4.获取get方式请求参数:name=zhangsan
String getQueryString()
*5.获取请求URI:/day14/demo1
String getRequestUrRI() -- /day14/demo1
StringBuffer getRequestURL() -- http://localhost/day14/demo1
URL和URI区别:
URL:统一资源定位符:http://localhost/day14/demo1
URI:统一资源标识符(范围相对大一些):day14/demo1
6.获取协议以及版本:HTTP/1.1
String getProtocol()
7.获取客户机的IP地址:
String getRemoteAddr()
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getMethod();
System.out.println(method);
String contextPath = request.getContextPath();
System.out.println(contextPath);
String servletPath = request.getServletPath();
System.out.println(servletPath);
String queryString = request.getQueryString();
System.out.println(queryString);
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURI);
System.out.println(requestURL);
String protocol = request.getProtocol();
System.out.println(protocol);
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}
}
②获取请求头数据
(*)String getHeader(String name):提供请求头名称获取请求头数据
Enumeration<String> getHeaderNames():获取所有的请求头名称
@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()){
String name = headerNames.nextElement();
String value = request.getHeader(name);
System.out.println(name+"---"+value);
}
}
}
@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String agent = request.getHeader("user-agent");
if (agent.contains("Chrome")){
System.out.println(agent);
System.out.println("谷歌...");
}else if (agent.contains("Firefox")){
System.out.println("火狐...");
}
}
}
③获取请求体数据
请求体:只有POST请求方式,才有请求体。在请求体中封装了POST请求的请求参数。
步骤:
- 获取流对象
BufferedReader getReader():获取字符输入流,只能操作字符数据 ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据 - 再从流对象中拿数据
@WebServlet("/demo5")
public class RequestDemo5 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BufferedReader br = req.getReader();
String line = null;
while ((line=br.readLine())!=null){
System.out.println(line);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
regist.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/day14/demo5" method="post">
<input type="text" placeholder="请输入用户名" name="username"<br>
<input type="text" placeholder="请输入密码" name="password"<br>
<input type="submit" value="注册">
</form>
</body>
</html>
3.3.2 其他功能
①获取请求参数通用方式
不论get还是post请求方式都可以使用下列方法来获取请求参数。
- String getParameter(String name):根据参数名称获取参数值 username=zs&password=123
- String[] getParameterValues(String name):根据参数名称获取参数值的数组hobby=xx&hobby=game
- Enumeration getParameterNames():获取所有请求的参数名称
- Map<String,String[]> getParameterMap():获取所有参数的map集合
@WebServlet("/demo6")
public class RequestDemo6 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
System.out.println("post");
System.out.println(username);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
System.out.println("get");
System.out.println(username);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/day14/demo6" method="get">
<input type="text" placeholder="请输入用户名" name="username"<br>
<input type="text" placeholder="请输入密码" name="password"<br>
<input type="submit" value="注册">
</form>
</body>
</html>
其实这样,get和post代码相似,以后只用写一遍就好了。
@WebServlet("/demo6")
public class RequestDemo6 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
System.out.println("post");
System.out.println(username);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
@WebServlet("/demo6")
public class RequestDemo6 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String[] hobbies = req.getParameterValues("hobby");
for (String hobby : hobbies) {
System.out.println(hobby);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/day14/demo6" method="get">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="checkbox" name="hobby" value="game">游戏
<input type="checkbox"name="hobby"value="study">学习
<br>
<input type="submit" value="注册">
</form>
</body>
</html>
@WebServlet("/demo6")
public class RequestDemo6 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Enumeration<String> parameterNames = req.getParameterNames();
while (parameterNames.hasMoreElements()){
String name = parameterNames.nextElement();
System.out.println(name);
String value = req.getParameter(name);
System.out.println(value);
System.out.println("-------------------------");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/day14/demo6" method="get">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="checkbox" name="hobby" value="game">游戏
<input type="checkbox"name="hobby"value="study">学习
<br>
<input type="submit" value="注册">
</form>
</body>
</html>
@WebServlet("/demo6")
public class RequestDemo6 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Map<String, String[]> parameterMap = req.getParameterMap();
Set<String> key = parameterMap.keySet();
for (String name : key) {
String[] values = parameterMap.get(name);
System.out.println(name);
for (String value : values) {
System.out.println(value);
}
System.out.println("------------------------");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/day14/demo6" method="get">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="checkbox" name="hobby" value="game">游戏
<input type="checkbox"name="hobby"value="study">学习
<br>
<input type="submit" value="注册">
</form>
</body>
</html>
中文乱码问题: get方式:tomcat 8已经把get方式乱码问题解决。 post方法:还会乱码,只能在获取参数前,设置request编码request.setCharacterEncoding(“utf-8”)。
②请求转发
一种在服务器内部的资源跳转方式。 步骤:
- 通过request对象获取请求转发器对象:RequestDispatchergetRequestDispatcher(String path)
- 使用RequestDispatcher对象来进行转发:forward(ServletRequest request,ServletResponse response)
@WebServlet("/demo8")
public class RequestDemo8 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo8...");
req.getRequestDispatcher("/demo9").forward(req,resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
@WebServlet("/demo9")
public class RequestDemo9 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo9...");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
特点:
- 浏览器地址栏路径不发生变化
- 只能转发到当前服务器内部资源中
- 转发是一次请求
③共享数据
先说个概念 域对象:一个有作用范围的对象,范围内能共享数据。 request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据。
方法:
- void setAttribute(String name,Object obj):存储数据
- Object getAttitude(String name):通过键获取值
- void removeAttribute(String name):通过键移除键值对
@WebServlet("/demo8")
public class RequestDemo8 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo8...");
req.setAttribute("msg","hello demo9...");
req.getRequestDispatcher("/demo9").forward(req,resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
@WebServlet("/demo9")
public class RequestDemo9 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object msg = req.getAttribute("msg");
System.out.println(msg);
System.out.println("demo9...");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
④获取ServletContext
ServletContext getServletContext( )
内容之后补上。。。
|