将Cookie存储在Java servlet中 [英] Storing Cookies in a Java servlet

查看:50
本文介绍了将Cookie存储在Java servlet中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在更新Java servlet来存储cookie。该类继承自HttpServlet并覆盖service。方法。在这个方法中,我有以下代码来测试cookie存储和retreival:

public void服务(HttpServletRequest req,HttpServletResponse res)抛出IOException

{

......

Cookie [] cookies = req.getCookies();

if(cookies!= null)

{

for(int i = 0; i< cookies.length; i ++)

{

System.out。 println(cookies [i] .getName());

}

}

Cookie c = new Cookie(" Acceptance",") ; y");

res.addCookie(c);


但我得到的cookies数组总是为空。它似乎永远不会存储接受。曲奇饼。任何人都可以帮忙吗?

Hi,

I am updating a Java servlet to store cookies. The class inherits from HttpServlet and overrides the "service" method. Within this method I have the following code to test cookie storing and retreival:

public void service( HttpServletRequest req, HttpServletResponse res ) throws IOException
{
......
Cookie[] cookies = req.getCookies();
if( cookies != null )
{
for( int i = 0; i < cookies.length; i++ )
{
System.out.println(cookies[i].getName());
}
}
Cookie c = new Cookie("Acceptance","y");
res.addCookie( c );


But the cookies array I get back is always null. It never seems to store the "Acceptance" cookie. Can anybody help?

推荐答案

java servlet是从UNIX平台运行的。我正在访问从NT机器引用servlet的页面。该页面包含一个指向cgi脚本的链接,cgi-script将一个HTTP请求发送到Java servlet。


此外,当我运行getDomain()和getPath()时我在上面的代码中创建的cookie我只是为这两个得到null。但是使用setDomain和setPath来纠正这个似乎无济于事。
The java servlet is being run from a UNIX platform. And I''m accessing the page that references the servlet from an NT machine. The page contains a link to a cgi-script and the cgi-script sends an HTTP request to the Java servlet.

Further, when I run getDomain() and getPath() on the cookie I create in the above code I just get null for the two. But using setDomain and setPath to correct this doesn''t seem to help.



java servlet正在从UNIX平台运行。我正在访问从NT机器引用servlet的页面。该页面包含一个指向cgi脚本的链接,cgi-script将一个HTTP请求发送到Java servlet。


此外,当我运行getDomain()和getPath()时我在上面的代码中创建的cookie我只是为这两个得到null。但是使用setDomain和setPath来纠正这个似乎并没有帮助。
The java servlet is being run from a UNIX platform. And I''m accessing the page that references the servlet from an NT machine. The page contains a link to a cgi-script and the cgi-script sends an HTTP request to the Java servlet.

Further, when I run getDomain() and getPath() on the cookie I create in the above code I just get null for the two. But using setDomain and setPath to correct this doesn''t seem to help.



我无法看到你在这里想做什么。您是否阅读过如何使用cookie的教程?


您在创建cookie之前获取cookie!

I can''t reaaly see what you are trying to do here. Have you read a tutorial on how to use cookies?

You are getting the cookie before you have created it!


No。我这样做是为了第一次不能找到饼干。但是,当第二次进入该方法时,不应该找到cookie吗?目前似乎没有...而且我已经检查了我的临时互联网文件。文件夹,看它是否在那里,但它不是。
No. I have done it that way so that first time round it won''t find the cookie. However, when the method is entered the second time, shouldn''t it find the cookie then? It doesn''t seem to at the moment... And I''ve checked my "temporary internet files" folder to see if it''s there, but it isn''t.


这篇关于将Cookie存储在Java servlet中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆