Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tags Not Scriplet

 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone plz tell me how do I write the following scriplet code using tags (either struts or JSTL)

[% if(request.getServerPort() == 8443) {%]

[base href="https://<%=request.getServerName()+ ":" + request.getServerPort() + request.getContextPath() %>/" /]
[%}

else { %]
[base href="http://<%=request.getServerName()+ ":" + request.getServerPort() + request.getContextPath() %>/" /]
[%} %]

where every square bracket is actually an angle bracket.

thanks
Imad
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how I might do it:



Actually, I'd probably write a custom tag to get all the goo of the page, but this is probably pretty close to how I'd do without writing my own tag.

I might probably also factor out pageContext.request to its own scoped variable since it is referenced so many times.
[ September 16, 2005: Message edited by: Bear Bibeault ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, you forgot to look at the superclass ServletRequest and it's getScheme method which eliminates the need for the switch.
It will also allow you to configure the server's ports without having to alter your code.



Also, getContextPath will provide the preceeding forward slash so you don't want to put it between getServerName and getContextPath

I agree with Bear though, it's much too long; begs for a custom tag.
[ September 16, 2005: Message edited by: Ben Souther ]
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much. I'll use Ben's solution
Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much. I'll use Ben's solution


Good plan. The less goo on the page the better. I just hope that my example was instructive with regards to using JSTL/EL on page for other purposes.
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben

I am trying to use the solution you provided but its not working in IE 6. It works fine in Mozilla. Do you know why ?

Appreciate your help.
Thanks
Imad
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your feedback Bear. Your example was no doubt helpful.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is server side code, it shouldn't make any difference what browser you're using. Look at the HTML source and see if it's being printed any differently.

If you mean that the <BASE tag isn't working as you expected, that's a different issue and has nothing to do with JSP.

The more common approach for J2EE apps is to make all links relative to the context root:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic