利用referer來獲得這個鏈接是從哪點進來的。瀏覽器
等於空是有可能,用戶直接把這個地址打到瀏覽器的地址欄的,這樣不行,而且,若是不是以http://localhot開頭的也是不行,那咱們就跳到首頁jsp
1 public void doGet(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 4 String referer = request.getHeader("referer"); 5 6 if(referer == null || !referer.startsWith("http://localhost")){ 7 //這個地址是爲瀏覽器用的 8 response.sendRedirect("/day03/index.jsp"); 9 return; 10 } 11 12 String data = "asdfasdfasd"; 13 14 response.getWriter().write(data); 15 16 }