Solved

Getting the path to the current page...

Posted on 2002-04-27
11
194 Views
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
0
Comment
Question by:goots
11 Comments
 
LVL 2

Accepted Solution

by:
coreyit earned 200 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.

-corey
0
 
LVL 27

Expert Comment

by:rrz
ID: 6975012
try this  
<%=application.getRealPath(request.getServletPath())%>
0
 
LVL 19

Expert Comment

by:cheekycj
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.

CJ
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 27

Expert Comment

by:rrz
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.
0
 

Expert Comment

by:MichaelSonny
ID: 6979972
getRequestURI()
          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.
0
 
LVL 19

Expert Comment

by:cheekycj
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.

CJ

0
 
LVL 27

Expert Comment

by:rrz
ID: 6981712
I was assuming that the page was a JSP.  
So locally I used  
<%=application.getRealPath(request.getServletPath())%>  
and the output was  
C:\Tomcat\webapps\MyContext\jsp\MyPage.jsp  
when I used  
<%=request.getRequestURI()%>  
the output was  
/MyContext/jsp/MyPage.jsp  
0
 
LVL 19

Expert Comment

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

CJ
0
 
LVL 27

Expert Comment

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

Expert Comment

by:cheekycj
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.

test.jsp

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 :-)

CJ
0
 

Expert Comment

by:chamlin
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best tutorial on log4j and logging for servlets/jsp codes 1 65
spring example errors 33 405
by zero exception 10 52
running on tomcat not jboss eap 7.0 3 18
Giving access to ESXi shell console is always an issue for IT departments to other Teams, or Projects. We need to find a way so that teams can use ESXTOP for their POCs, or tests without giving them the access to ESXi host shell console with a root …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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