Java Web程序设计——请求和响应(二)
思维导图:
实现重定向
实现重定向的相关方法
- 302和Location配合使用,实现重定向
比如:response.setStatus(302); response.setHeader("Location", "/chapter/login.html");
sendRediect("/chapter/login.html");
例:
重定向通过登录案例完成,在登录页面输入用户名和密码,在LoginServlet里面,判断用户名和密码是否正确。如果输入错误,或重新定向到登录页面,如果正确,跳转到欢迎页面。获取登录页面提交的用户名和密码:request.getParmeter()
方法。
package chapter.response;
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#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.获取登录的用户名和密码
String name = request.getParameter("username");
String password = request.getParameter("psw");
// 2. 判断用户名和密码是否正确
if (name.equals("admin") && password.equals("123")) {// 表明输入的数据是正确的
response.sendRedirect("/chapter/welcome.html");
} else {// 登录失败
response.sendRedirect("/chapter/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);
}
}
welcom.html
login.html
输入正确:
输入错误:
重新跳转至原页面,重新输入
重定向的特点
- 浏览器地址栏发生变化
- 发送了两次请求,都是浏览器发送的请求
重定向的路径
- 由于是浏览器发送得到的请求,所以路径必须要带项目名称,比如:
/chapter/welcome.html
HttpServletRequest对象
- HttpServletRequest它是一个接口,父接口ServletRequest,在开发中,常用的是带协议的请求对象
获取请求行信息的相关方法
- 获取请求提交的方式:getMethod();
- 获取请求的协议:getProtocol();
- 获取项目名称:getContextPath();
- 获取servlet路径:getServletPath();
- 获取请求路径:getRequestURI(),getRequestURL();
ps:此处只是例举了一些常用的,还有很多。
例:
package chapter.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 {
// 获取请求提交的方式:getMethod();
String method = request.getMethod();
System.out.println("请求方式:"+method);
// 获取请求的协议:getProtocol();
String proto = request.getProtocol();
System.out.println("请求协议:"+proto);
// 获取项目名称:getContextPath();
String contextPath = request.getContextPath();
System.out.println("项目路径:"+contextPath);
// 获取servlet路径:getServletPath();
String servletPath = request.getServletPath();
System.out.println("servlet路径:"+servletPath);
// 获取请求路径:getRequestURI(),getRequestURL();
String pathURI = request.getRequestURI();
String pathURL = request.getRequestURL().toString();
System.out.println("URI对应的值:"+pathURI);
System.out.println("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);
}
}
控制台可看到:
注:URI对应的值:/项目名称/servlet路径
URL对应的值:http://localhost:8080/uri
获取请求消息头的相关方法
- 根据消息头,获取值:String value = getHeader(消息头的名称);
- 获取所有的消息头:Enumeration er = getHeaderNames();
例:
package chapter.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 {
// 处理响应中文的乱码问题
response.setContentType("text/html;charset=utf-8");
// 1. 获取所有的消息头名称
Enumeration<String> er = request.getHeaderNames();
// 2.遍历
while (er.hasMoreElements()) {
// 3.获取消息头
String name = er.nextElement();
// 4.获取消息头对应的值
String value = request.getHeader(name);
response.getWriter().print("消息头:" + name + " " + value + "<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);
}
}
打开浏览器可看到:
根据Referer请求消息头,做一个防盗链的案例:
package chapter.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 {
// 1.获取Referer请求头:referer=http://localhost:8080/chapter/download
String referer = request.getHeader("Referer");
// 2.获取服务器名称
String serverName = request.getServerName();
// 3.判断是否是从本网站发起的下载请求
if (referer != null && referer.contains(serverName)) {
// refer请求头对于的值包含了服务器名称,说明是从本网站发起的请求,允许下载
response.getWriter().print("download-------------");
} else {
// 说明不是从本站发起的请求,不允许下载,可以定向到本网站的下载页面,从而让用户从本网站的页面开始下载
response.sendRedirect("/chapter/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
打开浏览器,可看到: