473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Storing Cookies in a Java servlet

Hi,

I am updating a Java servlet to store cookies. The class inherits from HttpServlet and overrides the "service" method. Within this method I have the following code to test cookie storing and retreival:

public void service( HttpServletRequest req, HttpServletResponse res ) throws IOException
{
......
Cookie[] cookies = req.getCookies();
if( cookies != null )
{
for( int i = 0; i < cookies.length; i++ )
{
System.out.println(cookies[i].getName());
}
}
Cookie c = new Cookie("Acceptance","y");
res.addCookie( c );


But the cookies array I get back is always null. It never seems to store the "Acceptance" cookie. Can anybody help?
Nov 8 '06 #1
10 8610
The java servlet is being run from a UNIX platform. And I'm accessing the page that references the servlet from an NT machine. The page contains a link to a cgi-script and the cgi-script sends an HTTP request to the Java servlet.

Further, when I run getDomain() and getPath() on the cookie I create in the above code I just get null for the two. But using setDomain and setPath to correct this doesn't seem to help.
Nov 8 '06 #2
r035198x
13,262 8TB
The java servlet is being run from a UNIX platform. And I'm accessing the page that references the servlet from an NT machine. The page contains a link to a cgi-script and the cgi-script sends an HTTP request to the Java servlet.

Further, when I run getDomain() and getPath() on the cookie I create in the above code I just get null for the two. But using setDomain and setPath to correct this doesn't seem to help.
I can't reaaly see what you are trying to do here. Have you read a tutorial on how to use cookies?

You are getting the cookie before you have created it!
Nov 9 '06 #3
No. I have done it that way so that first time round it won't find the cookie. However, when the method is entered the second time, shouldn't it find the cookie then? It doesn't seem to at the moment... And I've checked my "temporary internet files" folder to see if it's there, but it isn't.
Nov 9 '06 #4
Can't anybody help me? Why isn't it working?
Nov 10 '06 #5
I'm not going to relent. I really really really need help on this. Somebody must know!
Nov 13 '06 #6
r035198x
13,262 8TB
I'm not going to relent. I really really really need help on this. Somebody must know!
Expand|Select|Wrap|Line Numbers
  1. request.getCookies() 
gives you cookies sent from the clients. If you want this to contain anything, you set the cookies at the clients.

You are only sending a cookie from the server to the client in your code.
Nov 13 '06 #7
so request.getCookies() gets cookies from the client? Then how do I save cookies to the client if response.addCookie() saves it in the server. Can't I save cookies to the client from within the servlet. I would've hoped this would be possible as otherwise I can't see how useful the cookies API can be.
Nov 13 '06 #8
r035198x
13,262 8TB
so request.getCookies() gets cookies from the client? Then how do I save cookies to the client if response.addCookie() saves it in the server. Can't I save cookies to the client from within the servlet. I would've hoped this would be possible as otherwise I can't see how useful the cookies API can be.
When you do
Expand|Select|Wrap|Line Numbers
  1.  res.addCookie( c );
  2.  
in a servlet, you include the cookie for the clients. When you get to the client you can get this cookie and use it. From the client, when you then send a request to the server, a new request is generated for each request. If you want the cookie to be available, get the cookie first in the client and send it to the server.
Nov 13 '06 #9
I cannot access client code. This all needs to be from within the servlet. Surely this must be possible.

Incidently after creating the cookie, when I do a getPath or a getDomain I just get null back. Is this expected?
Nov 14 '06 #10
r035198x
13,262 8TB
I cannot access client code. This all needs to be from within the servlet. Surely this must be possible.

Incidently after creating the cookie, when I do a getPath or a getDomain I just get null back. Is this expected?
Yes it's expected. The path has not been set
I really think you should take a look at a cookie tutorial

http://www.echoecho.com/jscookies03.htm
http://www.roseindia.net/jsp/jspcookies.shtml
http://www.freewebmasterhelp.com/tutorials/cookies
Nov 14 '06 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chris Morgan | last post by:
I'm trying to get php to run on my webserver as a Java Servlet, it works the first time but fails the second time and crashes the JVM with the following error: I have tried the latest versions...
1
by: Blossom | last post by:
Hi there, I have a problem with storing my Cookies. I made a servlet in which I store a Cookie (in the doGet()-method) to detect whether a user has already visited my webpage. I used the...
1
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
7
by: Mike | last post by:
I have developed an application, for psyc patients.... they type in very personal information in a web form to help them work through problems in their lives. Once they enter the info, I encrypt...
0
by: TraceyAnnison | last post by:
I wonder if you can help me - I'm new to this, and working in a project that has already been configured to work with Axis. We have a Java project built with the Spring framework, in order to...
6
oll3i
by: oll3i | last post by:
when i run my jsp file i get an error SEVERE: Servlet.service() for servlet jsp threw exception java.util.MissingResourceException: Can't find bundle for base name MessagesBundle, locale pl_PL...
2
by: vijaykumardahiya | last post by:
Hello Sir, I have a simple Issue but It is not resolve by me i.e input parameter are not store in Ms-Access. I store the input parameter through Standard Action <jsp:useBean>. jsp:useBean call a...
7
by: swethak | last post by:
Hi, i have a command to convert the video file into image ffmpeg -i sample.wmv -f image2 -t 0.001 -ss 3 ss.jpg i run that one in command prompt it converted the video file into...
2
by: ramprakashjava | last post by:
This is index.jsp page code: <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.