hi all;
i always find answers for my problems here at daniweb, but this is my first time i post a question, so have mercy on me :)

i have a jsp page and a huge scriptlet inside of it, the page adds new users to the application, so i just added a IF statement

<%
if(true)
{
System.err.println("done")
}else
{
System.err.println("not done")
}
%>

and it works just fine, but then i wanted to output a js alert so i did

<%
if(true)
{
System.err.println("done")
}else
{
System.err.println("not done")
%>
<script>
alert("error");
</script>
<%
}
%>

but the alert wont fire, and i don't even get any error as if the whole line is not there, so i thought maybe u could guide me to solve this issue.

thanks in advance

Why dont u make this given code in js file?

hi there hako and thanks for replying,
why do u think it will work if i called the function from a js file??
i am wondering if the problem is that i cannot call a script tag through the execution of a scriptlet since the scritplet should be already executed on the server and the script tag will have to wait untill the page is called from the client???

i don't know...

The alert will not fire because it is in the else block. The if is true:

<%
// the if is always true. It will not go to the else and the alert which is inside the else will not execute
if (true) {

} else {
   %>
   <script>alert("error");</script>
   <%
}
%>

The if is always true, so the script that is inside the else will never be rendered to the page and will not execute.

don't EVER use scriptlets.
There's not been a need for them for at least a decade and they lead to essentially unreadable code.
Use JSTL instead.

The alert will not fire because it is in the else block. The if is true:

i am sorry for writing my example the wrong way, actually in th if statement i have a condition that is some times true and other times false.
and no matter what the condition value is, the scriptlet is ignoring the js script.

don't EVER use scriptlets.

thanks for the advice but this application was built using scriptlets, there is not a single servlet in it....and my company is responsible for some modifications...so technically im stuck with it ;)

now i tried something else and i think i am so close

if(rs1.next()&&rs1.getString("psm_first_name").equals(FirstName))
                {
                flag = 1;
                System.err.println("ResultSet IS NOT Empty");
                }else
                    {
                flag=0;
                System.err.println("ResultSet IS Empty");
                }

this if statement either assigns 0/1 to a variable called flag.
and i have a button after this if statement as follows:

<input type="image"  tabindex="22" id="addBtn" src="../images/add_big.gif" alt="<%=st.getSiteTerm("add")%>" [B]onclick="<%if(flag==1){%>hi();<%}else{%>setAction('1');<%}%>"[/B]>

so what i want is (if flag is 0) then the JS function hi() is triggered and (if flag is 1) the the JS function setAction('1') is triggered.
but no matter what the value of (falg) is, the function hi() is always called.!!!
i hope the problem is properly described.
and thanks a lot for ur replies. :)

I tried that code and it works ok. Are you sure about the value of the flag?

Are you sure about the value of the flag?

yes, i printed the flag value along with the message

System.err.println("ResultSet IS NOT Empty"+flag);

but if u say it working just fine with u, then i will look into it with more concentration it might be something elsewhere...i really dunno.

Value of Flag: <%=flag%>
<input type="image"  tabindex="22" id="addBtn" src="../images/add_big.gif" alt="<%=st.getSiteTerm("add")%>" onclick="<%if(flag==1){%>hi();<%}else{%>setAction('1');<%}%>">

Try this to make sure, and then right click the page and select view source code. And try to see the html that is rendered. try to see which method is written.

it is very wierd, really, it writes the vaue of flag correctly but no matter what happens the function that is bieng called is the first one no matter what it is
here the function that is called is hi()

<input type="image"  tabindex="22" id="addBtn" src="../images/add_big.gif" alt="<%=st.getSiteTerm("add")%>" onclick="<%if(flag==1){%>[B]hi()[/B];<%}else{%>setAction('1');<%}%>">

and here setAction('1') is the one that gets called

<input type="image"  tabindex="22" id="addBtn" src="../images/add_big.gif" alt="<%=st.getSiteTerm("add")%>" onclick="<%if(flag==1){%>[B]setAction('1');[/B]<%}else{%>hi();<%}%>">
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.