简体   繁体   中英

How to access a variable in a function from inside a jsp tag

I have the following function:

 <script>
  function assign()
  {
  var val = "";
  val = document.form1.text1.value;
  alert(val); 
  }

I want to access the variable val's value inside jsp tag so that i can pass it to the googlePlus method as a String. I tried making var val as a global variable, but it doesnt work. How can I access the variable val inside the following code?

<%
String output = "";
if ( Boolean.valueOf(request.getParameter("submitted")) == true ) {
    Scraping h = new Scraping();
    output  = h.googlePlus();
}
%>

You can assign the value of the variable using a assignment operator to some hidden JSP field and then use it in the JS using document.getElementById(). the code would be somewhat like: <input type="hidden" value="<%=output%>"> Or alternatively if your js is residing inside the JSP only var s = "<%=output%>"; should work! Cheers!

You can't access javascript variables via JSP Java Code.

Your JSP & Java codes are compiled at server side.

And your javascript runs in a browser.

So send the 'val' variable to your servlet by form submit, ajax or whatever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM