Web 前端学习 之Servlet(三) HttpServletRequest 类
文章目录
HttpServletRequest 类
作用
每次只要有请求进入Tomcat 服务器,Tomcat 服务器就会把请求过来的HTTP 协议信息解析好封装到Request 对象中。
然后传递到service 方法(doGet 和doPost)中给我们使用。我们可以通过HttpServletRequest 对象,获取到所有请求的信息。
常用方法
实例
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// i.getRequestURI() 获取请求的资源路径
System.out.println("URI => " + req.getRequestURI());
// ii.getRequestURL() 获取请求的统一资源定位符(绝对路径)
System.out.println("URL => " + req.getRequestURL());
// iii.getRemoteHost() 获取客户端的ip 地址
/**
* 在IDEA 中,使用localhost 访问时,得到的客户端ip 地址是===>>> 127.0.0.1<br/>
* 在IDEA 中,使用127.0.0.1 访问时,得到的客户端ip 地址是===>>> 127.0.0.1<br/>
* 在IDEA 中,使用真实ip 访问时,得到的客户端ip 地址是===>>> 真实的客户端ip 地址<br/>
*/
System.out.println("客户端ip 地址=> " + req.getRemoteHost());
// iv.getHeader() 获取请求头
System.out.println("请求头User-Agent ==>> " + req.getHeader("User-Agent"));
// vii.getMethod() 获取请求的方式GET 或POST
System.out.println("请求的方式==>> " + req.getMethod());
}
获取请求参数
先写个有表单的html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/07_servlet/helloServlet1" 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>
</html>
这里使用request.getParameter可以获取到表单中的参数,而获取的的方法通过表单中标签的name属性
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求参数
String username = request.getParameter("username");
String password = request.getParameter("password");
String[] hobby = request.getParameterValues("hobby");
System.out.println("用户名:" + username);
System.out.println("密码:" + password);
System.out.println("兴趣爱好:" + Arrays.asList(hobby));
}
post方法中文乱码解决
可以看到get方法是没有什么问题的,下面就说说post方法, 在post方法中我们需要事先设置utf-8,不然中文会出错
需要在最前面加一句话,如下
request.setCharacterEncoding("UTF-8");
这里有个注意点,设置UTF-8的语句要放在请求参数的前面,如下 输出的结果是可以解析中文的
req.setCharacterEncoding("UTF-8");
// 获取请求参数
String username = req.getParameter("username");
如果像下面这样,那么还是会失败
String password= req.getParameter("password");
req.setCharacterEncoding("UTF-8");
System.out.println("-------------doPost------------");
// 获取请求参数
String username = req.getParameter("username");
请求的转发
什么是请求的转发?
- 请求转发是指,服务器收到请求后,从一次资源跳转到另一个资源的操作叫请求转发。
下图十分生动的给出了解释
我们可以做一个简单的测试,代码如下
public class Servlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求的参数
String username = request.getParameter("username");
System.out.println("Servlet1中获取的参数 username:" + username);
// 给材料盖章,传递到Servlet2
request.setAttribute("key1", "Servlet1的章");
System.out.println("Servlet1完成盖章");
// Servlet2的路径
/**
* 请求转发必须要以斜杠打头,/ 斜杠表示地址为:http://ip:port/工程名/ , 映射到IDEA 代码的web 目录
*/
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/servlet2");
// 走向Sevlet2(柜台2)
requestDispatcher.forward(request, response);
}
}
public class Servlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
System.out.println("Servlet2中获取的参数 username:" + username);
Object key1 = request.getAttribute("key1");
System.out.println("Servlet1盖章的结果:" + key1);
System.out.println("下面是Servlet2自己处理的过程");
}
}
我们的目标就是实现一个Servlet1到servlet2的请求转发
下面就启动Tomcat服务来看看结果如何
这里要注意如果通过url传递参数
格式是路径?参数名=参数值
如http://localhost:8080/07_servlet/servlet1?username=tty
测试成功
WEB-INF目录下的访问
之前的请求转发并没有做到网页的互访,下面就实现如何访问到WEB-INF下的form.html
比如我们将之前的form网页放到了 WEB-INF的目录下,我们该如何访问呢
之前都知道.放在web文件夹下面的网页是可以通过项目路径+文件路径访问的
WEB-INF也在其下面,那么我们试试WEB-INF的可不可以
结论是错误的
所以我们只能通过资源来访问,现在修改一下之前的Servlet1
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求的参数
String username = request.getParameter("username");
System.out.println("Servlet1中获取的参数 username:" + username);
// 给材料盖章,传递到Servlet2
request.setAttribute("key1", "Servlet1的章");
System.out.println("Servlet1完成盖章");
// Servlet2的路径
/**
* 请求转发必须要以斜杠打头,/ 斜杠表示地址为:http://ip:port/工程名/ , 映射到IDEA 代码的web 目录
*/
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB_INF/form.html");
// 走向Sevlet2(柜台2)
requestDispatcher.forward(request, response);
}
这里就修改了一句话
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB_INF/form.html");
通过http://localhost:8080/07_servlet/servlet1
我们访问到了WEB-INF
中的内容
base 标签的作用
之前是不同资源的请求转发,我们也会遇到想要访问不同的页面
假如在servlet中建立a/b/c.html
我们接下来想要实现的是c.html与项目首页 index.html的跳转
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是c</title>
</head>
<body>
<h2>我是a/b/下的c.html页面</h2>
<a href="../../index.html">点击我可以跳转到首页,我的href是../../index.html</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h2>我是07_servlet项目的首页</h2>
<a href="a/b/c.html">点击我可以跳转到a/b/c.html,我的href是a/b/c.html</a>
</body>
</html>
如上代码是可以实现简单的跳转的,那么如何用请求的方式跳转呢
package com.tty;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author: Tong tianyu
* @date: 2020/8/15 - 11:52
*/
public class Forwardc extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是Forwardc");
request.getRequestDispatcher("a/b/c.html").forward(request, response);
}
}
新建一个Servlet来实现,但是这么做会产生一点问题
也就是说返回首页的时候回失败
因为在html的a标签中写的是../../index.html
而使用请求访问的时候路径变成了http://localhost:8080/07_servlet/forwardc
它的../../index.html
是http://localhost:8080/index.html
,这是不存在的,所以就会产生错误
具体的解析也可以看下面的图
为了解决这个问题,可以使用base标签
它设置了当前页面中所有相对路径的参照标准
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是c</title>
<base href="http://localhost:8080/07_servlet/a/b/">
</head>
<body>
<h2>我是a/b/下的c.html页面</h2>
<a href="../../index.html">点击我可以跳转到首页,我的href是../../index.html</a>
</body>
</html>
修改之后就可以成功跳转访问了