HttpServletRequest的getServletPath、getServletURI、getServletURL等区别 &&如何防止用户通过直接输入URL访问网页

现在项目做到加权限了,如何防止用户通过直接输入URL访问网页,去查资料,了解到在filter里用HttpServletRequest里的方法得到该请求的父URL-fatherURL,判断该fatherURL是否为空,如果为空就表示该请求是通过直接输入URL访问的。

代码:

[html]  view plain copy print ?
  1. public void doFilter(ServletRequest request, ServletResponse response,   
  2.           FilterChain chain) throws IOException, ServletException {  
  3.      //首先判断如果是在URL输入页面访问,直接非法  
  4.      HttpServletRequest req=(HttpServletRequest)request;  
  5.      HttpServletResponse resp=(HttpServletResponse)response;  
  6.      String fatherUrl = req.getHeader("REFERER");//取得上一级页面的RUL  
  7.     
  8.      if("".equals(fatherUrl ) || null == fatherUrl ) {  
  9.              String url = req.getServletPath();  
  10.              //第一次转发  
  11.              if("//pages/login.jsp".equals(url )) {  
  12.                      chain.doFilter(request, response);  
  13.              } else {     //设置输出页面编码  
  14.                      resp.setContentType("text/html;charset=gb2312");   
  15.                      resp.getWriter().write("错误页面 ,无此页");  
  16.                      return ;  
  17.               }  
  18.       } else{     //如果不是直接URL输入访问  
  19.           if(null==req.getSession().getAttribute("inWebUser")) {  
  20.                  resp.getWriter().write("非法访问");  
  21.                  return;  
  22.           } else {  
  23.                  chain.doFilter(request, response);  
  24.           }  
  25.      }  
  26. }  


知道这个方法后,感觉HttpServletRequest太好用了,就去了JavaEE API文档,看到HttpServletRequest里有几个得到URL的方法,既然有好几个,说明它们之间肯定有不同之处,针对各种实际情况运用。下面就说说它们的区别:

getServletPath:得到相对项目的url,不带项目名称        如://sys/menu/gomain.htm

[html]  view plain copy print ?
  1. String path = ((HttpServletRequest)req).getServletPath();  

getRequestURI:得到相对服务器的url,和上面有不同之处,getRequestURI带有项目root,以项目名称为开头,如:/cms//sys/menu/gomain.htm

[html]  view plain copy print ?
  1. String requestURI = ((HttpServletRequest)req).getServletURI();  


getContextPath:得到部署的项目名(Context-root),不是URL,仅仅项目名字  如:/cms

[html]  view plain copy print ?
  1. String ContextPath = ((HttpServletRequest)req).getContextPath();  


getRequestURL:得到绝对地址url如:http://localhost:8080/cms//sys/menu/gomain.htm

[html]  view plain copy print ?
  1. StringBuffer RequestURL = ((HttpServletRequest)req).getRequestURL();  


getRealPath:得到应用程序部署在服务器上的绝对路径。如:D:\apache-tomcat-5.5.23\webapps\cms\

[html]  view plain copy print ?
  1. String realPath = this.fc.getServletContext().getRealPath("/");  

               System.out.println("ServletPath:"+path+"\n"+"ContextPath:"+ContextPath+"\n"+"requestURI:"+requestURI+"\n"+"RequestURL:"+RequestURL+"\n"+"realPath:"+realPath);

结果如下:

ServletPath:/sys/menu/gomain.htm
ContextPath:/cms
requestURI:/cms//sys/menu/gomain.htm
RequestURL:http://localhost:8080/cms//sys/menu/gomain.htm
realPath:D:\apache-tomcat-5.5.23\webapps\cms\ 


速记:

  • getServletPath与getRequestURI相似,区别在于有无项目名称
  • getRequestURL是带有HTTP协议的URL,
  • getContextPath得到项目的名称
  • getRealPath得到在服务器上的绝对路径
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值