problem with getting cookies
here r 2 JSPs, the login.jsp gets params and after validating them, it adds the param into cookies and forwards 2 home.jsp, in home.jsp I want 2 get the cookie that was added in login.jsp, but when 1st time home.jsp is loaded, there iz no such cookie... so, can n.e body tell me what iz the problem with this code?
login.jsp
=========
<%!
String uname,pwd;
Cookie userCookie;
%>
<%
uname=request.getParameter("uname");
pwd=request.getParameter("pwd");
if(uname.equals("hello") && pwd.equals("world"))
{
userCookie=new Cookie("user",uname);
response.addCookie(userCookie);
%>
<jsp:forward page="home.jsp"/>
<%
}
else
{
out.println("<h3>Invalid user</h3>");
}
%>
home.jsp
========
<%!
Cookie[] cookies;
Cookie userCookie;
boolean found;
%>
<%
cookies=request.getCookies();
found=false;
for(int i=0;i<cookies.length;i++)
{
userCookie=cookies[i];
if(userCookie.getValue().equals("hello"))
{
found=true;
break;
}
if(found==true)
{
out.println("<h1> Hello World </h1>");
}
else
{
out.println("<h1> Sorry </h1>");
}
}
%>
|