JSP can get URL of current webpage. This is server side script code if you are looking for URL of browser. JAVA has function to get URL with request object.
request.getRequestURL() method return full path of webpage opened on web browser and return type in StringBuffer. This StringBuffer can be type cast to string by toString() method.
getRequestURL object returns with host name, port, protocol (http or https), context name and file name.
In example we will explain about how to get URL with jsp code. getRequestURL is method of request object.
If you want to use URL for manipulation, you can use other method instead of getRequestURL().
request.getContextPath(); this will return context path of URL.
If URL is like that http://localhost:8080/jsp/url.jsp
It will return jsp
request.getScheme() will return http or https
request.getServerName() will return domain name e.g. localhost
request.getServerPort() will return port of server e.g. 8080 or 7001
Example will explain
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %> <% String getURL=request.getRequestURL().toString(); %> <html> <head> <title>JSP get URL</title> </head> <body> <%=getURL%> </body> </html>
Output
http://localhost:8080/jsp/url.jsp
Individual URL can also get
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %> <% String path = request.getContextPath(); String getProtocol=request.getScheme(); String getDomain=request.getServerName(); String getPort=Integer.toString(request.getServerPort()); String getPath = getProtocol+"://"+getDomain+":"+getPort+path+"/"; String getURI=request.getRequestURI(); %> <html> <head> <title>JSP get URL</title> </head> <body> <%=getPath%> <br /> <%=getURI%> </body> </html>
Output
http://localhost:8080/jsp/
/jsp/getURL.jsp



Link to Us
Thanks a ton… Second program was the one i was searching for…
Sometimes, <%=getURI%> return \"null\"
In that case, use the following code to get the URL:
<%@ page import=\"java.util.*\"%>
<%
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = (String)headerNames.nextElement();
if(headerName.equals(\"Referer\")){
out.println(headerName + \" >>>::\"+request.getHeader(headerName));
}
}
%>
The \"Referer\" will display the url as
Referer >>>:: http://localhost:8080/jsp/url.jsp
Good it work for me
Thanx so much!!!!!!!!!!!!!!!!!!!