回顾
1. servlet概述
运行在服务器端java小程序
如果这个java类,想要被浏览器访问到,那么必须直接或间接的实现servlet接口规范
面向(接口)规范开发
2. xml配置servlet【掌握】
编写普通java类,实现servlet接口,重写抽象方法(service)
配置web.xml
3. servlet生命周期
创建?
1)默认情况下,用户第一次访问时创建
2)设置创建时机,在服务器启动时创建
运行?
用户访问时,执行service方法
销毁?
服务器正常关闭时,销毁servlet,执行destroy方法
4. servlet体系结构
servlet
|
genericServlet
|
httpServlet
5. Servlet3.0
编写普通java类,继承httpservlet,重写doGet、doPost
配置@WebServlet("/网络访问地址")
Request
今日目标
1. Request概述
2. reuqest获取请求消息内容
请求行
请求头
请求参数(体)【重点】
3. request其他功能
请求转发
域对象
4. 案例:用户登录
一 Request概述
-
用户通过浏览器访问服务器时,Tomcat将HTTP请求中所有的信息都封装在Request对象中
-
作用:开发人员可以通过request对象方法,来获取浏览器发送的所有信息.
Request体系结构
ServletRequest
|
HttpServletRequest
|
org.apache.catalina.connector.RequestFacade(由tomcat厂商提供实现类)
在javaEE中我们是面向接口编程
二 Request获取Http请求信息
2.1 获取请求行信息
* 例如:
GET /day09_request/requestDemo1 HTTP/1.1
* 相关API:
1. 获取请求方式 GET【掌握】
String getMethod()
2. 获取项目虚拟路径(项目名)/day09_request【掌握】
String getContextPath()
3. 获取URI /day09_request/requestDemo1
统一资源标识符(范围广) 共和国
String getRequestURI()
4. 获取URL http://localhost:8080/day09_request/requestDemo1
统一资源定位符(确定某一个地址) 中华人民共和国
StringBuffer getRequestURL()
5. 获取协议和版本号 HTTP/1.1
String getProtocol()
6. 获取客户端ip
String getRemoteAddr()
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// System.out.println(req);
System.out.println("请求方式:" + req.getMethod());
System.out.println("虚拟路径:" + req.getContextPath());
System.out.println("URI:" + req.getRequestURI());
System.out.println("URL:" + req.getRequestURL());
System.out.println("协议和版本:" + req.getProtocol());
System.out.println("客户端ip:" + req.getRemoteAddr());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
2.2 获取请求头信息
* 例如:
Host: localhost:8080
* 相关API:
1. 获取知道请求头名称对应的值,注:名称不区分大小写
String getHeader(String name)
2. 获取所有请求头的名称
Enumeration<String> getHeaderNames()
注:是Iterator前身
@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取所有的请求头名称
Enumeration<String> enumeration = req.getHeaderNames();
// 遍历
while(enumeration.hasMoreElements()){
// 取出元素名(请求头名称)
String name = enumeration.nextElement();
// 根据名称获取值
String value = req.getHeader(name);
System.out.println(name +" : "+ value);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
① 案例:视频防盗链
- referer : 请求来源
/*
视频防盗链
*/
@WebServlet("/refererRequest")
public class RefererRequest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
// 1.获取请求来源(如果是浏览器地址直接访问的话,referer就是null)
String referer = req.getHeader("referer");
// 2.判断是不是自家网站发起的请求
if (referer != null && referer.startsWith("http://localhost:8080")) {
resp.getWriter().write("正常播放视频...");
}else{
resp.getWriter().write("想看吗?来优酷吧....");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
② 案例:浏览器兼容性
- 现在后端处理兼容性问题的需求越来越少,基本都是前端工程师完成的....
- user-agen:浏览器版本信息
/*
浏览器兼容性
*/
@WebServlet("/userAgentReqeust")
public class UserAgentReqeust extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
// 1.获取浏览器版本信息
String userAgent = req.getHeader("user-agent");
// 2.判断浏览器版本
if (userAgent.contains("Chrome")) {
resp.getWriter().write("浏览器:谷歌");
} else if (userAgent.contains("Firefox")) {
resp.getWriter().write("浏览器:火狐");
} else {
resp.getWriter().write("浏览器:其他");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
2.3 获取请求参数(体)【重点....】
- 不论get还是post请求方式,都可以使用下列方法来获取请求参数
- 在web课程里面,get和post处理的业务逻辑都是一样的....
* 参数
username=jack&password=123&hobby=drink&hobby=perm
* API
1. 获取指定参数名的值 username=jack
String getParameter(String name)
2. 获取指定参数名的值数组 hobby=drink&hobby=perm
String[] getParameterValues(String name)
3. 获取所有参数名和对应值数组,参数名 name(key),值数组 value,封装map集合
Map<String,String[]> getParameterMap()
* 中文乱码【重点】
get:在tomcat8及以上版本,内部URL编码(UTF-8)
post:编码解码不一致,造成乱码现象
客户端(浏览器)编码:UTF-8
服务器默认 解码:ISO-8859-1 拉丁文
指定解码:void setCharacterEncoding(String env)
注:这哥们必须在方法内,行首
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>form</title>
</head>
<body>
<h3>get方式:</h3>
<form action="/day09_request/requestDemo3" method="get">
用户:<input type="text" name="username"> <br>
密码:<input type="password" name="password"> <br>
爱好:
<input type="checkbox" name="hobby" value="somke"/>抽烟
<input type="checkbox" name="hobby" value="drink"/>喝酒
<input type="checkbox" name="hobby" value="perm"/>烫头
<input type="submit" value="get提交...">
</form>
<h3>post方式:</h3>
<form action="/day09_request/requestDemo3" method="post">
用户:<input type="text" name="username"> <br>
密码:<input type="password" name="password"> <br>
爱好:
<input type="checkbox" name="hobby" value="somke"/>抽烟
<input type="checkbox" name="hobby" value="drink"/>喝酒
<input type="checkbox" name="hobby" value="perm"/>烫头
<input type="submit" value="post提交...">
</form>
</body>
</html>
@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("手动获取---------");
String username = req.getParameter("username");
System.out.println("用户:" + username);
String password = req.getParameter("password");
System.out.println("密码:" + password);
String[] hobby = req.getParameterValues("hobby");
System.out.println("爱好:" + Arrays.toString(hobby));
System.out.println("自动获取---------");
Map<String, String[]> parameterMap = req.getParameterMap();
parameterMap.forEach((k, v) -> {
System.out.println(k + " = " + Arrays.toString(v));
});
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 指定post的解码方式....
req.setCharacterEncoding("UTF-8");
System.out.println("post提交方法,再去调用get....");
this.doGet(req, resp); // 让get小老弟干活...
}
}
2.4 BeanUtils
- 这哥们apache组织,提供一套工具类,简化参数的封装.....
- 简单来说:将前端提交的数据,直接封装到你想要的JavaBean中
① 导入jar包
② 使用工具类封装数据
<h3>BeanUtils测试:</h3>
<form action="/day09_request/requestDemo4" method="post">
用户:<input type="text" name="username"> <br>
密码:<input type="password" name="password"> <br>
爱好:
<input type="checkbox" name="hobby" value="somke"/>抽烟
<input type="checkbox" name="hobby" value="drink"/>喝酒
<input type="checkbox" name="hobby" value="perm"/>烫头
<input type="submit" value="post提交...">
</form>
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
// 使用BeanUtils快速封装数据到 User对象中
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");// 指定解码方式
// 1.获取所有参数的数据,map集合
Map<String, String[]> parameterMap = request.getParameterMap();
// 2.创建user对象
User user = new User();
// 3.调用工具类,实现快速封装
try {
BeanUtils.populate(user, parameterMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(user);
}
}
三 Request其他功能
3.1 请求转发
- 一种在服务器内部的资源跳转方式
* API
1. 通过reqeust对象,获得转发器对象
RequestDispatcher getRequestDispatcher(String path)
2. 通过转发器对象,实现转发功能
void forward(ServletRequest request, ServletResponse response)
* 请求转发特点
浏览器:发了一次请求
地址栏:没有发生改变
只能转发到服务器内部资源....
* 链式编程
request.getRequestDispatcher("/bServlet").forward(reqeust,response)
2.2 域对象(共享数据)
- 域对象:一个有作用范围的对象,可以在范围内共享数据
- request域:代表一次请求的范围,一般用于一次请求中转发的多个资源中共享数据
* API
1. 设置数据
void setAttribute(String name, Object o)
2. 获取数据
Object getAttribute(String name)
3. 删除数据
void removeAttribute(String name)
* 生命周期
1. 何时创建?
用户发送请求时,创建request
2. 何时销毁
服务器返回响应是,销毁request
3. 作用范围?
一次请求,包含多次转发
@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("aServlet处理功能上....");
// 转发到BServlet
/* // 1.获得转发器对象 path = @WebServlet("/bServlet")
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/bServlet");
// 2.实现转发功能
requestDispatcher.forward(request, response);*/
// 存一个数据
request.setAttribute("hanbao", "香辣鸡腿堡");
// 链式编程横
request.getRequestDispatcher("bServlet").forward(request, response);
}
}
@WebServlet("/bServlet")
public class BServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("bServlet处理功能下....");
// 获取数据
String hanbao = (String) request.getAttribute("hanbao");
System.out.println("hanbao:" + hanbao);
}
}
2.3 获取ServletContext对象【了解】
- 中文直译:应用上下文对象,表示一个web项目
通过reuqest,可以获得ServletContext对象
public ServletContext getServletContext();
四 案例:用户登录
需求
实现用户的登录功能
登录成功跳转到SuccessServlet展示:登录成功!xxx,欢迎您
登录失败跳转到FailServlet展示:登录失败,用户名或密码错误
需求分析
代码实现
① 创建web项目
② 导入BeanUtils工具类
③ 编写index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h4>用户登录</h4>
<form action="/day09_login/loginServlet" method="post">
用户:<input type="text" name="username"> <br>
密码:<input type="password" name="password"> <br>
<input type="submit" value="登录">
</form>
</body>
</html>
④ User实体类
// 用户对象
public class User {
private String username;// 用户名
private String password;// 密码
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{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
⑤ LoginServlet
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.设置request解码方式
request.setCharacterEncoding("UTF-8");
// 2.获取浏览器请求参数,map集合
Map<String, String[]> parameterMap = request.getParameterMap();
// 3.使用BeanUtils工具类,封装user对象中
User user = new User();
try {
BeanUtils.populate(user, parameterMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 4.判断(课下作业,你们写一个file文件,存储真实用户名和密码 判断)
if("jack".equals(user.getUsername())&&"123".equals(user.getPassword())){
// 正确
request.setAttribute("user", user);
request.getRequestDispatcher("/successServlet").forward(request,response);
}else{
// 错误
request.getRequestDispatcher("/failServlet").forward(request,response);
}
}
}
⑥ SuccessServlet
@WebServlet("/successServlet")
public class SuccessServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.从request域获取user对象
User user = (User) request.getAttribute("user");
// 2.友情提示
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(user.getUsername() + ",登录成功");
}
}
⑦ FailServlet
@WebServlet("/failServlet")
public class FailServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.友情提示
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("用户名或密码错误..");
}
}
附录
Servlet模板设置
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
@javax.servlet.annotation.WebServlet("/${Class_Name}")
public class ${Class_Name} extends javax.servlet.http.HttpServlet {
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
this.doPost(request,response);
}
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
}
}
网友评论