Comparison of various methods for obtaining url Information in jsp Request, requesturl

Source: Internet
Author: User

Comparison of various methods for obtaining url Information in jsp Request, requesturl

Various path information can be obtained from the Request object. The following example shows:

If the requested page is index. jsp and the project is WebDemo, the following information about various paths of the request object is obtained in index. jsp:

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>"); 

Result:

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 the corresponding request methods.

Obtain the summary of various paths from the request:

Request. getRealPath ("url"); // The virtual directory maps to the actual directory request. getRealPath (". /"); // request in the directory where the webpage is located. getRealPath (".. /"); // The previous directory of the directory where the webpage is located

Assume that your web application is named news. In your browser, enter the Request Path: 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 request. getRealPath ("/") method is no longer recommended.

Available

ServletContext. getRealPath (java. lang. String) instead. request. getSession (). getServletContext (). getRealPath () Get the actual physical path of the project file, that is, the 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 that does not contain parameters,The differences are as follows:

1. The former returns the relative path, and the latter returns the complete path.

2. The former returns string, and the latter returns stringbuffer.

To obtain the complete request url, you can use the following method. getQueryString () obtains the parameter string after the url, and adds it to the Request Path with parameters.

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

The above is all the content of the comparison of various methods for getting url information through jsp Request provided by xiaobian. I hope it will be helpful to you and provide more support to the customer's home ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.