File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes cookie problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "cookie problem" Watch "cookie problem" New topic
Author

cookie problem

deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
i have created cookie in an jsp page like this


String s3=request.getParameter("user");
Cookie ck2=new Cookie("cookiee",s3);
response.addCookie(ck2);

when i display the cookies it only displays the latest value of s3
while i want all values
how can i do that?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41885
    
  63
How are you displaying cookies? You're aware that those will only be available after a round trip to the browser and back to the server, right?


Ping & DNS - my free Android networking tools app
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
means
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41885
    
  63
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61229
    
  66

amrita singhal wrote:means

Try putting as much effort into your posts as you would like to see in the responses.

Your habit of making single-word and incomplete-sentence posts does not make people want to spend their volunteer time to help you if you won't even take the time to form complete sentences.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(On a cultural note, that's a reasonable response in Hindi, but comes across a little weird in English.)

A cookie can only have a single value, and as mentioned, needs a round-trip.

If you're trying to keep user-specific objects, keep them in the session.

I'd really recommend getting familiar with how HTTP, web applications, etc. work before trying to go much further, and Bear is correct-it's a good idea to really make sure your messages are as clear and concise as possible, but not so concise that they sound dismissive, or trivialize the amount of time people are putting in to helping you. Thanks!
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
i want that whenever anyone signup his user name is saved in cookie that's why i used this in user_regis.jsp page
Cookie ck2=new Cookie("cookiee",s3);
response.addCookie(ck2);

now when i get the value from cookie it does not give all values of users that are signed up
this is the problem'
can anyone give me the solution?
thankx in advance
Nilesh Miskin
Ranch Hand

Joined: Jun 17, 2010
Posts: 44
I think a good look at the javax.servlet.http.Cookie & javax.servlet.http.HttpServletRequest APIs, coupled with a simple tutorial will do the trick.


Nilesh Miskin
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Cookies are local to a single machine--if you want to store the names of all people that are signed up you'd need to collect them on the server side during the login process. And again, I strongly recommend taking a step back and trying to understand the nature of Java web applications before proceeding much further--it will save you untold headaches and frustration.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
i know that cookie is stored on local machine saved in browser
i want to create it for change password coding
if anyone use my username and get password from change password link then he will be able to see my profile
i want to remove it
so i create coookie
when person gets login with my username/password at another machine ,then he would be not eligible
because cookies are available only my pc.
so can anyone solve me this problem
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

And what I'm saying is that you'll need to gather the currently logged in users on the server side during the login process.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
ya it show currently logged user as cookie value
then what should i do?
because i want to apply change password concept with security
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Maybe try listing specifically the steps you think you'd need to take in order to implement this--think through the problem, post your potential solutions, we can take it from there. You already know that you want a cookie (probably set on login and removed on logout) and that you need to track those on the server side.

How might you implement that?
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
should i not use cookies in change password concept
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're very frustrating sometimes. Did you consider doing what I asked?
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
but i posted already what my problem is?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

David Newton wrote:Maybe try listing specifically the steps you think you'd need to take in order to implement this--think through the problem, post your potential solutions, we can take it from there. You already know that you want a cookie (probably set on login and removed on logout) and that you need to track those on the server side.

How might you implement that?

But you didn't do what I asked, which is to write down the exact steps, in English (NOT code), that you'd need to take in order to solve this problem.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
1. i create sign up page in which form action is regis.jsp
2. in regis.jsp database insertion takes place
3. in regis.jsp i also create cookie when i store that people user name
4. in login.jsp when person click on change password link it must be cheked that the people user name is available in cookies or not
5. if not means that people has fake login

problem is that all people that are signed up has username values and that values must be stored in cookies
while it stores only latest person usermname
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Right, so you need to gather the user names on the server side during the login process so you know who's currently logged in.

There's a bit more to it than that, but one thing at a time--so far you still don't seem to believe me.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
now please tell me what should i do for getting names of all users that signed up during login process
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Gather them during the login process. Do you know what a list or map or any other collection is? Do you know how to add elements to a collection?
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
i have used List to add items in session

now i want to use list with cookies
can you give me the code or link
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61229
    
  66

Please read the following FAQ articles: NotACodeMill, ShowSomeEffort, DoYourOwnHomework.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
but these links donot have cookies information
how can it will help me?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The links were provided in an attempt to explain to you how JavaRanch works, and why you're getting the answers you are instead of someone here doing your work for you. So no, they're not directly related to your question--but they *are* related to the interactions between us (the JavaRanch "staff") and you.

The next step in solving your problem is deciding how to use that list of currently logged in users. (You'll also need to remove users from that list if they log out or if that session expires.)

Since you're specifically trying to exclude the same user ID from logging in from a different *machine* at the same time they're already logged in, your problem is actually more complicated than you might have thought at first--what if I try to log in using a different browser on the *same* machine? We'll cross that bridge later. (It's also not clear how you'll handle a case when the user has cookies turned off--also a task for another time.)

So when a user logs in, you'll give them a cookie, and mark them as being logged in by storing their user ID in the "logged in list". What's next?
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
List users=new ArrayList();
Cookie passwd=new Cookie("user",users);
response.addCookie(passwd);

this is written in login.jsp page
now when we add username in cookie i write this
users.add(s3); //in s3 there is username
is it correct way?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Cookies can only store string values. That aside, the list of logged in users needs to be stored on the server side. Cookies are client side.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
i prevent my profile accessed by fake user that's why i want to use cookies
is there any other method for security
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Let me ask you a few questions:

1) What's your definition of a cookie?
2) Where does cookie data reside?
3) What kind of data can a cookie hold?

Here's the solution I think you're trying to use:

1   User logs in
    1.1   Store user name on server side
    1.2   Give user a cookie saying they're logged in
2   Another user logs in
    2.1   Is that user already logged in?
    2.2   If yes, signal error
    2.3   If no, continue normally
3   User logs out
    3.1   Remove their name from the list of logged in users

Is that correct, more or less? If so, which specific part are you having a problem with? If not, tell me what your ideas are, and we'll go from there.

(Noting that there are still a couple of issues with this solution that are probably not worth solving, given the nature of web applications and how HTTP works.)
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
difficulty is at-store user name at server side
is there separate cookies given to each user OR a single cookie store all users
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The users will not be stored in a cookie. The users will be stored *SERVER* side. Cookies are *CLIENT* side.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
how can we store user at server side
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Keeping a list in the application context is probably the easiest solution. You could also persist currently-logged-in info to a database, but that might be overkill--I have no idea what your capacity requirements are.
 
jQuery in Action, 2nd edition
 
subject: cookie problem