gpt4 book ai didi

java - getCookies() 获取 cookie 和 session

转载 作者:行者123 更新时间:2023-11-30 03:32:49 28 4
gpt4 key购买 nike

我有两个 servlet,另一个 servlet 应该从 cookie 和 session 中输出信息(我们教授的说明)。问题是,我尝试使用 getCookies() 从请求对象获取 cookie,但它返回两个对象,第一个对象的名称为“JSESSIONID”及其随附的十六进制代码,第二个对象包含我创建的 cookie。为什么会发生这种情况? getCookie() 如何单独返回 cookie?

代码如下:

Servlet 1:

package com.telco.process;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class ConvertServlet
*/
@WebServlet("/Convert")
public class ConvertServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String num;
private String telco;
private HttpSession session;
private RequestDispatcher rd;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String convertedNum = "";
session = request.getSession();
num = request.getParameter("number");
telco = request.getParameter("telco");
rd = request.getRequestDispatcher("Display");

Cookie cTelco = new Cookie("telco", telco);
if (!(num.equals(null)) || !(num.equals("")))
{
for (byte i = 0; i < num.length(); i++)
{
switch(num.charAt(i))
{
case 'A':
case 'B':
case 'C': convertedNum += "2"; break;
case 'D':
case 'E':
case 'F': convertedNum += "3"; break;
case 'G':
case 'H':
case 'I': convertedNum += "4"; break;
case 'J':
case 'K':
case 'L': convertedNum += "5"; break;
case 'M':
case 'N':
case 'O': convertedNum += "6"; break;
case 'P':
case 'Q':
case 'R':
case 'S': convertedNum += "7"; break;
case 'T':
case 'U':
case 'V': convertedNum += "8"; break;
case 'W':
case 'X':
case 'Y':
case 'Z': convertedNum += "9"; break;
}
}
session.setAttribute("number", convertedNum);
response.addCookie(cTelco);
}
rd.forward(request, response);
}

}

Servlet 2:

package com.telco.process;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/Display")
public class DisplayNumber extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
Cookie ck[] = request.getCookies();
System.out.println(ck[0].getValue());
pw.println("The converted number is " + "-" + request.getSession(false).getAttribute("number"));
}
}

最佳答案

JSESSIONID确实是一个cookie,它用于标识 session 。在服务器上创建一个带有 ID 的 session 。服务器将带有 ID 的 cookie (JSESSIONID) 发送到浏览器。然后,浏览器在每次请求时都会将此 JSESSIONID cookie 发送到服务器,服务器使用它来将请求映射到 session 。

编辑:查找 cookie 的一些示例代码:

您应该仅通过名称访问您的 cookie,而不是通过索引。这是一个示例帮助例程,您可以像这样使用它(当然,在实际程序中检查 null):

System.out.println(getCookieByName(request, "telco").getValue());


/**
* Find a specific HTTP cookie in a request.
*
* @param request
* The HTTP request object.
* @param name
* The cookie name to look for.
* @return The cookie, or <code>null</code> if not found.
*/
protected Cookie getCookieByName(HttpServletRequest request, String name) {
if (request.getCookies() == null) {
return null;
}
for (int i = 0; i < request.getCookies().length; i++) {
if (request.getCookies()[i].getName().equals(name)) {
return request.getCookies()[i];
}
}
return null;
}

关于java - getCookies() 获取 cookie 和 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28632077/

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