gpt4 book ai didi

java - 使用 cookie 传递值

转载 作者:太空宇宙 更新时间:2023-11-04 14:59:06 24 4
gpt4 key购买 nike

在我的 servlet.java 中我声明了一个 cookie,

String loc = "Some random data";
Cookie thecookie = new Cookie("thecookie", loc);
response.addCookie(thecookie);

在执行一些文件上传操作后,我再次为此 cookie 设置一个值,

fileTxt = FileUtils.readFileToString(uploadedFile);
thecookie.setValue(fileTxt);

RequestDispatcher rd = request.getRequestDispatcher("thejsppage.jsp");
rd.forward(request, response);

现在在我的 jsp 页面中,如上面重定向的那样,

<%
Cookie[] my = request.getCookies();
for(int i=0;i<my.length;i++){
String filetext = my[i].getValue().toString();
}
%>

<div id="editor"><%=filetext %></div>

但是这返回了一个垃圾值,我的错误是什么?

如何使用 cookie 传递值?

我不想使用 request.setAttribute("name", value); 方法。

最佳答案

根据我的上述问题,

我变了

<%
Cookie[] my = request.getCookies();
for(int i=0;i<my.length;i++){
String filetext = my[i].getValue().toString();
}
%>

<div id="editor"><%=filetext %></div>

<%
Cookie[] my = request.getCookies();
for(int i=0;i<my.length;i++){
if(my.getName()=="thecookie"){
String filetext = my[i].getValue().toString();
}
}
%>

<div id="editor"><%=filetext %></div>

现在我可以获得所需的值。

关于java - 使用 cookie 传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22828555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com