0

I am new in java and javascript. I want to pass a value from function in js file to JSP file. But I didn't get the value and it shows NULL. Let's say js file named as maintain.js and JSP file as form.jsp. Supposedly the value obtains from function check in maintain.js are pass to JSP file which when onclick it passes the value to the searchItem function which in the form.jsp file.

In maintain.js file

function check(){
 var p = "apple";
   var A = "form.jsp?apple=" + p;
}

In my form.jsp file

 <div id="openModalDialog" align="center"  style="display:none">
   <form id="srch_grp">
       <%  String la=request.getParameter("apple"); 
            System.out.println("apple:" + apple);

       %>
 <input type="button" name="btnitemsearch" value="Search" onclick="searchItem('<%=apple%>')">
     </form>
  </div>

In a console, I getapple:null. What do I wrong here?

Thank you in advance.

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
Iyda
  • 25
  • 8
  • 2
    JSP code runs on the server. JS code embedded in or loaded by the page runs on the client. JS can communicate with the server in various ways, but by the time it has a chance to do so, the JSP involved in rendering its page has already completed its work. – John Bollinger May 16 '18 at 03:27
  • you are using incorrect variable 'apple'. Use 'la' in your onclick function. – Derrick May 16 '18 at 04:48

1 Answers1

2

Instead of using

<%=apple%>

you should use <%=la%> It would be work

<div id="openModalDialog" align="center" style="display:none">
    <form id="srch_grp">
        <% String la=request.getParameter( "apple"); System.out.println( "apple:" + la); %>
            <input type="button" name="btnitemsearch" value="Search" onclick="searchItem('<%=la%>')"> 
     </form>
</div>
Derrick
  • 3,669
  • 5
  • 35
  • 50
Vinod Kumawat
  • 741
  • 5
  • 8