You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by XiaoPeng <xi...@gmail.com> on 2005/04/25 05:30:55 UTC

tomcat jsp problem with cookies

Dear Sir/Madam,

This is XiaoPeng here. I am using Tomcat 5.5.8 and JDK 1.5 to develope a project
now. The problem is the cookie is not stable. I had tried many times,
and tried to use session insead of cookie, but seems it doesn't work.
The cookie code is showing below,
------------------------------set cookie-------------------------
String cookieName="userlogin";
 Cookie userCookie = new Cookie("user", uname1);
 userCookie.setMaxAge(-1);
 userCookie.setPath("/");
 response.addCookie(userCookie);

------------------------------get cookie--------------------------
Cookie cookies[]=request.getCookies();
Cookie uCookie=null;
String username=null;
String uservalue=null;
if(cookies==null){
%><jsp:forward page="error.htm"/><%
}else{
       uCookie=cookies[0];
   username=sCookie.getName();
       uservalue = sCookie.getValue();
}
--------------------------------------------------------------------------

Is there any error in my code? Or there is any way to wok?

Thank you very much!
Hope to hear from you soon!

Regards
XiaoPeng

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: tomcat jsp problem with cookies

Posted by Darek <dc...@infinitesource.ca>.
I think you have an error in your code.
Cookie cookies[]=request.getCookies();
...
   uCookie=cookies[0];
   username=sCookie.getName();
is it sCookie or uCookie?


DarekC


On Mon, 25 Apr 2005 11:30:55 +0800
 XiaoPeng <xi...@gmail.com> wrote:
>Dear Sir/Madam,
>
>This is XiaoPeng here. I am using Tomcat 5.5.8 and JDK 1.5
>to develope a project
>now. The problem is the cookie is not stable. I had tried
>many times,
>and tried to use session insead of cookie, but seems it
>doesn't work.
>The cookie code is showing below,
>------------------------------set
>cookie-------------------------
>String cookieName="userlogin";
> Cookie userCookie = new Cookie("user", uname1);
> userCookie.setMaxAge(-1);
> userCookie.setPath("/");
> response.addCookie(userCookie);
>
>------------------------------get
>cookie--------------------------
>Cookie cookies[]=request.getCookies();
>Cookie uCookie=null;
>String username=null;
>String uservalue=null;
>if(cookies==null){
>%><jsp:forward page="error.htm"/><%
>}else{
>       uCookie=cookies[0];
>   username=sCookie.getName();
>       uservalue = sCookie.getValue();
>}
>--------------------------------------------------------------------------
>
>Is there any error in my code? Or there is any way to wok?
>
>Thank you very much!
>Hope to hear from you soon!
>
>Regards
>XiaoPeng
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail:
>tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail:
>tomcat-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org