简体   繁体   English

将值从.jsp传递到新页面.jsp

[英]Pass Value from .jsp to new page .jsp

I want to ask you , how to pass variable, there's the example: 我想问你,如何传递变量,有个例子:

I have variable String X in pageone.jsp then i open a new tab page to pagetwo.jsp (using JavaScript window.open) , Is it possible to use the variable X (from pageone.jsp) in pagetwo.jsp ? 我在pageone.jsp中有变量String X,然后我打开了一个新的标签页到pagetwo.jsp(使用JavaScript window.open),是否有可能在pagetwo.jsp中使用变量X(来自pageone.jsp)? Please Explain, thank you verymuch! 请说明,非常感谢!

You should store the variable name and value in a cookie. 您应该将变量名称和值存储在Cookie中。
And fetch the cookie value in the pagetwo.jsp. 并在pagetwo.jsp中获取cookie值。

OR 要么

You can store the variable value in session. 您可以将变量值存储在会话中。

Just put the data in the URI, probably as a query string. 只需将数据放在URI中,可能作为查询字符串。 Make sure you encodeURIComponent it to make the data safe for URIs. 确保对它进行了encodeURIComponent ,以确保数据对于URI而言是安全的。

It sounds like the data starts out life in Java, so you'll need to include it in the page you send to the client for pageone. 听起来数据似乎是从Java开始使用的,所以您需要将其包含在发送给客户端进行分页的页面中。 This is probably done most simply by setting up a data structure, encoding it using JSON , and then assigning it to a variable in a <script> element. 可能最简单的方法是设置数据结构,使用JSON对其进行编码,然后将其分配给<script>元素中的变量。

Set the varaible into the session in Page1.jsp and access the same in Page2.jsp. Page1.jsp中的会话中设置变量,并在Page2.jsp中访问该变量。

In Page1.jsp 在Page1.jsp中

session.setAttribute("X", "value");

In Page2.jsp 在Page2.jsp中

String x = session.getAttribute("X");

use request.setAttribute("attribute_x", x); 使用request.setAttribute("attribute_x", x); to store the value of x in request in page1.jsp and then use request.getAttribute("attribute_x").toString(); 将x的值存储在page1.jsp中的请求中,然后使用request.getAttribute("attribute_x").toString(); in the page2.jsp to retrieve the value. page2.jsp中检索值。

You can also use session for this purpose ( session.setAttribute() and session.getAttribute() ) 您也可以为此使用sessionsession.setAttribute()session.getAttribute()

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

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