This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes Tags Not Scriplet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Tags Not Scriplet" Watch "Tags Not Scriplet" New topic
Author

Tags Not Scriplet

Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
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

Joined: Jan 10, 2002
Posts: 61458
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
Thank you so much. I'll use Ben's solution
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

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

Joined: Sep 13, 2005
Posts: 238
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

Joined: Sep 13, 2005
Posts: 238
Thanks for your feedback Bear. Your example was no doubt helpful.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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
 
subject: Tags Not Scriplet