欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HttpServletRequest request获取请求url等信息

程序员文章站 2024-02-01 23:28:40
...

 

ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		HttpServletRequest request = null;
		if (null != requestAttributes) {
			request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			String url = request.getScheme()+"://"+ request.getServerName()+request.getRequestURI()+"?"+request.getQueryString();
			System.out.println("7获取全路径(协议类型://域名/项目名/命名空间/action名称?其他参数)url="+url);
			String url2=request.getScheme()+"://"+ request.getServerName();//+request.getRequestURI();
			System.out.println("协议名://域名="+url2);
			System.out.println("获取项目名="+request.getContextPath());
			System.out.println("获取参数="+request.getQueryString());
			System.out.println("获取全路径="+request.getRequestURL());
			System.out.println("获取web项目的路径="+request.getSession().getServletContext().getRealPath("/"));//获取web项目的路径
			System.out.println("获取类的当前目录="+this.getClass().getResource("/").getPath());//获取类的当前目录
		}else{
			System.out.println("--------null == requestAttributes-----------");
		}

资料地址:https://blog.csdn.net/lili518/article/details/77922265

https://blog.csdn.net/GuozgIT/article/details/79433451

https://www.cnblogs.com/qubo520/p/10970845.html