• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP - How to get the current URL?

 
Bob Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please read this for information on posting in older topics.
 
Anna Anna
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please continue any personal discussion in email or via private message.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"AnnaVA", please check your private messages for an important administrative matter one final time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic