add user

[nobr]Hi,

help me with this please

I am working in this jsp-cookie issue,

and have this two .jsps: "1.jsp" and "2.jsp"

the first "1.jsp" looks like this(snippet):

It asks the user for his username, and it add it to a cookie

<form action="1.jsp" method="post" >

Username:

<br>

<input type="text" name="value">

<br><br>

<input type="submit" value="Send">

<%

if(request.getParameter("value") !=null){

String rt = request.getParameter("value");

Cookie cookie1 =new Cookie("message", rt);

cookie1.setMaxAge(24 * 60 * 60);

response.addCookie(cookie1);

}

%>

</form>

<A HREF="2.jsp"/>next</A>

the "2.jsp", basically makes a request, and if there is an available cookie, it prints it.

snippet of 2.jsp:

<%

Cookie[] cookies = request.getCookies();

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

{

if (cookies[loopIndex].getName().equals("message")){

out.println("The user is: " +

cookies[loopIndex].getValue());

}

}

%>

what i am trying to achieve is to add another user (with the cookies), when the username is different; for example:

a user goes to the 1.jsp, enter his name"jack", then goes to the 2.jsp, and sees it displayed.

then another user goes to the 1.jsp, enter his name"marc", and sees it displayed in the 2.jsp.

so, the two names will be in two diferent cookies, and they will be "recorded"; and everytime a new user comes, the same will happen.

how can i do this?

thanks for any response...[/nobr]

[2386 byte] By [deroka] at [2007-11-27 6:11:46]
# 1
Cannot you add the users in the same string with some seperator and when reading use tokenizer.
skp71a at 2007-7-12 17:18:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> Cannot you add the users in the same string with some

> seperator and when reading use tokenizer.

what kind of separator?

do you have a code example?

I've been trying to do it with this other way:

there is an "if" condition to ask if the cookie1 exists, if it's true then adds the input to another cookie and tryes to read it in the "2.jsp"-

but in the 2.jsp,

its not printing diferent values/usernames,like I wanted,

its printing the same value,

how can i fix this?

<%

Cookie cookie1 = null;

if(request.getParameter("valor") != null){

String rt = request.getParameter("value");

cookie1 = new Cookie("message", rt);

cookie1.setMaxAge(24 * 60 * 60);

response.addCookie(cookie1);

}

if(cookie1 != null){

String rtq = request.getParameter("value");

Cookie cookie2 = new Cookie("message2", rtq);

cookie2.setMaxAge(24 * 60 * 60);

response.addCookie(cookie2);

}

%>

in the 2.jsp it reads:

Cookie[] cookies2 = request.getCookies();

for(int loopIndex = 0; loopIndex < cookies2.length; loopIndex++)

{

if (cookies2[loopIndex].getName().equals("message2")) {

out.println("The user is: " +

cookies2[loopIndex].getValue());

}

}

Message was edited by:

derok

deroka at 2007-7-12 17:18:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
I meant append with - or | and later split it. I have never done this. But I would give a try.
skp71a at 2007-7-12 17:18:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
> I meant append with - or | and later split it. I> have never done this. But I would give a try.but how would you do it?StringBuffer appendto what?how would you do it with code?
deroka at 2007-7-12 17:18:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
response.addCookie(cookie1 + '|' + cookie2);then when reading, var cookies2 = request.getCookies(); var temp1 = new Array(); temp1 = cookies2.split('|');now loop temp1:temp1[0] is cookie1temp1[1] is cookie2 then your if
skp71a at 2007-7-12 17:18:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

> response.addCookie(cookie1 + '|' + cookie2);

>

> then when reading,

> var cookies2 = request.getCookies();

>

> var temp1 = new Array();

> temp1 = cookies2.split('|');

> ow loop temp1:

>

> temp1[0] is cookie1

> temp1[1] is cookie2

>then your if condition:

I get this error:

operator + cannot be applied to javax.servlet.http.Cookie,char

response.addCookie(cookie1 + '|' + cookie2);

^

1 error

is there another way to solve this issue?

Message was edited by:

derok

deroka at 2007-7-12 17:18:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Use double quote instead of single quote. Whatever I sent is js code.
skp71a at 2007-7-12 17:18:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
> Use double quote instead of single quote.now i get:addCookie(javax.servlet.http.Cookie) in javax.servlet.http.HttpServletResponse cannot be applied to (java.lang.String)response.addCookie(cookie1 + "|" + cookie2);^1 error
deroka at 2007-7-12 17:18:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
please somebody help me with this problem.
deroka at 2007-7-12 17:18:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...