import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//防盗链[下载文件,必须从指定网站进入,否则不允许下载文件]
public class Rdome4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//取得浏览器来自于何方
String referer = request.getHeader("referer");
//取得浏览器访问的URL
// String url = request.getRequestURL().toString();
//判段
// if(referer != null && url.equals("http://localhost:8080/day04/index.html"))
if(referer!=null && referer.equals("http://localhost:8080/day04/index.html"))
{
//转发到download.html页面
this.getServletContext().getRequestDispatcher("/download.html").forward(request, response);
}else
{
//转发到ad.html页面
this.getServletContext().getRequestDispatcher("/ad.html").forward(request, response);
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<a href="/day04/Rdome4">进入下载页面</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>download.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
这是下载页面<br>
</body>
</html>
原文地址:http://blog.51cto.com/357712148/2104805