how to write an if statement in a .jsp page

Nemetona
Nemetona used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
<c:if test="${dbname eq 'mydatabasename'} ">

Author

Commented:
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}" />
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Author

Commented:
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

Author

Commented:
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>

Author

Commented:
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"
hard code the dbname value like this and see whether its going into if loop or not.
<c:set var="dbname" value="value1" scope="page" />


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

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

Open in new window

Author

Commented:
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'

Author

Commented:
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.

Author

Commented:
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 ....

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial