aspose file tools*
The moose likes JSP and the fly likes JSP - How to get the current URL? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP - How to get the current URL?" Watch "JSP - How to get the current URL?" New topic
Author

JSP - How to get the current URL?

Bob Smith
Greenhorn

Joined: Sep 10, 2008
Posts: 12
I'm new to jsp and would like to know how to get the current JSP file name that I am on.

I have see people use ${pageContext.request.requestURI} to get up to the current path, but how do I just get the current file name?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Please take the time to choose the correct forum for your posts. This forum is for questions on beginning Java.

For more information, please read this.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

The fact that you need the name of the JSP is a red flag that you have a serious design problem. In fact, in a properly structured web application, the name of the JSP is abstracted away and there is no way to know what it is.

Why not let us know why you think you need this information and we can guide you to a better way of doing things.
Bob Smith
Greenhorn

Joined: Sep 10, 2008
Posts: 12
I have a menu that I am building and putting in a tag file. I want to send the current page to the tag file so that the link to the current page in the menu is not active. In other words the link to the current page will not be active.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Using the page name as a trigger is a really bad and fragile design. A better approach would be to have an attribute on the tag that tells the tag which entry to disable. The calling pages can set this value as appropriate.

This is an example of loose coupling and is a good precedent to follow.
Bob Smith
Greenhorn

Joined: Sep 10, 2008
Posts: 12
So I should just send an attribute such as this

<myTag:default_menu currentpage="home" />

Then test currentpage in the tag page?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Something along those lines, yes. That way, the code isn't tied to the file name, which is (a) fragile, as everything breaks if the file name is changed, (b) difficult to deduce in a deterministic manner (there are way too many "what if" scenarios).

The fewer things that depend upon implicit dependencies (tight coupling) like filenames, the more robust and less fragile your code will be.
dan hagar
Greenhorn

Joined: Nov 17, 2007
Posts: 3
Bear Bibeault,

Please don't post if you don't know the answer to the question being asked. Your unsolicited design claim makes this thread useless. Bob's question is valid not to mention the fact that his approach is simpler and will require less maintenance in the future. In a small system this will work just fine. I suppose it would be appropriate for you to offer design advice as a footnote but only after you have answered the question correctly.

Thanks,

Dan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

You're new here aren't you? Welcome to the Ranch! JavaRanch is a learning site, not an answer or code mill. If the posting of advice and best-practice recommendations offends you -- indeed, in a thread in which you did not even participate -- then you may be disappointed here.
[ December 31, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Also, please read this for information on posting in older topics.
Anna Anna
Greenhorn

Joined: Nov 13, 2008
Posts: 4
HI
I think the below oneline will be helpful for you.
we can get the complete uri that means the current page url with the jsp name.

String strHost=request.getHeader("referer");
strHost will return like http://host:port/app/test.jsp
dan hagar
Greenhorn

Joined: Nov 17, 2007
Posts: 3
Bear Bibeault,

Thank you for the warm welcome. I've used this resource for several years although I generally don't post(call me shy). After 19.5 years of dealing with developers like myself, I am comfortable with spirited interactions. I read the link you suggested concerning older topics however I still would have posted to this thread despite the fact that it is not in my nature to post. I felt compelled to contribute my perspective because I believe that there is a bigger topic here. I'd like to see JavaRanch continue to be "a friendly place for greenhorns". You may want to try a little role playing. Imagine yourself as Bob and have someone read to your thread contributions to you.

Thanks,

Dan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Please continue any personal discussion in email or via private message.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

"AnnaVA", please check your private messages for an important administrative matter one final time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP - How to get the current URL?