servlet小问题,高手指教,多谢!

kanghl 2004-12-17 08:15:42
下面是我的程序,我认为应该在第二次调用页面时出现第一次调用时写的cookie,但总是在第三次出现,也就是隔一次,为什么?
说明:我用的是tomcat5.0.27,jdk1.5.0


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class ServletSession1 extends HttpServlet
{
int i=0;
public void service(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
i++;
res.addCookie(new Cookie("cookie"+i,""+(i*i)));
PrintWriter out=res.getWriter();
out.println("<html><head><title>Test the Cookie of the servlet</title></head>");
out.println("<body><center><h1>Test result"+i+"</h1>");
Cookie[] c=req.getCookies();
if(c!=null)
{
for(int j=0;j<c.length-1;j++)
{
out.println("Cookie:"+c[j].getName()+"="+c[j].getValue());
}
}
out.println("</center>");
out.println("</body></html>");
out.close();
}
}

...全文
81 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kanghl 2004-12-17
  • 打赏
  • 举报
回复
自己解决了,帖子无法删除,给分吧。
bonniewater 2004-12-17
  • 打赏
  • 举报
回复
mark

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧