response.setDateHeader("Expires",0);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
3、实现代码如下:
(1)MyServlet 程序类如下:
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
System.out.println("这里是 MyServlet!");
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
(2)web.xml 文件配置如下:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>
cn.itcast.chapter04.servlet.MyServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
4、该功能的实现步骤如下:
1)创建一个 DownManagerServlet 类,使其继承 HttpServlet 类并重写该类的 doGet()方
法。
2)在 doGet()方法中,使用 HttpServletRequest 对象的 getHeader("referer")方法得到字段
referer 的值,并判断是否可以进行资源下载。
3)如果不能下载,则进入下载页(需要在 Eclipse 的 WebContent 目录下创建一个
download.html)。
(1)DownManagerServlet 类的实现代码如下:
public class DownManagerServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
// 获取 referer 头的值
String referer = request.getHeader("referer");
// 获取访问地址
String sitePart = "http://" + request.getServerName();
if (referer != null && referer.startsWith(sitePart)) {