Is it possible to get the current URL in my JSP page.

Say i open a site ""
I want to display it in page1.jsp the total url as "" If yes please tell me the step!

Thanks in Advance

Dani AI

Generated

Yes. In a JSP you can reconstruct the browser-visible URL from the servlet request, but there are a few gotchas to be aware of:

  • getRequestURL() returns scheme, host, port, and path, but not the query string. Append getQueryString() yourself if you need the full URL.
  • If your app sits behind a proxy/load balancer (your Tomcat/JBoss note), getRequestURL() will reflect how the container sees the request. To get the external URL, configure Tomcat with proxyName/proxyPort on the Connector or enable RemoteIpValve so it honors X-Forwarded-* headers. See Tomcat’s docs for HTTP Connector proxy settings and RemoteIpValve.
  • When a request is forwarded or included, you may want the original URL. The Servlet spec exposes it via request attributes like javax.servlet.forward.request_uri and javax.servlet.forward.query_string.

If you prefer to avoid scriptlets, JSTL/EL can safely render the URL and escape it for HTML:

<c:set var="url" value="${pageContext.request.requestURL}" />
<c:if test="${not empty pageContext.request.queryString}">
  <c:set var="url" value="${url}?${pageContext.request.queryString}" />
</c:if>
<c:out value="${url}" />

Reference: [HttpServletRequest#getRequestURL](https://jakarta.ee/specifications/servlet/5.0/apidocs/jakarta/servlet/http/httpservletrequest#getRequestURL()).

Recommended Answers

All 15 Replies

Any one? is that not possible..?

any example would be helpful! Pls

any example would be helpful! Pls

Does the getRequestURL() need any example?

Reconstructs the URL the client used to make the request.

classic example of "gif mi zu koduz", aka a homework kiddo that can't be bothered to do its own homework.
It probably didn't even bother to read the documentation I supplied, far too much trouble.

I guess he should realy show more interest in this case but he may be too busy with his blog. ;) <offTopic>Interesting things, but not coolest in my opinion</offTopic> I hope he will at least say thank you

classic example of "gif mi zu koduz", aka a homework kiddo that can't be bothered to do its own homework.
It probably didn't even bother to read the documentation I supplied, far too much trouble.

I guess he should realy show more interest in this case but he may be too busy with his blog. ;) <offTopic>Interesting things, but not coolest in my opinion</offTopic> I hope he will at least say thank you

bro thanks a lot.. was out of station for some days so wasn't replying.. thanks again... Sorry for late reply.. :)

Since this appears first on google for some related searches, it would be nice to have the answer, even if it is one line.

To get the full url:
String url = (request.getRequestURL()).toString();[/java][code=java]String url = (request.getRequestURL()).toString();[/java]

I am looking for the answer to this, too. Our server used to use JRun and now uses Tomcat/JBoss. We cannot know the port number used to access the JSP since JRun got it from the URL itself while Tomcat/JBoss gets the port number from the HTTP request header for the host name. Unfortunately because of this, many of our client requests did not include the port number in the request header. When I RTFM, the documentation said that it recreates...I'm going to guess that it will be recreated without the port number.

<%
Connection con = null;
String url = "jdbc:mysql://192.168.10.211:3306/";;
String db = "amar";
String driver = "com.mysql.jdbc.Driver";
int rows = 0;
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"amar","amar123");
try{
Statement st = con.createStatement();
String query = "select count(*) from user_Register";
ResultSet rs = st.executeQuery(query);

while (rs.next()) {
rows =rs.getInt(1);

%>

i hope this will help u

commented: Ignoring question, ignoring code tags and showing sample of bad programming practice -2


1. You completely disregarded question and provided "code" for something else
2. You completely ignored forum rules to always wrap programming code within posts in code-tags
3. Get your self slap on hand for doing database connection directly from page instead of servlet

<%
  String getURL=request.getRequestURL().toString();
%>
<%=getURL%>
commented: zombie master -3

Or you could just do this:

<%=request.getRequestURL()%>

or you could just read the original post before adding your nonsense.

I think it is work <%=(String)request.getRequestURL()%>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.