1 HTTP协议介绍
2.HttpServletRequest类
3.HttpServletResponse类
4.请求转发与请求重定向对比
1 HTTP协议介绍
a)HTTP协议
1.HTTP(hypertext transport protocol),即超文本传输协议。这个协议详细规定了浏览器和万维网服务器之间互相通信的规则。
2.客户端与服务端通信时传输的内容我们称之为报文。
3.HTTP就是一个通信规则,这个规则规定了客户端发送给服务器的报文格式,
也规定了服务器发送给客户端的报文格式。实际我们平常用到的就是这两种报文。
客户端发送给服务器的称为”请求报文“,服务器发送给客户端的称为”响应报文“。
大白话说,什么是协议。是双方相互约定好的规则;
比如:租房协议:租房协议就是租客和房东之间相互约定好的租房规则
b)、请求的协议格式
请求的HTTP协议格式
请求首行;
请求头信息;
空行;
请求体;
GET请求协议格式
POST请求协议格式
c)、常见请求头的说明
GET /Hello/index.jsp HTTP/1.1:GET请求,请求服务器路径为Hello/index.jsp,协议为1.1;
Host:localhost:请求的主机名为localhost;
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0…:与浏览器和OS相关的信息。有些网站会显示用户的系统版本和浏览器版本信息,这都是通过获取User-Agent头信息而来的;
Accept: */*:告诉服务器,当前客户端可以接收的文档类型, */*,就表示什么都可以接收;
Accept-Language: zh-CN:当前客户端支持的语言,可以在浏览器的工具à选项中找到语言相关信息;
Accept-Encoding: gzip, deflate:支持的压缩格式。数据在网络上传递时,服务器会把数据压缩后再发送;
Connection: keep-alive:客户端支持的链接方式,保持一段时间链接。
d)、get请求和post请求都分别是哪些操作?
GET请求
1)、在浏览器地址栏中输入地址直接按回车
2)、点击超链接 <a>
3)、GET请求表单提交 <form mehtod=”get”>
4)、script src=””,引入外部文件
5)、img src=”路径”,引入图片
6)、引入外部css。。。
POST请求
1)只有表单提交的时候method=post,提交表单就是发post请求 <form method=”POST” />
e)、响应的协议格式
响应的HTTP协议格式
响应首行
响应头信息
空行
响应体
f)、常见的响应码
响应码对浏览器来说很重要,它告诉浏览器响应的结果,常见的响应状态码是:
200 请求成功
404 服务器已经收请求,但是请求的资源不存在
405 表示当前请求的GET或POST请求不支持
500 服务器已经收到请求,但是服务器错误
302 表示请求重定向。
g)、MIME类型
MINE是HTTP协议中数据类型。
MIME的英文全称是"Multipurpose Internet Mail Extensions" 多功能Internet 邮件扩充服务。MIME类型的格式是“大类型/小类型”,并与某一种文件的扩展名相对应。
MIME是数据类型的字符串描述符“大类型/小类型”
常见的MIME类型:
文件 |
MIME类型 |
超文本标记语言文本 |
.html,.html text/html |
普通文本 |
.txt text/plain |
RTF文本 |
.rtf application/rtf |
GIF图形 |
.gif image/gif |
JPEG图形 |
.jpeg,.jpg image/jpeg |
au声音文件 |
.au audio/basic |
MIDI音乐文件 |
mid,.midi audio/midi,audio/x-midi |
RealAudio音乐文件 |
.ra, .ram audio/x-pn-realaudio |
MPEG文件 |
.mpg,.mpeg video/mpeg |
AVI文件 |
.avi video/x-msvideo |
GZIP文件 |
.gz application/x-gzip |
TAR文件 |
.tar application/x-tar |
2.HttpServletRequest类
a) HttpServletRequest类有什么作用。
HttpServletRequest表示请求的信息。它封装了所有请求过来的数据,由Tomcat服务器在每次请求进来的时候创建,并且封装。然后传递给doGet或doPost方法使用。我们可以使用这个方法获取到所有请求的信息。
b) HttpServletRequest类的常用方法
i. getRequestURI()获取请求的资源路径
ii. getRequestURL()获取请求的绝对路径(统一资源定位符)
iii. getRemoteHost()获取客户端的ip
iv. getHeader()获取请求头信息
v. getParameter() 获取请求的参数值
vi. getParameterValues()获取请求的参数值(多个值)
vii. getMethod()获取请求的方式GET或POST
------------------request对象,也是域对象---------------------
viii. setAttribute(key, value);保存数据
ix. getAttribute(key); 获取数据
-------------------------------------------------------
x. getRequestDispatcher() 获取请求转发对象
-------------------------------------------------------
xi. getCookies()获取Cookie对象
xii. getSession()获取会话Session对象
c) 如何获取请求参数
表单的代码
<body> <form action="http://localhost:8080/hello/parameterServlet" method="get"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password" /><br/> 兴趣爱好: <input type="checkbox" name="hobby" value="cpp"/> C++ <input type="checkbox" name="hobby" value="java"/> java <input type="checkbox" name="hobby" value="js"/> javaScript<br/> <input type="submit" /> </form> </body>
服务器接收参数的代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取用户名 String username = request.getParameter("username"); // 获取密码 String password = request.getParameter("password"); // 获取兴趣爱好 String[] hobbies = request.getParameterValues("hobby"); System.out.println("用户名:" + username); System.out.println("密码:" + password); if (hobbies != null) { for (String string : hobbies) { System.out.println("兴趣爱好:" + string); } } }
d) GET请求的中文乱码解决
解决方案一:
//1 使用iso-8859-1编码操作
//2 使用UTF-8进行解码
username= new String(username.getBytes("iso-8859-1"),"UTF-8");
GET请求解决方案二:
找到Tomcat服务器的配置文件Server.xml配置文件,找到如下的内容修改:
<ConnectorURIEncoding="UTF8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
e) POST请求的中文乱码解决
方案一(不推荐):也可以使用GET请求的中文乱码解决方案
//1 使用iso-8859-1编码操作
//2 使用UTF-8进行解码
username= new String(username.getBytes("iso-8859-1"),"UTF-8");
方案二:
// 设置请求体的字符集为UTF-8
// 要求必须在getParameter之前调用。
request.setCharacterEncoding("UTF-8");
f) 请求的转发(在jsp的时候)
以银行处理业务为例
参数是在浏览器地址栏中或表单 中获取的,
属性是在代码中赋值与获取的
Servlet1程序的代码:
public class Servlet1 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1 查看是否带了办事的材料 --- 请求的参数 System.out.println( "从Servlet1中获取请求的参数(查看办事的材料):" + request.getParameter("username") ); //2 处理柜台1的功能,--- 盖个章-----保存到request域对象中 request.setAttribute("key", "盖了一个章"); System.out.println("Servlet1柜台发现材料没问题,盖了章。"); //3 流转到下一个柜台----请求转发给Servlet2程序 // 斜线在服务器中,表示到http://ip:port/工程名/ 映射到代码的WebContent目录 // 只是获取到请求转发对象 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/servlet2"); // RequestDispatcher requestDispatcher = request.getRequestDispatcher("http://www.baidu.com"); System.out.println("Servlet1并从柜台1处得到下一个柜台2的位置,然后移动到下一个柜台"); // 真正的转发到请求转发对象中的地址所表示的资源 requestDispatcher.forward(request, response); } }
Servlet2程序的代码:
public class Servlet2 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 先看材料 --- 请求的参数 System.out.println("Servlet2中看材料(请求参数)" + request.getParameter("username")); // 查看地是否有章 Object value = request.getAttribute("key"); // 不等于null,说明有盖章 if(value != null) { System.out.println("在Servlet2中查看是否有章:[" + value + "]" ); System.out.println("处理柜台2中也需要输的业务"); } else { System.out.println("柜台1还没有办理,请去柜台1"); } } }
请求转发测试的地址是:http://localhost:8080/hello/servlet1?username=wzg168
g) Base标签的作用
h) Web中的相对路径和绝对路径
相对路径:
. 当前目录
.. 上一级目录
资源名 当前目录/资源名
绝对路径:
http://ip:port/工程名/资源名
i) web中/斜杠的不同意义
web中斜杠也表示绝对路径。
/在html标签中,表示http://ip:port/
斜杠在服务器中,表示到http://ip:port/工程名/
web.xml中配置
<url-pattern>/http</url-pattern>
/http 表示:http://ip:port/工程名/http
ServletContext.getRealPath("/") 斜杠就表示到http://ip:port/工程名/ 映射到WebContent目录
request.getRequestDispatcher("/servlet2"); 斜杠就表示到http://ip:port/工程名/ 映射到WebContent目录
例外:
response.sendRedirect(“/”); 表示http://ip:port/
3.HttpServletResponse类
a) HttpServletResponse类的作用
httpServletResponse类表示响应。所有返回给客户端的信息,都可以通过response类进行设置
b) 两个输出流的说明。
字节流 getOutPutStream()
字符流 getWriter()
两个流只能使用一个。同时使用两个,最后一个就会报错
c) 如何往客户端回传数据
我要给客户端回传字符串。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter writer = response.getWriter(); writer.write("this is the content of response"); }
d) 响应的乱码解决
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置服务器输出的数据使用UTF-8字符集 response.setCharacterEncoding("UTF-8"); // 告诉客户端使用UTF-8字符集查看页面数据 response.setHeader("Content-Type", "text/html; charset=UTF-8"); PrintWriter writer = response.getWriter(); writer.write("返回中文"); }
解决响应中文乱码的第二种方案
// 同时设置服务器和客户端都使用UTF-8字符集(推荐使用这种方案解决响应的中文乱码) // 一定要在获取响应流之前调用 response.setContentType("text/html; charset=UTF-8");
e) 如何设置响应头和响应状态码
// 设置响应状态码为302.表示需要重定向
response.setStatus(302);
// 设置响应头 Location
response.setHeader("Location", "http://localhost:8080/hello/response2");
注:el表达式中也有一个Location href="http://localhost:8080/hello/response2";
请求重定向第二种方案(官方推荐)
// 设置请求重定向
response.sendRedirect("http://localhost:8080/hello/response2");
3.请求和转发的对比
|
转发 |
重定向 |
浏览器地址栏 |
没有变化 |
有变化 |
几次请求 |
1 |
2次 |
API |
request |
response |
WEB-INF |
可以访问 |
不能访问 |
共享request请求域数据 |
共享 |
不共享 |
目标资源 |
只能是工程内的资源 |
任意资源(工程内,工程外) |
1、2、浏览器地址栏变化与请求次数:
Request请求转发:
Response重定向:
3、WEB-INF:
Request请求转发可以访问WEB-INF目录:
request.getRequestDispatcher("/aaa/bbb/ccc.html").forward(request, response);
Response重定向不可以访问WEB-INF目录:
response.sendRedirect("http://localhost:8080/hello/WebContent/aaa/bbb/ccc.html");
5、共享request请求:
Request请求转发:源文件:request.setAttribute("key", "enheng");
目标文件:Object attribute = request.getAttribute("key");
返回enheng,可以共享
Response重定向:源文件:request.setAttribute("key", "一个章");
目标文件:request.getAttribute("key")
返回null,不可以共享
6、目标资源:
请求转发不可以访问工程外
重定向可以访问工程外
response.sendRedirect("http://www.baidu.com");
因为response的路径/表示:http://ip:端口,request的路径/表示http://ip:端口/工程名/
Request请求转发与Response重定向:
1)、request1→request2请求转发:
request.setAttribute("key", "enheng");
request.getRequestDispatcher("/servlet2").forward(request, response);
2)、response1→response2重定向:
response.setStatus(302);
response.setHeader("Location", "http://localhost:8080/hello/response2");
不过官方推荐:response.sendRedirect("http://localhost:8080/hello/response2");
服务端响应response客户端汉字乱码问题方法:
response.setCharacterEncoding("utf-8");
response.setHeader("content-type", "text/html;charset=utf-8");
不过官方推荐
response.setContentType("text/html;charset=utf-8");
GET请求的中文乱码解决
解决方案一:(推荐)
//1 使用iso-8859-1编码操作
//2 使用UTF-8进行解码
username= new String(username.getBytes("iso-8859-1"),"UTF-8");
GET请求解决方案二:
找到Tomcat服务器的配置文件Server.xml配置文件,找到如下的内容修改:
<ConnectorURIEncoding="UTF8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
POST请求的中文乱码解决
方案一(不推荐):也可以使用GET请求的中文乱码解决方案
//1 使用iso-8859-1编码操作
//2 使用UTF-8进行解码
username= new String(username.getBytes("iso-8859-1"),"UTF-8");
方案二:(不过官方推荐)
// 设置请求体的字符集为UTF-8
// 要求必须在getParameter之前调用。
request.setCharacterEncoding("UTF-8");
编码规范大小写都可以