Skip to content Skip to sidebar Skip to footer

Checking Whether The Variable Is True Or Not

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

Solution 1:

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


Solution 2:

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

Solution 3:

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.


Post a Comment for "Checking Whether The Variable Is True Or Not"