Link to home
Start Free TrialLog in
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
ASKER CERTIFIED SOLUTION
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nemetona
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>
put this code before if statement so that whether mydatabase value is  coming or not.
<c:out value="${dbname}" />
Yes, it returns mydatabase on screen.
try this;

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

Open in new window

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

Open in new window

No change.  This code allows the second video to display but not the first one.
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>
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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'
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.
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.
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

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