gpt4 book ai didi

java - session 参数始终为空

转载 作者:行者123 更新时间:2023-12-01 06:21:58 27 4
gpt4 key购买 nike

我试图将字符串从一个 servlet 传递到另一个 servlet,但我总是在第二个 servlet 中得到空值。我已经用我的头来对抗这个问题近两周了,但毫无结果,我已经没有任何想法了。我已阅读此处发布的所有类似问题,但仍然得到空值。

这是第一个 servlet

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
HttpSession session = request.getSession();
String name = (String) session.getAttribute("name");
int userId = DBConnect.getIdByName(name);
int lobbyId = DBConnect.insertInLobby(userId);
request.setAttribute("lobbyId", lobbyId);
request.getRequestDispatcher("PlatformPairsLobby.jsp").forward(request, response);
}
catch (ServletException | IOException e)
{
logger.error("Error " + e);
}
}

这是第二个 servlet,其名称字符串始终为 null。

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
HttpSession session = request.getSession();
String name = (String) session.getAttribute("name");
int id = DBConnect.getIdByName(name);
request.setAttribute("id", id);
String lobbyId = request.getParameter("lobbyId");
request.setAttribute("lobbyId", lobbyId);
response.setContentType("application/x-java-jnlp-file");
try
{
request.getRequestDispatcher("PairsJnlp.jsp").include(request, response);
}
catch (ServletException | IOException e)
{
logger.error("Error " + e);
}
}

为了访问第二个 servlet,我在该 JSP 中使用了一个按钮

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.awt.List"%>
<%@page import="java.util.*"%>
<%@page import="database.DBConnect"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<html>
<head>
<%
ArrayList<String> list = (ArrayList<String>)
request.getAttribute("lobbyList");
Integer lobbyId = (Integer) request.getAttribute("lobbyId");
%>

------------------

window.location.href =
"http://192.168.100.154:8080/GamePlatformCore/SecondServlet?lobbyId=<%=lobbyId%>";

名称在不同的 servlet 中设置。

request.setAttribute("verificare", verificare);
HttpSession session = request.getSession();
session.setAttribute("name", name);

if(request.getServletContext().getAttribute("userMessages")==null)
{
request.getServletContext().setAttribute("userMessages",new ArrayList<String>());
}

request.getRequestDispatcher(WELCOME_PAGE).forward(request, response);

最佳答案

您正在使用请求范围属性来获取字符串值,请使用以下代码来获取 session 值:

<% String lobbyId = (String)request.getSession().getAttribute(lobbyId); %>

关于java - session 参数始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52811726/

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