请求重定向
在某些情况下,针对客户端的请求,一个Servlet类可能无法完成全部工作。这时,可以使用请求重定向来完成。所谓请求重定向,指的是Web服务器接收到客户端的请求后,可能由于某些条件限制,不能访问当前请求URL所指向的Web资源,而是指定了一个新的资源路径,让客户端重新发送请求。
为了实现请求重定向,在HttpServletResponse接口中,定义了一个sendRedirect()方法,该方法用于生成302响应码和Location响应头,从而通知客户端重新访问Location响应头中指定的URL,sendRedirect()方法的完整语法如下所示:
sendRedirect()方法的工作原理
入门小案例——登陆案例
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录页面</h1>
<font color="red">用户或密码错误,请重新输入</font><br />
<form action="http://localhost:8080/Review4/login" method="get">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="登陆" />
</td>
</tr>
</table>
</form>
</body>
</html>
welcome.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User,Welcome!</title>
</head>
<body>
<h1>登陆成功!</h1>
<h2>いらっしゃいませ</h2><br />
<h2>欢迎光临</h1>
</body>
</html>
LoginServlet.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// 1.获取登陆的用户名和密码
String nameString = request.getParameter("username");
String passwordString = request.getParameter("pwd");
// 2.判断用户名和密码是否正确
if (nameString.equals("admin") && passwordString.equals("123")) {
response.sendRedirect("/Review4/welcome.html");
} else {
response.sendRedirect("/Review4/login.html");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
全局配置:
运行部分效果:
原理图如下:
HttpServletRequest
在Servlet API中,定义了一个HttpServletRequest接口,它继承自ServletRequest接口,专门用来封装HTTP请求消息。由于HTTP请求消息分为请求行、请求消息头和请求消息体三部分,因此,在HttpServletRequest接口中定义了获取请求行、请求头和请求消息体的相关方法
获取请求行的相关方法(部分)
当访问Servlet时,会在请求消息的请求行中,包含请求方法、请求资源名、请求路径等信息,为了获取这些信息,在HttpServletRequest接口中,定义了一系列用于获取请求行的方法,如表所示。
入门小案例
RequestLineServlet.java
package Request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RequestLineServlet
*/
public class RequestLineServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// 1.获取请求提交的方式
String methodString = request.getMethod();
System.out.println("请求方式:" + methodString);
// 2.获取请求的协议
String protocolString = request.getProtocol();
System.out.println("请求的协议:" + protocolString);
// 3.获取项目路径
String contextPathString = request.getContextPath();
System.out.println("项目路径:" + contextPathString);
// 4.获取servlet路径
String servletPathString = request.getServletPath();
System.out.println("servlet路径:" + servletPathString);
// 5.获取请求的路径
String pathURI = request.getRequestURI();
StringBuffer pathURL = request.getRequestURL();
System.out.println("URI对应的值:" + pathURI + " URL路径对应的值:" + pathURL);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Review4</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>PrintServlet</servlet-name>
<servlet-class>PrintServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PrintServlet</servlet-name>
<url-pattern>/ps</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ChineseServlet</display-name>
<servlet-name>ChineseServlet</servlet-name>
<servlet-class>ChineseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChineseServlet</servlet-name>
<url-pattern>/cs</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ChineseServlet2</display-name>
<servlet-name>ChineseServlet2</servlet-name>
<servlet-class>ChineseServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChineseServlet2</servlet-name>
<url-pattern>/cs2</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>refreshServlet</display-name>
<servlet-name>refreshServlet</servlet-name>
<servlet-class>refreshServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>refreshServlet</servlet-name>
<url-pattern>/rs</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RequestLineServlet</display-name>
<servlet-name>RequestLineServlet</servlet-name>
<servlet-class>Request.RequestLineServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestLineServlet</servlet-name>
<url-pattern>/line</url-pattern>
</servlet-mapping>
</web-app>
全局配置:
运行结果:
URI对应的值:/项目名称/servlet路径
URL对应的值是完整的:如上图,http://localhost:8080/Review4/line
获取请求头的相关方法
当请求Servlet时,需要通过请求头向服务器传递附加信息,例如,客户端可以接收的数据类型,压缩方式,语言等等。为此,在HttpServletRequest接口中,定义了一系列用于获取HTTP请求头字段的方法,如表所示。
入门小案例
RequestHeaderServlet.java
package Request;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RequestHeaderServlet
*/
public class RequestHeaderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// 1.获取所有的消息头名称
Enumeration<String> enumeration = request.getHeaderNames();
// 2.遍历
while (enumeration.hasMoreElements()) {
// 3.获取消息头
String nameString = enumeration.nextElement();
// 4.获取消息头对应的值
String valueString = request.getHeader(nameString);
response.getWriter().print("消息头:" + nameString + " " + valueString + "<br />");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Review4</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>PrintServlet</servlet-name>
<servlet-class>PrintServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PrintServlet</servlet-name>
<url-pattern>/ps</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ChineseServlet</display-name>
<servlet-name>ChineseServlet</servlet-name>
<servlet-class>ChineseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChineseServlet</servlet-name>
<url-pattern>/cs</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ChineseServlet2</display-name>
<servlet-name>ChineseServlet2</servlet-name>
<servlet-class>ChineseServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChineseServlet2</servlet-name>
<url-pattern>/cs2</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>refreshServlet</display-name>
<servlet-name>refreshServlet</servlet-name>
<servlet-class>refreshServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>refreshServlet</servlet-name>
<url-pattern>/rs</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RequestLineServlet</display-name>
<servlet-name>RequestLineServlet</servlet-name>
<servlet-class>Request.RequestLineServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestLineServlet</servlet-name>
<url-pattern>/line</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RequestHeaderServlet</display-name>
<servlet-name>RequestHeaderServlet</servlet-name>
<servlet-class>Request.RequestHeaderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestHeaderServlet</servlet-name>
<url-pattern>/header</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DownLoadServlet</display-name>
<servlet-name>DownLoadServlet</servlet-name>
<servlet-class>Request.DownLoadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownLoadServlet</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>
</web-app>
全局配置:
运行结果:
乱码
在servlet中加入代码
response.setContentType("text/html;charset=utf-8");
即可解决
运行结果:
防盗链的小案例
需求:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Review4</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>PrintServlet</servlet-name>
<servlet-class>PrintServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PrintServlet</servlet-name>
<url-pattern>/ps</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ChineseServlet</display-name>
<servlet-name>ChineseServlet</servlet-name>
<servlet-class>ChineseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChineseServlet</servlet-name>
<url-pattern>/cs</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ChineseServlet2</display-name>
<servlet-name>ChineseServlet2</servlet-name>
<servlet-class>ChineseServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChineseServlet2</servlet-name>
<url-pattern>/cs2</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>refreshServlet</display-name>
<servlet-name>refreshServlet</servlet-name>
<servlet-class>refreshServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>refreshServlet</servlet-name>
<url-pattern>/rs</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RequestLineServlet</display-name>
<servlet-name>RequestLineServlet</servlet-name>
<servlet-class>Request.RequestLineServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestLineServlet</servlet-name>
<url-pattern>/line</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RequestHeaderServlet</display-name>
<servlet-name>RequestHeaderServlet</servlet-name>
<servlet-class>Request.RequestHeaderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestHeaderServlet</servlet-name>
<url-pattern>/header</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DownLoadServlet</display-name>
<servlet-name>DownLoadServlet</servlet-name>
<servlet-class>Request.DownLoadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownLoadServlet</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>
</web-app>
DownLoadServlet.java
package Request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DownLoadServlet
*/
public class DownLoadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// 1.获取Referer请求头:Referer=http://localhost:8080/Review4/download
String refererString = request.getHeader("Referer");
// 2.获取服务器名称
String serverNameString = request.getServerName();
// 3.判断是否是从本网站发起的下载请求
if (refererString != null && refererString.contains(serverNameString)) {
response.getWriter().print("download......");
} else {
response.sendRedirect("/Review4/download.html");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
download.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/Review4/download">从这个网站的页面打开,才能下载!</a>
</body>
</html>
全局配置:
运行结果如下:
由于这里没有另一个服务器,所以就无法演示盗链的情况,但是基本原理就是上面那样的