文章目录
四、HTTP请求消息
4.1 概念及特点
Hyper Text Transfer Protocol 超文本传输协议
传输协议:定义了,客户端和服务器端通信时,发送数据的格式
特点:
- 基于
TCP/IP
的高级协议 - 默认端口号:
80
- 基于请求/响应模型的:一次请求对应一次响应
- 无状态的:每次请求之间相互独立,不能交互数据
历史版本:
-
1.0
:每一次请求响应都会建立新的连接 -
1.1
:复用连接
4.2 请求消息数据格式
4.2.1 请求行
格式:请求方式 请求url 请求协议/版本
GET /login.html HTTP/1.1
HTTP
协议有7
种请求方式,常用的有2
种:
[1]GET
:
- 请求参数在请求行中,在
url
后 - 请求的
url
长度有限制的 - 不太安全
[2]POST
:
- 请求参数在请求体中【参数位置与
GET
不一样】 - 请求的
url
长度没有限制的 - 相对安全
4.2.2 请求头
请求头用于浏览器告诉服务器一些信息
格式:请求头名称: 请求头值
常见的请求头:
User-Agent
:浏览器告诉服务器,我访问你使用的浏览器版本信息
可以在服务器端获取该头的信息,解决浏览器的兼容性问题
Referer
:http://localhost/login.html
- 告诉服务器,我(当前请求)从哪里来?
- 作用:防盗链;统计工作。
4.2.3 请求空行
空行,用于分割POST
请求的请求头和请求体。
4.2.4 请求体(正文)
请求体封装POST
请求消息的请求参数。【GET
无】
4.3 字符串格式
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
5.1 request对象和response对象的原理
【注】
request
和response
对象是由服务器创建的。我们来使用它们request
对象是来获取请求消息,response
对象是来设置响应消息
5.2 request对象继承体系结构
ServletRequest – 接口
| 继承
HttpServletRequest – 接口
| 实现
org.apache.catalina.connector.RequestFacade 类(tomcat)
5.3 request获取请求消息数据功能
5.3.1 获取请求行数据
GET /day14/demo1?name=zhangsan HTTP/1.1
方法:
- 获取请求方式 :
GET
String getMethod()
- **(*)**获取虚拟目录:
/day14
String getContextPath()
- 获取
Servlet
路径:/demo1
String getServletPath()
- 获取
get
方式请求参数:name=zhangsan
String getQueryString()
- **(*)**获取请求
URI
:/day14/demo1
-
String getRequestURI()
:/day14/demo1
-
StringBuffer getRequestURL()
:http://localhost/day14/demo1 -
URL
(统一资源定位符): http://localhost/day14/demo1 中华人民共和国 -
URI
(统一资源标识符):/day14/demo1
共和国
- 获取协议及版本:
HTTP/1.1
String getProtocol()
- 获取客户机的
IP
地址:
String getRemoteAddr()
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/RequestDemo")
public class RequestDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//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方式请求参数:
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);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
5.3.2 获取请求头数据
方法:
- (*)
String getHeader(String name)
:通过请求头的名称获取请求头的值 Enumeration<String> getHeaderNames()
:获取所有的请求头名称【类似迭代器】
[例1]
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;
import java.util.Enumeration;
@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 {
//演示获取请求头数据
//1.获取所有请求头名称
Enumeration<String> headerNames = request.getHeaderNames();
//2.遍历
while(headerNames.hasMoreElements()){
String name = headerNames.nextElement();
//根据名称获取请求头的值
String value = request.getHeader(name);
System.out.println(name+"---"+value);
}
}
}
[例2]
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;
import java.util.Enumeration;
@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("火狐来了...");
}
}
}
[例3]
import javax.servlet.ServletContext;
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;
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
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")){
//正常访问
// 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("想看电影吗?来优酷吧...");
}
}
}
}
5.3.3 获取请求体数据
请求体:只有POST
请求方式,才有请求体,在请求体中封装了POST
请求的请求参数
步骤:
- 获取流对象
BufferedReader getReader()
:获取字符输入流,只能操作字符数据ServletInputStream getInputStream()
:获取字节输入流,可以操作所有类型数据【在文件上传知识点后讲解】
- 再从流对象中拿数据
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.BufferedReader;
import java.io.IOException;
@WebServlet("/requestDemo5")
public class RequestDemo5 extends HttpServlet {
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);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
5.4 request其他功能
5.4.1 获取请求参数
获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数
String getParameter(String name)
:根据参数名称获取参数值username=zs&password=123
String[] getParameterValues(String name)
:根据参数名称获取参数值的数组【多用于复选框】hobby=xx&hobby=game
Enumeration<String> getParameterNames()
:获取所有请求的参数名称Map<String,String[]> getParameterMap()
:获取所有参数的map集合
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.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post 获取请求参数
//根据参数名称获取参数值
String username = request.getParameter("username");
//根据参数名称获取参数值的数组
String[] hobbies = request.getParameterValues("hobby");
//获取所有请求的参数名称
Enumeration<String> parameterNames = request.getParameterNames();
// 获取所有参数的map集合
Map<String, String[]> parameterMap = request.getParameterMap();
//遍历
Set<String> keyset = parameterMap.keySet();
for (String name : keyset) {
//获取键获取值
String[] values = parameterMap.get(name);
System.out.println(name);
for (String value : values) {
System.out.println(value);
}
System.out.println("-----------------");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get 获取请求参数
this.doPost(request,response);
}
}
[注]中文乱码问题:
get
方式:tomcat 8
已经将get
方式乱码问题解决了post
方式:会乱码
- 解决:在获取参数前,设置
request
的编码request.setCharacterEncoding("utf-8");
5.4.2 请求转发
一种在服务器内部的资源跳转方式。
步骤:
-
通过
request
对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
-
使用
RequestDispatcher
对象来进行转发:forward(ServletRequest request, ServletResponse response)
特点:
-
浏览器地址栏路径不发生变化
-
只能转发到当前服务器内部资源中
-
转发是一次请求
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/RequestDemo")
public class RequestDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo8888被访问了。。。");
//转发到demo1资源
request.getRequestDispatcher("/RequestDemo1").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
5.4.3 共享数据
- 域对象:一个有作用范围的对象,可以在范围内共享数据
request
域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据- 方法:
request.xx
void setAttribute(String name,Object obj)
:存储数据Object getAttitude(String name)
:通过键获取值void removeAttribute(String name)
:通过键移除键值对
5.4.4 获取ServletContext
ServletContext getServletContext()
ServletContext
对象的功能将在下节讲解。
六、案例:用户登录
6.1 用户登录案例需求
1.编写login.html
登录页面 username & password
两个输入框
2.使用Druid
数据库连接池技术,操作mysql
,day14数据库中user表
3.使用JdbcTemplate
技术封装JDBC
4.登录成功跳转到SuccessServlet
展示:登录成功!用户名,欢迎您
5.登录失败跳转到FailServlet
展示:登录失败,用户名或密码错误
6.2 开发步骤
6.2.1 创建项目和数据库环境
-
创建项目,导入
html
页面,配置文件,jar
包注意:
- 需使用
maven
框架创建项目! - 项目结构如下,需注意
druid.properties
配置文件需放在resources
文件夹下。
- 需使用
-
创建数据库环境
6.2.2 创建类User
创建类User
,并提供getter
和setter
方法
/**
* 用户实体类
*/
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
6.2.3 编写JDBC工具类
使用Durid
连接池。
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
/**
* JDBC工具类,使用Druid连接池
*/
public class JDBCUtils {
private static DataSource ds;
static{
try {
//1.加载配置文件
Properties pro = new Properties();
//使用ClassLoader加载配置文件,获取字节输入流
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//2.初始化连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//获取连接池对象
public static DataSource getDataSource(){
return ds;
}
//获取连接Connection对象
public static Connection getConnection() throws Exception{
return ds.getConnection();
}
}
6.2.4 创建UserLogin类,提供login方法
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserLogin {
//声明JDBCTemplate对象共用
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* 登录方法
*/
public User login(User loginUser){
try {
//1.编写sql
String sql = "select * from user where username = ? and password = ?";
//2.调用query方法
User user = template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),
loginUser.getUsername(),loginUser.getPassword());
return user;
} catch (DataAccessException e) {
e.printStackTrace(); //记录日志
return null;
}
}
}
6.2.5 编写LoginServlet类
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/loginServlet")
public class Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取请求参数
String username = request.getParameter("username");
String password = request.getParameter("password");
//3.封装user对象
User loginUser = new User();
loginUser.setUsername(username);
loginUser.setPassword(password);
//4.调用UserLogin的login方法
UserLogin userLogin = new UserLogin();
User user = userLogin.login(loginUser); //查询到的user
//5.判断user
if(user == null)
{
//登录失败
request.getRequestDispatcher("/failServlet").forward(request,response);
}else
{
//登录成功
request.setAttribute("user",user); //存储数据,键为"user",值为user对象
request.getRequestDispatcher("/successServlet").forward(request,response);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
6.2.6 编写FailServlet和SuccessServlet类
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;
@WebServlet("/successServlet")
public class SuccessServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取request域中共享的user对象
User user = (User)request.getAttribute("user");
//给页面写一句话
if(user != null) //防止空指针异常
{
//设置编码
response.setContentType("text/html;charset=utf-8");
//输出
response.getWriter().write("登录成功!"+user.getUsername()+"欢迎您");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/failServlet")
public class FailServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//给页面写一句话
//设置编码
response.setContentType("text/html;charset=utf-8");
//输出
response.getWriter().write("登录失败!用户名或密码错误");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
【备注】login.html中form表单的action路径的写法:虚拟目录+Servlet的资源路径
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/loginServlet" method="post">
用户名:<input type="text" name="username"> <br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
6.2.7 演示
输入正确:
输入错误: