简体   繁体   English

我如何在会话中使用javascript变量以在另一个jsp页面中检索?

[英]How can i strore javascript variables in session to retrieve in another jsp page?

This is my javaScript code where i'm getting student name and saving in session while retrieving in another jsp page its showing null? 这是我的javaScript代码,在其中获取学生姓名并在会话中保存,同时在另一个jsp页面中检索其显示为null? ' var studentName = document.getElementById("studentName").value; '<%Session["studentName"] = "' + studentName + '"; %>'; ' var studentName = document.getElementById("studentName").value; '<%Session["studentName"] = "' + studentName + '"; %>'; var studentName = document.getElementById("studentName").value; '<%Session["studentName"] = "' + studentName + '"; %>'; ' '

and i'm forwarding that page to next.jsp page as window.location.href="next.jsp" and i'm calling those values as ' String studentName = request.getParameter("studentName"); out.println(studentName); 我将该页面作为window.location.href =“ next.jsp”转发到next.jsp页面,并将这些值称为' String studentName = request.getParameter("studentName"); out.println(studentName); String studentName = request.getParameter("studentName"); out.println(studentName); ' '

in the javascript im getting correct value,and in next.jsp page its printing null value,what's mistake i'm doing? 在javascript im中获取正确的值,并在next.jsp页中显示为null的值,这是什么错误?

You have set a session attribute in the first page and your are trying to get its value as if you are getting it from a submitted form. 您已经在第一页中设置了会话属性,并且您试图像从提交的表单中获取它那样获取它的值。

While setting session in the previous page, use 在上一页中设置会话时,请使用

session.setAttribute("session_attributename", studentName)

While getting the value in next page, use 在获取下一页的值时,使用

session.getAttribute("session_attributename");

If you submit a form, then only you can use request.getParameter("....") to obtain the value in next page. 如果提交表单,则只有您可以使用request.getParameter("....")在下一页中获取值。

String studentName = request.getParameter("studentName");
out.println(studentName);
var studentName = document.getElementById("studentName").value;
window.location.href="next.jsp?studentName="+studentName;

in next.jsp 在next.jsp中

String studentName = request.getParameter("studentName");
out.println(studentName);

In this page you can store the name in session and once stored you can access the session value anywhere by using the session attribute name. 在此页面中,您可以将名称存储在会话中,并且一旦存储,就可以使用会话属性名称在任何位置访问会话值。

session.setAttribute("sname", studentName); (in next.jsp)

To get the Student name (in any page) 获取学生姓名(在任何页面中)

String studentName = (String)session.getAttribute("sname");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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