-4

I am calling a Javascript test() function for validation purpose before it can be committed. This function has Java scriptlets where validation logic is return and it updated a boolean variable. Using this boolean variable, I am checking if this js function should return true or false. If false then it is negative case of validation and in true positive case of validation.

The general code is something like:

<% boolean check = false ; %>
<script>
function test(){
/*
* java code for updating test variable for validating values on the jsp page
*/

if(!check){
<% System.out.println("this runs ! "); %> // line 1
alert("validation fails"); // line 2
return false;
}
else return true;

}
</script>
..
..
<input type="submit".. onclick="return test()">
..
..

Is it going wrong somewhere? I have been trying a lot of things for line 1 to run, but it doesnt. My code execution reached inside the if check at line 1, but doesnt alert i.e line 2 and the validation thing also doesnt work.

Someone had similar issues? What is the resolution?

Groot
  • 211
  • 1
  • 5
  • 11
  • Naming everything `test` seems unwise. Give different names to different things so the system knows which one you're talking about. Once you do that, also examine the actual client-side code that's in your browser and see if it's what you expect. Check the browser's debugging console for errors, use the debugger, etc. What is this even supposed to do inside the `test` function?: `if(!test)` Are you checking if the function exists? From inside the function itself? Also, `this runs!` is a syntax error in JavaScript. It seems like there's *a lot* wrong here. – David Aug 29 '18 at 17:41
  • 1
    You might want to take some time going through the basic concepts of server/client-side development before continuing. There are issues everywhere: mixing server/client-side variables, invalid `onclick` expression, etc. – Derek 朕會功夫 Aug 29 '18 at 17:42
  • using test variable was just for reference, I just changed it to check now. Actual code has different names. – Groot Aug 29 '18 at 17:45
  • @Groot: With the updated code it looks like the other comment above is spot-on. You're *very much* misunderstanding the difference between server-side code and client-side code. Study this: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming It's about PHP, but the concepts are identical. – David Aug 29 '18 at 17:46
  • <% System.out.println("this runs ! ") ; %> using this I am trying to add check points, I am comfortable with java thats why added this. This will check if the logic is correctly put. Also how is it invalid `onclick` expression? – Groot Aug 29 '18 at 17:48
  • Should I use document. using name and access the values of the page and then run js function? – Groot Aug 29 '18 at 17:50
  • @Groot: When you load this page in the browser, examine the page source within the browser. I think you'll find that the lone statement `this runs !` is not valid code and is resulting in a JavaScript syntax error. This comes from the previous misunderstanding on your part. Server-side code runs *on the server* and it runs *before the page is sent to the browser*. – David Aug 29 '18 at 17:50
  • @David the error is not with the comment line I added, it is with the alert function. The comment line print whatever I write in it. Is it still the reason for alert() to not work? – Groot Aug 29 '18 at 17:54
  • @Groot: Specifically what "error" are you referring to? – David Aug 29 '18 at 17:54
  • the alert does not pop up on the window and the validation also does not work the call to `return test()` – Groot Aug 29 '18 at 17:56
  • @Groot: Again, examine the page source in your browser. What code is there? Check your browser's development console. Are there any errors? When you debug in your browser, is the function ever invoked? What happens? You're also trying to use a JavaScript variable called `check` which you never defined in JavaScript. I would expect that behavior to be undefined. – David Aug 29 '18 at 17:58

3 Answers3

0

The problem I see is, that the declaration of boolean check is in Java, but the test for it is in Javascript in the genrated page. There is no Javascript variable check in the generated page.

Ondřej Fischer
  • 411
  • 1
  • 7
  • 16
  • I am trying to correct that, so if I create a `var check = false` and try to update its value using logic written in java, how could that be done? – Groot Aug 29 '18 at 18:11
0

Here 'check' is a Java variable, not javascript variable. So try if(<%=!check%>)

0

You're very much confusing the difference between server-side code and client-side code. Conceptually think of them as entirely separate. Your server-side code is this:

boolean check = false;
System.out.println("this runs ! ");

Two things to notice:

  1. You define a variable that you never use.
  2. The message will always print to the output because there's no reason why it shouldn't.

Your client-side code is this:

function test(){

    if(!check) {
        alert("validation fails");
        return false;
    }
    else return true;

}

If you're successfully invoking test() you'll find that this will produce an error on the browser's console because check is undefined.


As for how to correct this, that really depends on what you're actually trying to do here. "I want to use a Java variable in JavaScript" is not the problem you're trying to solve. Stepping back from that, there must be a reason behind writing this code. Whatever that reason is, it's very likely that there's a better way to do this than to continue down this path, given that you took a wrong turn on the path a while ago.

However, if all you want is to output that variable to JavaScript, you may be able to do something like this:

if(<%=!check%>) {
    alert("validation fails");
    return false;
}

This would output the result of evaluating !check on the server, and that result would be a raw string embedded in the JavaScript code. I'm not 100% sure if Java/JSP/etc. evaluates that way, but hopefully you get the idea. Examine the resulting client-side code to see what it actually outputs of course. Ideally it would end up something like this:

if(true) {
    alert("validation fails");
    return false;
}

Granted, if this bit of hand-waving is covering up more problems:

/*
* java code for updating test variable for validating values on the jsp page
*/

Then we won't be able to determine that within the scope of this question.

David
  • 208,112
  • 36
  • 198
  • 279