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;
import sun.awt.RepaintArea;
public class RequestDemo7 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String referer = request.getHeader("referer");
//防盗链
//判断请求是从哪里过来的,如果不符合要求就跳转到我网站首页上
if(referer==null || !referer.startsWith("http://localhost")){
response.sendRedirect("//index.jsp");
return;//这里一点要return 不然后面的代码还是会执行,我们希望是不执行
}
String data = "你要看的资源";
response.getWriter().write(data);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}