Comparison of various methods for jsp Request to obtain url information

  • 2021-10-27 08:27:37
  • OfStack

Various path information can be obtained from Request objects, such as the following example:

Assuming the requested page is index. jsp and the project is WebDemo, get various path information about the request object in index. jsp as follows


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(); 
out.println("path:"+path+"<br>"); 
out.println("basePath:"+basePath+"<br>"); 
out.println("remoteAddr:"+remoteAddress+"<br>"); 
out.println("servletPath:"+servletPath+"<br>"); 
out.println("realPath:"+realPath+"<br>"); 
out.println("remoteUser:"+remoteUser+"<br>"); 
out.println("requestURI:"+requestURI+"<br>"); 

Results:


path:/WebDemo 
basePath:http://localhost:8683/WebDemo/ 
remoteAddr:127.0.0.1 
servletPath:/index.jsp 
realPath:D:\apache-tomcat-6.0.13\webapps\WebDemo\ 
remoteUser:null 
requestURI:/WebDemo/index.jsp 

It is not difficult to see the meaning of each corresponding method of request from the above

Get a summary of various paths from request:


request.getRealPath("url");// Mapping virtual directories to actual directories  
request.getRealPath("./");// Directory where the page is located  
request.getRealPath("../");// On the directory where the web page is located 1 Layer directory  

Assume that your web application (web application) name is news, and enter the request path in your browser: http://localhost: 8080/uploading/load. jsp


request.getContextPath() => /uploading 
request.getServletPath() => /load.jsp 
request.getRequestURL() => http://localhost:8080/uploading/load.jsp 
request.getRealPath("/") =>  F:\learn\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\uploading\ 

The method request. getRealPath ("/") is no longer recommended

You can use the


ServletContext.getRealPath(java.lang.String) instead. 
request.getSession().getServletContext().getRealPath()  Get the actual physical path of the project file , That is, absolute address  

//Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request 
// eg./manage/editExam.domethod=goExamSet&type=U 
String url = request.getRequestURI();  
//The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters 
//eg. http://127.0.0.1:8080/manage/editExam.domethod=goExamSet&type=U 
StringBuffer url_buffer = request.getRequestURL(); 

Both methods of HttpServletRequest can only get the request url without parameters, with the following differences:

1 The former returns the relative path and the latter returns the full path

2 The former returns string and the latter returns stringbuffer

The complete request url can be obtained by the following method. getQueryString () obtains the parameter string after url, and the former is added to the request path with parameters


 String queryString = request.getQueryString(); 
ring fullPath = url + queryString;  //  Or is it url_buffer.toString()+queryString; 

Related articles: