Avatar of Nemetona
Nemetona
 asked on

how to write an if statement in a .jsp page

Hi

I am trying to test for the name of the current database from a .jsp page.

When I use ="<c:out value='${dbname}'/> it displays the name of my database on the page but when I try
    <c:if test="<c:out value='${dbname}'/>='mydatabasename'"> it does not work.

Can I combing the c:If test with the c: out value?

When I tried it as
<c:if test="'${dbname}'=='mydatabasename'> it did not work.

Can anyone shed any light for me, please
JSP

Avatar of undefined
Last Comment
Sathish David Kumar N

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Sathish David Kumar N

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Nemetona

ASKER
Thank you for the help.

I tried the code you suggested but it did not give me the result I wished.  I double checked that I could see the value of dbname when using c:out value and it was present.

I then changed the eq to ne but still did not see any result so I wonder if the problem lies in the rest of my code.

Could you run your eye over it and see if I have anything wrong?  I am trying to display a single video if the user belongs to one database and two video's if the user belongs to mydatabase.

 <div clsss="showvideo">
       <table  border="0" cellspacing="1" width="80%" >
         <tr>
           <td width="12%"></td>
            <c:if test="${dbname eq 'mydatabase'} ">

                       <td>Introduction to the CCP</td>
                 </c:if>

           <td>How to complete your profile in CCP</td></tr>
         <tr>
           <td width="12%"></td>
           <c:if test="${dbname eq 'mydatabase'} "> 
             <td><iframe width="210" height="136" src="http://www.youtube.com/embed/xxxxx=0" frameborder="0" allowfullscreen></iframe></td>
           </c:if>

          <td><iframe width="210" height="136" src="http://www.youtube.com/embed/xxxx?rel=0" frameborder="0" allowfullscreen></iframe></td>

       </tr>

   </table>
</div>
chaitu chaitu

put this code before if statement so that whether mydatabase value is  coming or not.
<c:out value="${dbname}" />
Nemetona

ASKER
Yes, it returns mydatabase on screen.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
chaitu chaitu

try this;

<c:if test="${dbname}== 'mydatabase'}">

Open in new window

chaitu chaitu

<c:if test="${dbname == 'mydatabase'}"> 

Open in new window

Nemetona

ASKER
No change.  This code allows the second video to display but not the first one.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
chaitu chaitu

you mean to say its displaying only

td><iframe width="210" height="136" src="http://www.youtube.com/embed/xxxxx=0" frameborder="0" allowfullscreen></iframe></td>


not this one
<td>Introduction to the CCP</td>
Nemetona

ASKER
Currently it is displaying How to complete your profile in CCP and the video http://www.youtube.com/embed/xxxx?rel=0

but it is not showing Introduction to the CCP and the video ="http://www.youtube.com/embed/xxxxx=0"
SOLUTION
chaitu chaitu

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Nemetona

ASKER
No, When I hard coded the dbname value it does not appear to be going into the if loop at all.  I tried it with both eq and ne and it made no difference.

I confirmed the value of dbname by using a c:out statement after I had set the value and before the if statement.  it returned value1 eq 'value1'
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Nemetona

ASKER
Sorry, I realise that I made a syntax error when testing the hard coding.

When I set the dbname to value1 it does go into the loop.
Nemetona

ASKER
Thank you both.  I realised that when I typed in the first solution I had left a space between the } and the " and that seemed to cause it to fail.

Thanks to chainuu as well for showing me how to hard code the value to test it.
chaitu chaitu

it worked for me

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c"%>

 <table  border="0" cellspacing="1" width="80%" >
         <tr>
           <td width="12%"></td>
		   <c:set var="dbname" value="mydatabase" scope="page" />

output: <c:out value="${dbname}" />

            <c:if test='${dbname eq "mydatabase"}'>

                       <td>Introduction to the CCP</td>
                 </c:if>



           <td>How to complete your profile in CCP</td></tr>
         <tr>
           <td width="12%"></td>
           <c:if test='${dbname eq "mydatabase"}'> 
             <td><iframe width="210" height="136" src="http://www.youtube.com/embed/xxxxx=0" frameborder="0" allowfullscreen></iframe></td>
           </c:if>

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Sathish David Kumar N

your welcome ....  I didnt check the EE properly thats y i didnt update ur answer .... Any how u got answer ....