JSP request get URL information of various methods to compare _jsp programming

Source: Internet
Author: User
Tags stringbuffer

You can get various path information from the request object, the following example:

If the requested page is index.jsp and the project is Webdemo, the various path information about the request object is obtained 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 the various corresponding methods in the request

get various path summaries from request:

Request.getrealpath ("url");//The virtual directory is mapped to the actual directory 
Request.getrealpath ("./");//The directory where the page is located 
request.getrealpath (". /");//The previous level of the directory in which the page is located 

Assuming that your Web application (Web application) name is news, 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\ 

Now the Request.getrealpath ("/") method is deprecated.

can use

Servletcontext.getrealpath (java.lang.String) instead. 
Request.getsession (). Getservletcontext (). Getrealpath () gets the actual physical path of the engineering file, which is the absolute address 
Returns the part of this request is the URL from the "protocol name up to the" query string in the "the" The HTTP Reque St 
//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 PA Rameters 
//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 request URLs that do not contain parameters, and the difference is as follows:

1 The former returns a relative path, which returns the full path

2 The former returns a string, the latter returns StringBuffer

The complete request URL can be obtained by means of the following method, GetQueryString () is the parameter string after the URL, and the former is added to the request path with the parameter.

 String querystring = Request.getquerystring (); 
Ring FullPath = URL + querystring;  

The above is a small series for everyone to bring JSP request to get URL information of the various methods of comparison of all the content, I hope to help you, a lot of support cloud Habitat Community ~

Related Article

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.