• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using parameters for if statement inside a jsp

 
sanitation engineer
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an if statement in my jsp page that shows one piece of html if true and another if it's false. This works.
I need to have a parameter value, on a href link that is on this same page, so that if that link is clicked, the if statement will be true, and the page will be redisplayed with the specified piece of html.
I hope I explained well and someone knows how to do this. Thanks!
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I understand.
On the calling page, the href uri needs to include a parameter. Example
< a href="http://www.javaranch.com/index.jsp?showPart1=true">JavaRanch....
In your jsp you can test the value in a manner similar to:
< %
String showIT = request.getParameter("showPart1");
if(showIT.equals("true"))
{......
[ February 15, 2002: Message edited by: Carl Trusiak ]
 
Carlisia Campos
sanitation engineer
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot! However, I'm getting a null error.
<a href="index.jsp?showOSMain=true">
The showOSMains parameter has a null value whe the jsp runs instead of a "true" value. Any hint as why the jsp page is not recognizing this value?

Thanks.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the Parameter showOSMain or showOSMains? the spelling, case everything has to match exactly.
 
Carlisia Campos
sanitation engineer
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's showOSMain, I misspelled it in the msg, sorry. The page compiles and builds ok. If I substitute the
<% String showString = "false";
showString = request.getParameter("showOSMain");
if (showString.equals("true")) { %> <jsp:include page="opensource/opensourcemain.html" flush="true" />...
by: if (true) { %> ...
It runs fine and gives me the correct page. It's just when the parameter [showString.equals("true")] gets passed it has a null value, and that is a boo boo.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You definately have something miss typed. I ran a test real quick the href :
< a href="testCat.jsp?showOSMain=true">Test it Carl</a>
and textCat.jsp simply
< %= request.getParameter("showOSMain") %>
And it prints true so, check your spellings between the two very closely.
 
Carlisia Campos
sanitation engineer
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Carl Trusiak:
You definately have something miss typed. I ran a test real quick the href :
< a href="testCat.jsp?showOSMain=true">Test it Carl</a>
and textCat.jsp simply
< %= request.getParameter("showOSMain") %>
And it prints true so, check your spellings between the two very closely.


Well, I ran your test on my jsp and the value I get is null. The null value actually now makes sense, probably it happens because the link hasn't been clicked yet (but then how did you get it??). But when I use an if statement to account for the null value (if null display x, else y), I get the same nullpointerexception error.
 
Carlisia Campos
sanitation engineer
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I got it.
The problem: on my if statement, I was testing for (showString.equals(null))
Solution: (showString.equals("null"))
It was the quotes around the null inside the operation that were missing that was the problem.
Thank you so much for your help.
 
Carlisia Campos
sanitation engineer
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, that did work at some point but then it stoped working again. The problem is that .equals() does not take a null value for comparisson. That has to be done with ==.
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic