1

I have a JSP page that need read a value of a input in a if, i tried with Jquery but don't work.

<%-- my input with the year value --%>    
<input type="text" id="year" value="2021">

<%-- my JSP page need test if year is bigger then 2019 --%>
<c:if test="${ $("#year") > 2019 }">
    ...
</c:if>

Any help is welcome.

sergioBertolazzo
  • 585
  • 2
  • 11
  • 27
  • 1
    JS runs too late in the page lifecycle for this to work as you are attempting. – Rory McCrossan May 13 '19 at 13:08
  • 3
    Regardless of the languages/frameworks being used, the information here is vital for anybody who wants to do web programming: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – David May 13 '19 at 13:13
  • 1
    Read the link by @David, but in short: the key is to realize, **where** and **when** which code is executed - JSP on the server, when the page is requested and rendered (i.e. **before** the response is sent to the browser), and Javascript in the browser, **after** the browser receives the **already generated** response. – Jozef Chocholacek May 13 '19 at 13:25

1 Answers1

0

I could solve this question with 3 steps:

First, i send the value of the input in a ajax parameter

year    : $("#year").val()    

After i recive the value in Servlet

String year_value = request.getParameter("year");

And set a atributte to get value in JSP page.

request.setAttribute("year_value", Integer.parseInt(year_value));

In JSP page i use the variable year_value to compare in if.

<c:if test="${ year_value > 2019 }">
    ...
</c:if>
sergioBertolazzo
  • 585
  • 2
  • 11
  • 27