1. Servlet
2. HTTP协议
3. Request
在上一次笔记中已经讲解了servlet 概念、执行步骤、执行原理、生命周期和Servlet3.0 注解配置,今天接着讲解Servlet的体系结构。
Servlet的体系结构
Servlet -- 接口
|
GenericServlet -- 抽象类
|
HttpServlet -- 抽象类
* GenericServlet:将Servlet接口中其他的方法做了默认空实现,只将service()方法作为抽象
* 将来定义Servlet类时,可以继承GenericServlet,实现service()方法即可
* HttpServlet:对http协议的一种封装,简化操作
1. 定义类继承HttpServlet
2. 复写doGet/doPost方法
Servlet相关配置
1. urlpartten:Servlet访问路径
1. 一个Servlet可以定义多个访问路径 : @WebServlet({"/d4","/dd4","/ddd4"})
2. 路径定义规则:
1. /xxx:路径匹配
2. /xxx/xxx:多层路径,目录结构
3. *.do:扩展名匹配
* 概念:Hyper Text Transfer Protocol 超文本传输协议
* 传输协议:定义了,客户端和服务器端通信时,发送数据的格式
* 特点:
1. 基于TCP/IP的高级协议
2. 默认端口号:80
3. 基于请求/响应模型的:一次请求对应一次响应
4. 无状态的:每次请求之间相互独立,不能交互数据
* 历史版本:
* 1.0:每一次请求响应都会建立新的连接
* 1.1:复用连接
* 请求消息数据格式
1. 请求行
请求方式 请求url 请求协议/版本
GET /login.html HTTP/1.1
* 请求方式:
* HTTP协议有7中请求方式,常用的有2种
* GET:
1. 请求参数在请求行中,在url后。
2. 请求的url长度有限制的
3. 不太安全
* POST:
1. 请求参数在请求体中
2. 请求的url长度没有限制的
3. 相对安全
2. 请求头:客户端浏览器告诉服务器一些信息
请求头名称: 请求头值
* 常见的请求头:
1. User-Agent:浏览器告诉服务器,我访问你使用的浏览器版本信息
* 可以在服务器端获取该头的信息,解决浏览器的兼容性问题
2. Referer:http://localhost/login.html
* 告诉服务器,我(当前请求)从哪里来?
* 作用:
1. 防盗链:
2. 统计工作:
3. 请求空行
空行,就是用于分割POST请求的请求头,和请求体的。
4. 请求体(正文):
* 封装POST请求消息的请求参数的
* 字符串格式:
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
1. request对象和response对象的原理
1. request和response对象是由服务器创建的。我们来使用它们。
2. request对象是来获取请求消息,response对象是来设置响应消息。
2. request对象继承体系结构:
ServletRequest -- 接口
| 继承
HttpServletRequest -- 接口
| 实现
org.apache.catalina.connector.RequestFacade 类(tomcat)
3. request功能:
1. 获取请求消息数据
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 getRequestURI(): /day14/demo1
* StringBuffer getRequestURL() :http://localhost/day14/demo1
* URL:统一资源定位符 : http://localhost/day14/demo1 中华人民共和国
* URI:统一资源标识符 : /day14/demo1 共和国
6. 获取协议及版本:HTTP/1.1
* String getProtocol()
7. 获取客户机的IP地址:
* String getRemoteAddr()
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; /** * 演示Request对象获取请求行数据 */ @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 { /* 1. 获取请求方式 :GET * String getMethod() 2. (*)获取虚拟目录:/day14 * String getContextPath() 3. 获取Servlet路径: /requestDemo1 * String getServletPath() 4. 获取get方式请求参数:name=zhangsan * String getQueryString() 5. (*)获取请求URI:/day14/demo1 * String getRequestURI(): /day14/requestDemo1 * StringBuffer getRequestURL() :http://localhost/day14/requestDemo1 6. 获取协议及版本:HTTP/1.1 * String getProtocol() 7. 获取客户机的IP地址: * String getRemoteAddr() */ //1. 获取请求方式 :GET String method = request.getMethod(); System.out.println(method); //2.(*)获取虚拟目录:/day14 String contextPath = request.getContextPath(); System.out.println(contextPath); //3. 获取Servlet路径: /demo1 String servletPath = request.getServletPath(); System.out.println(servletPath); //4. 获取get方式请求参数:name=zhangsan String queryString = request.getQueryString(); System.out.println(queryString); //5.(*)获取请求URI:/day14/demo1 String requestURI = request.getRequestURI(); StringBuffer requestURL = request.getRequestURL(); System.out.println(requestURI); System.out.println(requestURL); //6. 获取协议及版本:HTTP/1.1 String protocol = request.getProtocol(); System.out.println(protocol); //7. 获取客户机的IP地址: String remoteAddr = request.getRemoteAddr(); System.out.println(remoteAddr); } }
输出结果:
GET
/HttpTest_war_exploded
/requestDemo01
null
/HttpTest_war_exploded/requestDemo01
http://localhost:8080/HttpTest_war_exploded/requestDemo01
HTTP/1.1
0:0:0:0:0:0:0:1
2. 获取请求头数据
* 方法:
* (*)String getHeader(String name):通过请求头的名称获取请求头的值
* Enumeration<String> getHeaderNames():获取所有的请求头名称
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //演示获取请求头数据 //1.获取所有请求头名称 Enumeration<String> headerNames = request.getHeaderNames(); //2.遍历 while(headerNames.hasMoreElements()){ String name = headerNames.nextElement(); //根据名称获取请求头的值 String value = request.getHeader(name); System.out.println(name+"---"+value); } }
host---localhost:8080 connection---keep-alive cache-control---max-age=0 upgrade-insecure-requests---1 user-agent---Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36 accept---text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 sec-fetch-site---none sec-fetch-mode---navigate sec-fetch-user---?1 sec-fetch-dest---document accept-encoding---gzip, deflate, br accept-language---zh-CN,zh;q=0.9 cookie---JSESSIONID=B91E596B2CC2DC7785CCBA1BBE99A871; Pycharm-7c531bbf=04307420-89bd-48fd-a81f-237f7134473e; Idea-a98ae54d=9696be39-df73-4fee-b539-193c5bd1720e; remember-me=YWRtaW46MTYyMTQxNTQwOTQ1ODpmODdlZWU5NWU5ZGFlNjkyODU0ZTYzNjMxYjQ2ZTI2Yg
@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 { //演示获取请求头数据:user-agent String agent = request.getHeader("user-agent"); //判断agent的浏览器版本 if(agent.contains("Chrome")){ //谷歌 System.out.println("谷歌来了..."); }else if(agent.contains("Firefox")){ //火狐 System.out.println("火狐来了..."); } }
通过获取referer值判断访问来源,防止盗链
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //演示获取请求头数据:referer String referer = request.getHeader("referer"); System.out.println(referer);//http://localhost/day14/login.html //防盗链 if(referer != null ){ if(referer.contains("/day14")){ //正常访问 站点虚拟路径day14 // System.out.println("播放电影...."); response.setContentType("text/html;charset=utf-8"); response.getWriter().write("播放电影...."); }else{ //盗链 //System.out.println("想看电影吗?来优酷吧..."); response.setContentType("text/html;charset=utf-8"); response.getWriter().write("想看电影吗?来优酷吧..."); } } }
3. 获取请求体数据:
* 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数
* 步骤:
1. 获取流对象
* BufferedReader getReader():获取字符输入流,只能操作字符数据
* ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据
* 在文件上传知识点后讲解
2. 再从流对象中拿数据
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求消息体--请求参数 //1.获取字符流 BufferedReader br = request.getReader(); //2.读取数据 String line = null; while((line = br.readLine()) != null){ System.out.println(line); } }
2. 其他功能:
1. 获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数
1. String getParameter(String name):根据参数名称获取参数值 username=zs&password=123
2. String[] getParameterValues(String name):根据参数名称获取参数值的数组 hobby=xx&hobby=game
3. Enumeration<String> getParameterNames():获取所有请求的参数名称
4. Map<String,String[]> getParameterMap():获取所有参数的map集合
* 中文乱码问题:
* get方式:tomcat 8 已经将get方式乱码问题解决了
* post方式:会乱码
* 解决:在获取参数前,设置request的编码request.setCharacterEncoding("utf-8");
@WebServlet("/requestDemo03") public class RequestDemo03 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //String username = request.getParameter("username"); //Map<String, String[]> parameterMap = request.getParameterMap(); /* String[] hobbies = request.getParameterValues("hobby"); for (String hobby:hobbies) { System.out.println(hobby); } */ Enumeration<String> parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()){ String name = parameterNames.nextElement(); System.out.println(name); //String parameter = request.getParameter(name); String[] parameterValues = request.getParameterValues(name); for (String parameterValue:parameterValues ) { System.out.println(name+"-------"+parameterValue); } //System.out.println(parameter); System.out.println("-------------"); } /* Map<String, String[]> parameterMap = request.getParameterMap(); Set<String> keySet = parameterMap.keySet(); for(String name : keySet){ String[] strings = parameterMap.get(name); System.out.println(strings); for(String value:strings){ System.out.println(value); } }*/ } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
解决乱码问题:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.设置流的编码 request.setCharacterEncoding("utf-8"); //获取请求参数username String username = request.getParameter("username"); System.out.println(username); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
2. 请求转发:一种在服务器内部的资源跳转方式
1. 步骤:
1. 通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
2. 使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response)
2. 特点:
1. 浏览器地址栏路径不发生变化
2. 只能转发到当前服务器内部资源中。
3. 转发是一次请求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/HttpTest_war_exploded/requestDemo06" method="post"> <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">学习 <input type="submit" value="注册"> </form> </body> </html>
@WebServlet("/requestDemo06") public class RequestDemo06 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo05"); requestDispatcher.forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
@WebServlet("/requestDemo05") public class RequestDemo05 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo04"); requestDispatcher.forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
@WebServlet("/requestDemo04") public class RequestDemo04 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.设置流的编码 request.setCharacterEncoding("utf-8"); //获取请求参数username String username = request.getParameter("username"); System.out.println(username); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }