使用javascript设置会话属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18032185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set session attribute using javascript
提问by Bishan
I'm using jqueryui autocomplete comboboxin my jsppage. I need to set selected value of combo-box to the HttpSession
.
我在我的jsp页面中使用jqueryui 自动完成组合框。我需要将组合框的选定值设置为.HttpSession
I tried as below.
我试过如下。
this._on(this.input, {
autocompleteselect: function (event, ui) {
// alert(ui.item.value);
var value = ui.item.value;
<% session.setAttribute("comboboxvalue",value); %>
ui.item.option.selected = true;
this._trigger("select", event, {
item: ui.item.option
});
}
Problem with this way is that code don't recognize value
param.
这种方式的问题是代码无法识别value
参数。
How can I solve this and set session attribute using javascript
?
如何解决此问题并使用 设置会话属性javascript
?
采纳答案by Bishan
Here is how I achieved my task successfully.
这是我成功完成任务的方式。
I have created a new servlet and trigger an AJAX call as below. Then I set comboboxvalue
to session from servlet.
我创建了一个新的 servlet 并触发了一个 AJAX 调用,如下所示。然后我comboboxvalue
从 servlet设置为会话。
this._on( this.input, {
autocompleteselect: function( event, ui ) {
$.ajax({
type: 'POST',
url: 'AjaxServlet',
dataType : 'json',
data: { comboboxvalue : ui.item.value }
});
ui.item.option.selected = true;
this._trigger( "select", event, {
item: ui.item.option
});
},
autocompletechange: "_removeIfInvalid"
});
回答by Vikas V
Java script is a client side technology. Its not possible to set any session variables from Java script.
Java 脚本是一种客户端技术。无法从 Java 脚本设置任何会话变量。
You can do this using Ajax. Through Ajax you have to send a request to the server asynchronously and then add the data to the session from within the servlet.
您可以使用 Ajax 执行此操作。通过 Ajax,您必须异步地向服务器发送请求,然后从 servlet 内将数据添加到会话中。
回答by Suresh Atta
You might misunderstand that jsp
and javascript
existed on same file. Yes but JSP
part compiles on server side itself comes to client
.
你可能会误会jsp
,并javascript
在同一个文件存在。是的,但JSP
部分在服务器端编译本身就变成了client
.
The code inbetween <% %>
executes on serverside.
中间的代码<% %>
在服务器端执行。
You can't do that with Javascript
.
你不能用Javascript
.
You need to make a server request(There are forms,Ajax,url..etc) for that.
您需要为此发出服务器请求(有表单、Ajax、url.. 等)。
回答by s_u_f
You cannot set session values in JavaScript.
您不能在 JavaScript 中设置会话值。
For your case, you can trigger an AJAX call for onChange event in your select box. You can simply send the select box value to server and place it in session.
对于您的情况,您可以在选择框中触发 onChange 事件的 AJAX 调用。您可以简单地将选择框值发送到服务器并将其放入会话中。
回答by admin
you can use javaScript operating form to post args to a servlet class, and setSession in this servlet class.
您可以使用javaScript 操作形式将args 发布到servlet 类中,并在该servlet 类中设置会话。