views:

78

answers:

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

     %>
    df('ADD');        

    }

When i click on add, i need to check whether the empSelected is true or not and pass this canModify value. Will this be called?

Is this right way i am checking a Scriptlet inside a JavaScript

+1  A: 

The canModify value defined in JSP is never passed to JavaScript. You need to redefine the variable in JavaScript, for example:

<%
if (canModify) {  // This is the JSP variable
%>
  var canModify = true;  // This is the JavaScript variable
<%
} else {
%>
  var canModify = false;
<%
}
%>

On a different note, you should abandon JSP scriptlets and switch to JSTL.

casablanca
If the canModify is true, then i need to execute this JSP statement UTIL.hasSecurity("PFTMODFY")
John
@John: Am I missing something? The code you posted is different from what you mentioned in your comment.
casablanca
Yes... if the canModify is true can i execute any JSP statements
John
@John: Yes you can, but they will be executed on the server-side and *only once*, so if you expect it to execute whenever the JavaScript `add` function is called, it won't work that way.
casablanca
+1  A: 

A more elagant way to do this

var canModify = Boolean(${canModify});

Use jstl el, it turns more clear what do you intend to do. The call to boolean will convert the given value in javascript boolean.

Remember:

Boolean(true); // returns true
Boolean(false); // return false
Boolean(); // returns false
madeinstefano
This will only work if "canModify" is an attribute in the page or request scope. The way it's defined in the original question, this will not work.
Pointy
+2  A: 

You need to get the following concept right: Java/JSP runs at webserver and produces HTML/CSS/JS output. Webserver sends HTML/CSS/JS output to webbrowser. Webbrowser retrieves HTML/CSS/JS output and displays HTML, applies CSS and executes JS. If Java/JSP has done its job right, you should not see any line of Java/JSP code in webbrowser. Rightclick page in webbrowser and choose View Source. Do you see it, right?

The webbrowser has totally no notion about the Java/JSP code on the server side. All it knows about and can see is the HTML/CSS/JS code it has retrieved. The only communication way between webbrowser and webserver is using HTTP requests. In the webbrowser, a HTTP request can be fired by entering URL in address bar, clicking a (bookmark) link, pressing a submit button or executing XMLHttpRequest using JavaScript. In the webserver, the Java/JSP (and Servlet) code can be configured so that it executes on certain URL's only. E.g. a JSP page on a certain location, a Servlet which is mapped on a certain url-pattern, etcetera.

In a nutshell, to have JavaScript to access Java/JSP variables, all you need is to let Java/JSP print them as if it is a JavaScript variable. To have JavaScript to execute Java/JSP methods, all you need is to let JavaScript fire a HTTP request.

See also: Communication between Java/JSP/JSF and JavaScript

BalusC