gpt4 book ai didi

java - 为什么session的3个参数中有1个为null(Java),但在servlet中不为null

转载 作者:行者123 更新时间:2023-12-01 22:43:18 28 4
gpt4 key购买 nike

JSP中的用户名为null,为什么?在 servlet 中我没有 null 参数。 namefamiliya 运行良好,只有 usernamenull

Servlet:

HttpSession session = request.getSession();
session.setMaxInactiveInterval(30);
session.setAttribute(username, uservalue);
session.setAttribute(name, namevalue);
session.setAttribute(familiya, familiyavalue);
PrintWriter out = response.getWriter();
out.println(uservalue); ==== Is NOT null

JSP:

<%
HttpSession ses = request.getSession();
String name =(String)ses.getAttribute("name");
String familiya =(String)ses.getAttribute("familiya");
String username = (String)ses.getAttribute("username");
//out.println(name); out.println(familiya);
out.println(username);
%>
<a href="http://localhost:8080/siteg/index?user=<%=username%>"><%=name %> <%=familiya %> </a>

最佳答案

您可能应该使用 setAttribute 和常量名称,如下所示:

session.setAttribute("username", uservalue);
session.setAttribute("name", namevalue);
session.setAttribute("familiya", familiyavalue);

与您检索它们相同!

关于java - 为什么session的3个参数中有1个为null(Java),但在servlet中不为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25774095/

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