Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Getting the path to the current page...

Posted on 2002-04-27
Medium Priority
Last Modified: 2010-05-18
What is the "correct" means by which to determine the path the current page; that is to say the page which was requested? Thanks,

Jack D. Bates
Question by:goots
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

coreyit earned 800 total points
ID: 6974671
I'm not totally clear on what results you're wanting, but there are a number of possibities along these lines.

ServletContext.getRealPath(java.lang.String path) sould give you the file system path to a requested file.

HttpServletRequest.getRequestURI() would give you a context-relative path (for http://www.ste.com/dir/page.jsp would give you /dir/page.jsp)

check out http://java.sun.com/j2ee/sdk_1.3/techdocs/api/index.html for more possibilties.

LVL 28

Expert Comment

ID: 6975012
try this  
LVL 19

Expert Comment

ID: 6978209
If you want to know the URL that is in the browser.. you need JavaScript.  The Servlet will only know the path of the servlet on the system.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 28

Expert Comment

ID: 6978784
cheekyci>The Servlet will only know the path of the servlet on the system.
I am confused I thought he wanted the path of the request.

Expert Comment

ID: 6979972
          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.
LVL 19

Expert Comment

ID: 6981398
Servlet only knows its path on the system.

For example a servlet doesn't know that if it was a part of a forward, include or a direct request.

It just knows.. it was called and its path.

getRequestedURI() will tell you the requested URI but not the actual URL typed by the user.


LVL 28

Expert Comment

ID: 6981712
I was assuming that the page was a JSP.  
So locally I used  
and the output was  
when I used  
the output was  
LVL 19

Expert Comment

ID: 6981800
remember JSPs are converted to servlets.. so you can still forward to them and include them.

LVL 28

Expert Comment

ID: 6981855
But, the servlet that the JSP generated, doesn't matter here. We are just  
>Getting the path to the current page...  
LVL 19

Expert Comment

ID: 6981878
that was my question.. do they want the path to the servlet or the location the user called.

for example if the user types in.


but test.jsp is including test2.jsp

and test2.jsp is including include.jsp

How do you know what file is really calling include.jsp

the URI will say test.jsp when in actuality it is test2.jsp

so it depends on the scenario.. does the question asker here want the calling servlet or the actual URI.

You can't get the calling page.

sometimes the URI will not match what the user typed in.. in the scenario of frames.

if I have a page called index.htm that has test.jsp in the one of its frames.. what will the URI be?

It will be test.jsp but in reality the user has index.htm loaded right?

Just some scenarios that I have run into :-)


Expert Comment

ID: 7615199
> if I have a page called index.htm that has test.jsp
> in the one of its frames.. what will the URI be?

> It will be test.jsp but in reality the user has
> index.htm loaded right?

Yes, but ... in this case, one of the components
of index.htm (i.e., test.jsp) is being loaded
via a separate HTTP request.  This is basically
the same as the URL for an image in a page.  The
user never sees the URL, but the request for the
image is separate and doesn't have much to do with
the original page (except as a referrer).

So this particular case seems to me to be the
correct behavior, and separate from the question
of redirects/includes _within_ the server
processing of the request.

  - ch

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Geo-targeting is the practice of distributing content based on a person’s location, as best as you can determine it. Let’s look at some ways you could successfully use this tactic. The following tips and case studies could lead to meaningful results.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question