views:

47

answers:

3
<%   if(empRecNum != null && !(empRecNum.equals("")))
        {
           empSelected=true;
        }
        boolean canModify = UTIL.hasSecurity("PFTMODFY") && empSelected;
%>

<script type="text/javascript">

    function add(){
        alert(canModify);
        df('ADD');
    }

</script>

I need to alert the canModify in my JavaScript

+3  A: 

Do some thing like.

function add(){
        alert(<%=canModify%>);
        df('ADD');
    }
org.life.java
+2  A: 

I know I use something like this:

<%
String s = "BOB";
%>
<script>
alert('<%= s %>');
</script>

The '<%= %>' tag lets you put in the value of the java variable.

josh.trow
You need to escape the string to block XSS attacks.
SLaks
Yes, and the simplest way to do that is to use a JSON encoder.
bobince
+3  A: 

Get away from using scriptlets, use jstl and expression language and it would look like this:

alert( ${UTIL.hasSecurity("PFTMODFY") 
         && empRecNum != null 
         && !(empRecNum.equals("")
        }
      );
NimChimpsky
That will work **if** "UTIL" is a reference that's available in the request attribute map (or maybe the page attribute map; I can't recall whether that works implicitly or not). If it's just a variable declared in a scriptlet, then it won't work. I do agree with you however that scriptlets are horrid and in 2010 nobody should be using them.
Pointy