本文主要是介绍【javaweb:servlet】防盗链的demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package indi.referer;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*** 一个简单的判断是否为盗链的demonstrate* @author Administrator**/
public class RefererServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//用来获取来访者地址String referer=request.getHeader("referer");//校验访问网站是否是以指定地址(比如http://localhost)开头if(referer!=null&&referer.startsWith("http://localhost")) {response.setContentType("text/html;charset=UTF-8");response.getWriter().write("中国已经获取金牌!");}else {response.getWriter().write("你是盗链者!");}}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
这篇关于【javaweb:servlet】防盗链的demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!