本文继续介绍request请求对象功能
一、获取请求头数据
方法:
String getHeader(String name):通过请求头的名称获取请求头的值
getHeaderName():获取所有的请求头名称(封装成了字符串格式)
演示如下:
package cn.itcast.web.servlet.request;
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> headNames = request.getHeaderNames();
// 遍历
while(headNames.hasMoreElements()){
String name = headNames.nextElement();
String value = request.getHeader(name);
System.out.println(name+"---"+value);
}
}
}
测试结果:
访问浏览器类型
?
?
?如果想要获取referer值:
package cn.itcast.web.servlet.request;
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);
}
}
?
此处访问不到,需要建一个超链接(新建login.html)
其中<a href>? </a>?是一个超链接标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/demo3" method="GET">
<input name = "username">
<input types="submit" value="提交">
</form>
<hr>
<a href="/day03/RequestDemo4">demo4...</a>
</body>
</html>
测试结果如下:
--------获取到referer值
接下来试一试防盗链的操作:
@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);
//防盗链
if(referer != null){
if(referer.contains("/day03")){
// 正常
System.out.println("播放电影");
}else{
// 盗链
System.out.println("想看电影吗?来优酷");
}
}
}
}
测试结果:
接下来测试不成功的,要新建一个module,让两个项目一起跑起来,现在在新建的module中创建一个“index.html”超链接:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="http://localhost/day03/RequestDemo4">高清电影</a>
</body>
</html>
?在edit 添加一个tomcat8,两个tomcat的startup路径不同
以下是tomcat8的成功访问结果:
tomcat8.5.37访问:
如果想直接在页面上获取文字:
@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);
//防盗链
if(referer != null){
if(referer.contains("/day03")){
// 正常
response.setContentType("text/html;charset = utf-8");
response.getWriter().write("播放电影");
//System.out.println("播放电影");
}else{
// 盗链
response.setContentType("text/html;charset = utf-8");
response.getWriter().write("想看电影吗?来优酷");
//System.out.println("想看电影吗?来优酷");
}
}
}
}
测试结果如下:
结束!
二、获取请求体数据
请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数
获取请求体步骤:
获取流对象
BufferedReader? getReader():获取字符输入流,只能操作字符数据
ServletInputStream getInputStream():获取字节输入流,可以操作所有类型的数据
再从流对象中拿数据
新建一个RequestDemo5,在day03项目中新建register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="/day03/RequestDemo5" method="post">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
新建servlet:
@WebServlet( "/RequestDemo5")
public class RequestDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求消息体
// 获取字符流
BufferedReader br = request.getReader();
// 读取数据
String line = null;
while((line = br.readLine())!=null){
System.out.println(line);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
测试结果
?
三、其他功能
获取请求参数方式功能
1.String getParameter(String name) :根据参数名称获取参数值
2.String[] getParameterValues(?String name)
3.Enumeration getParameterNames():获取所有请求的参数名称
4.Map<String,String[]> getParameterMap():获取所有参数的map集合
请求转发
共享数据
获取ServletContext
首先要新建一个register2.html,新建servlet
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="/day03/RequestDemo6" method="post">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
新建servlet
@WebServlet( "/RequestDemo6")
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// post 获取请求参数
// 根据参数名称获取参数值
String username = request.getParameter("username");
System.out.println("post");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// get 获取请求参数
// 根据参数名称获取参数值
String username = request.getParameter("username");
System.out.println("get");
System.out.println(username);
}
}
测试结果:
如果想要用get方法,将register2.html中method?改为get
测试结果:
注意:上述例子表明 以上方法不论是get还是post请求方式都可以用
<body>
<form action="/day03/RequestDemo6" 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>
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// post 获取请求参数
// 根据参数名称获取参数值
String username = request.getParameter("username");
/*System.out.println("post");
System.out.println(username);*/
// 根据参数名称获取参数值的数组
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 value = request.getParameter(name);
System.out.println(value);
System.out.println("------");
}
注意:此方法只获取一个参数(game)
// 获取所有的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("--------");
}
测试结果:
特别指出:用post方法获取参数值,网页输入中文以后乱码(get方法不存在这个问题)
解决方法:
获取参数前? 需要设置流的编码:request.setCharacterEncoding
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// post 获取请求参数
// 根据参数名称获取参数值
String username = request.getParameter("username");
}
}
测试结果:
四、请求转发功能
这是一种在服务器内部的资源跳转方式(访问的时候如果访问服务器A,也能在A中访问B)
步骤如下:
通过request对象来获取请求转发器对象:RequestDispatcher? getRequestDispatcher(String path)
使用RequestDispatcher对象来进行转发:
forword(ServletRequest request , ServletResponse response)
@WebServlet("/RequestDemo8")
public class RequestDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo8被访问");
// 转发到demo9资源
/*RequestDispatcher requestDispatcher = request.getRequestDispatcher("/RequestDemo9");
requestDispatcher.forward(request,response);*/
//或者
request.getRequestDispatcher("/RequestDemo9").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
@WebServlet( "/RequestDemo9")
public class RequestDemo9 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo9被访问");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
测试结果:
在网页中输入: localhost/day03/RequestDemo8
关于转发的特点:(重要)
1.浏览器的地址栏路径不发生变化
2.只能转发到当前服务器内部的资源中
3.转发是一次请求
五、共享数据功能
域对象:一个有作用范围的对象,可以在范围内共享数据
request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据
共享数据的方法:setAttribute(String name,Object obj):存储数据
setAttribute(String name,Object obj):存储数据
getAttribute(String name):通过键获取值
removeAttribute(String name):通过键移除键值对
?以下是新建了两个servlet:
public class RequestDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo8被访问");
// 转发到demo9资源
/*RequestDispatcher requestDispatcher = request.getRequestDispatcher("/RequestDemo9");
requestDispatcher.forward(request,response);*/
//或者
// 存储数据到request域中
request.setAttribute("mm","hello");
request.getRequestDispatcher("/RequestDemo9").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
---------------------------------------------
@WebServlet( "/RequestDemo9")
public class RequestDemo9 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// RequestDemo8转发数据以后,在RequestDemo9中获取数据
Object mm = request.getAttribute("mm");
System.out.println(mm);
System.out.println("demo9被访问");
}
测试结果:
获取ServletContext方法
ServletContext? getServletContext()
@WebServlet("/RequestDemo10")
public class RequestDemo10 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext= request.getServletContext();
System.out.println(servletContext);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
测试结果:
以上是Request的一些方法,get!
|