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
ASKER CERTIFIED SOLUTION
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
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>
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

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

ASKER

Yes, it returns mydatabase on screen.
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

try this;

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

Open in new window

Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

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

Open in new window

Avatar of Nemetona
Nemetona

ASKER

No change.  This code allows the second video to display but not the first one.
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

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>
Avatar of Nemetona
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
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Nemetona
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'
Avatar of Nemetona
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.
Avatar of Nemetona
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.
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

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 ....
JSP
JSP

JavaServer Pages (JSP) allow the development of dynamically generated web pages. It uses the Java programming language; JSP pages are translated into servlets at runtime, with each servlet being cached and reused until the JSP is modified. JSP allows Java code to be interleaved with static web markup content, so the resulting page can be compiled and executed on the server to deliver the content.

16K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo