1 HttpServletRequest req=(HttpServletRequest)request; 2 HttpServletResponse res=(HttpServletResponse)response; 3 4 String referer=req.getHeader("referer"); 5 //如果referer非空 6 if(referer!=null) //非空说明是内部forward 访问其他页面的 7 {//放行资源 8 chain.doFilter(request, response); 9 } 10 //如果referer空 是直接 回车键访问 servlet的 11 else 12 { 13 System.out.println("无法直接访问"); 14 response.setContentType("text/html;charset=utf-8"); 15 //警告 无法直接访问 16 response.getWriter().write("" + 17 "<script>" + 18 "window.alert(‘你是非法闯入者,滚出去‘);" + 19 "window.location.href=‘http://127.0.0.1:8080/bbs/welcome.jsp‘" + 20 "</script>"); 21 22 }
原文:http://www.cnblogs.com/friends-wf/p/3709501.html