request中的各种path
从Request对象中可以获取各种路径信息,以下例子:
假设请求的页面是index.do,项目是WebDemo,则在index.do中获取有关request对象的各种路径信息如下
Java代码 收藏代码
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String remoteAddress=request.getRemoteAddr();
String servletPath=request.getServletPath();
String realPath=request.getRealPath("/");
String remoteUser=request.getRemoteUser();
String requestURI=request.getRequestURI();
System.out.println("path:"+path);
System.out.println("basePath:"+basePath);
System.out.println("remoteAddr:"+remoteAddress);
System.out.println("servletPath:"+servletPath);
System.out.println("realPath:"+realPath);
System.out.println("remoteUser:"+remoteUser);
System.out.println("requestURI:"+requestURI);
结果:
Java代码 收藏代码
path:/WebDemo
basePath:http://localhost:8683/WebDemo/
remoteAddr:127.0.0.1
servletPath:/index.do
realPath:D:\apache-tomcat-6.0.13\webapps\WebDemo\
remoteUser:null
requestURI:/WebDemo/index.do
从Request对象中可以获取各种路径信息,以下例子:
假设请求的页面是index.do,项目是WebDemo,则在index.do中获取有关request对象的各种路径信息如下
Java代码 收藏代码
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String remoteAddress=request.getRemoteAddr();
String servletPath=request.getServletPath();
String realPath=request.getRealPath("/");
String remoteUser=request.getRemoteUser();
String requestURI=request.getRequestURI();
System.out.println("path:"+path);
System.out.println("basePath:"+basePath);
System.out.println("remoteAddr:"+remoteAddress);
System.out.println("servletPath:"+servletPath);
System.out.println("realPath:"+realPath);
System.out.println("remoteUser:"+remoteUser);
System.out.println("requestURI:"+requestURI);
结果:
Java代码 收藏代码
path:/WebDemo
basePath:http://localhost:8683/WebDemo/
remoteAddr:127.0.0.1
servletPath:/index.do
realPath:D:\apache-tomcat-6.0.13\webapps\WebDemo\
remoteUser:null
requestURI:/WebDemo/index.do