Java 将变量从一个jsp发送到另一个jsp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18763168/
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
sending variable from one jsp to another jsp
提问by Mayur Gupta
I have one JSP file as jsp 1.jspand another JSP file as jsp 2.jsp
我有一个 JSP 文件作为jsp 1.jsp和另一个 JSP 文件作为jsp 2.jsp
I've included jsp 2.jspin jsp 1.jspusing <%@include file="jsp 2.jsp" %>
我已经包括JSP 2.jsp在JSP 1.jsp页面使用<%@include file="jsp 2.jsp" %>
Now I need a click event on some element. And on that event I want to transfer a string variable to included jsp.
现在我需要某个元素上的点击事件。在那个事件中,我想将一个字符串变量传输到包含的 jsp。
Lets say I have a list and on click of it I want to transfer the name of the list to another JSP,
假设我有一个列表,单击它我想将列表的名称转移到另一个 JSP,
And in another JSP I am trying to use that string to carry out some task.
在另一个 JSP 中,我试图使用该字符串来执行某些任务。
And I am doing all these without any servlet. challenging one!! I have google'd a lot, but didnt find anything.
我在没有任何 servlet 的情况下做所有这些。挑战一!!我已经谷歌了很多,但没有找到任何东西。
采纳答案by OldCurmudgeon
You have a number of options:
您有多种选择:
Store it in the session.
// Memorise any passed in user. String username = request.getParameter("username"); if (username != null && username.length() > 0) { session.setAttribute("username", username); }
Store it as a hidden field in the form.
<input name="username" type="hidden" value=""/>
Store it in a cookie.
username = getCookie(userCookieName); // Get from cookie. function getCookie(name) { if (document.cookie) { index = document.cookie.indexOf(name); if (index !== -1) { f = (document.cookie.indexOf("=", index) + 1); t = document.cookie.indexOf(";", index); if (t === -1) { t = document.cookie.length; } return(document.cookie.substring(f, t)); } } return (""); }
Persist it on the client side in sessionStorage. See herefor details.
sessionStorage.setItem("username", "...");
Not really another option but a mechanism - pass it in the URL:
.... onclick="window.location='details.jsp?username=...'
将其存储在会话中。
// Memorise any passed in user. String username = request.getParameter("username"); if (username != null && username.length() > 0) { session.setAttribute("username", username); }
将其存储为表单中的隐藏字段。
<input name="username" type="hidden" value=""/>
把它储存在饼干里。
username = getCookie(userCookieName); // Get from cookie. function getCookie(name) { if (document.cookie) { index = document.cookie.indexOf(name); if (index !== -1) { f = (document.cookie.indexOf("=", index) + 1); t = document.cookie.indexOf(";", index); if (t === -1) { t = document.cookie.length; } return(document.cookie.substring(f, t)); } } return (""); }
将其保留在 sessionStorage 中的客户端。有关详细信息,请参见此处。
sessionStorage.setItem("username", "...");
不是真正的另一种选择,而是一种机制 - 在 URL 中传递它:
.... onclick="window.location='details.jsp?username=...'
回答by Sajan Chandran
If the reason for including your jsp 2
in jsp 1
is for sending variable, then its not needed
.
you just need to set a hidden
variable in jsp 1
, and in your jsp 2
you should able to access it, by using the jsp implicit request
object.
如果包括你的原因jsp 2
在jsp 1
用于发送变量,那么它的not needed
。您只需要在 中设置一个hidden
变量jsp 1
,并且jsp 2
您应该能够通过使用该jsp implicit request
对象来访问它。
回答by Sajan Chandran
in jsp1 use this way to pass value.
在jsp1中使用这种方式传递值。
<form action="">
<input type="hidden" name="hidden" value="hidden">
<input type="submit" value="submit"></form>
in jsp2 to get value
在jsp2中获取值
String value=request.getParameter("hidden");
回答by MaheshVarma
Try to use Ajax with JQuery
to achieve your goal or you can simply store the String object in session
and you can delete it in jsp2.jsp
when ever you use it. or you can simply append to a url
as query string
there are multiple ways among them the above are few.
尝试使用Ajax with JQuery
来实现您的目标,或者您可以简单地将 String 对象存储在其中,session
并且您可以在jsp2.jsp
使用它时将其删除。或者您可以简单地附加到 a ,url
因为query string
其中有多种方法,上面很少。
These questions may help you
这些问题可能对你有帮助
回答by aUserHimself
You can set a jsp:param
, as in this example jsp:param elementor take a look at this similar post Passing parameters between JSP pages.
您可以设置一个jsp:param
,如本示例中的jsp:param 元素,或者查看类似的文章Passing parameters between JSP pages。