java - JAVASCRIPT values to JSP variables - Stack Overflow

How can I pass the value of a variable from javascript to a java variable?<% String st = "<s

How can I pass the value of a variable from javascript to a java variable?

<% String st = "<script>document.writeln(selected)</script>";
 out.print("value = " + st);%>

This is my code for java getting the values from javascript variable, selected. But no value is displayed.

How can I pass the value of a variable from javascript to a java variable?

<% String st = "<script>document.writeln(selected)</script>";
 out.print("value = " + st);%>

This is my code for java getting the values from javascript variable, selected. But no value is displayed.

Share Improve this question edited Dec 26, 2012 at 9:48 Erwin Brandstetter 661k157 gold badges1.1k silver badges1.3k bronze badges asked Dec 26, 2012 at 8:38 T E MT E M 551 gold badge2 silver badges8 bronze badges 4
  • 1 Javascript executes on client side, Java executes on server side. You cannot pass values between these two directly. – Pradeep Simha Commented Dec 26, 2012 at 8:40
  • Hmm.. then how can i make them municate knowing that they are in different side of programming? :) – T E M Commented Dec 26, 2012 at 8:42
  • possible duplicate of javascript to jsp – Thilo Commented Dec 26, 2012 at 8:44
  • <% String st = "<script>document.writeln(selected)</script>"; out.print("value = " + st);%> this is my code for java getting the values from javascript variable, selected. but no value is displayed – T E M Commented Dec 26, 2012 at 8:49
Add a ment  | 

4 Answers 4

Reset to default 3

You have to make a request and send your variable from the browser (where Javascript lives) to the server (where the JSP lives) that way.

Ajax would work, or an HTML form.

In you JSP, you can then receive it as a request parameter (or as part of the request body, or as a request header).

Javascript runs on client. JSP runs on server. The only way to pass information from client to server in web environment is via HTTP parameters or HTTP headers during HTTP request or as a part of request body if method is POST or PUT.

So, you should create such request. It can be done using either changing of your document location or utilizing AJAX call.

you can pass parameter or make a hidden field inside your jsp code and using javascript assign value for this hidden field, then get parameter value in java code.

Use HTML forms.

On server side, you'll get the data in HTTPServletRequest parameter.

Check this too: Building my first Java Web Application

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744323974a4568557.html

相关推荐

  • java - JAVASCRIPT values to JSP variables - Stack Overflow

    How can I pass the value of a variable from javascript to a java variable?<% String st = "<s

    7天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信