Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
How to write JSP Code inside a Javascript if-statement?

I hope everyone is doing well!

I was wondering if there was a way to write JSP code inside a Javascript if-statement?

For instance, the testVoid() method from the funStuff.java class changes a certain String in the class, and then retK() returns that String.

However, the JSP code runs irrespective of the condition in the if-statement. I saw another post that stated using JSTL could resolve this, but I am not sure how to write the intended code segment using JSTL.

Any help would be appreciated! Thanks!

<script>
if (false) {
<%
  funStuff.testVoid();
%>  
}

console.log('<%= funStuff.retK() %>');
</script>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I was wondering if there was a way to write JSP code inside a Javascript if-statement?

No, this isn't possible.

At a high-level, once the request lands at the server:

  1. Server runs any JSP and produces a new page. After this step, any JSP things are complete.
  2. Server sends that generated page to the client
  3. Client (browser) sees the page, maybe runs some Javascript (if present)

Your desired behavior is:

  1. Javascript code runs first, checks the if statement condition, then proceeds with the body of the if statement only when "condition" was true.
  2. JSP code inside the Javascript if block runs

The actual behavior is:

  1. JSP code runs first, no matter what your intended code logic is. All JSP throughout the entire page will run first, always.
  2. The output of the JSP code is mixed with the rest of the static HTML+Javascript in the apge.
  3. The browser gets involved for the first time, seeing the newly-created page: JSP output + static HTML and Javascript.
  4. Browser runs any Javascript in the page.

Your example starts like this as JSP:

<script>
if (false) {
<%
  funStuff.testVoid();
%>  
}
console.log('<%= funStuff.retK() %>');
</script>

The server will evaluate the JSP <% ... %> bits, so for example, assume that:

  • funStuff.testVoid() returns "xyz"
  • funStuff.retK() returns "K"

then the server will produce this:

<script>
if (false) {
  xyz
}
console.log('K');
</script>

The browser will receive the output as above, having no knowledge that "xyz" or "K" were generated on the fly vs. being pre-written into the page, or that JSP scriptlets were involved in any way.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!