Do you want to check (and perhaps set) cookies within a Java Servlet? I was looking around for good, straightforward examples today but found that most of the published code is only snippets rather than complete examples, or is over-complex for what should be a simple demo.
First - do you
really want to use Cookies directly? If you're looking to save data between a series of accesses that are linked together into a session, Java's HttpSession objects may be what you need. You can store objects into your session, and it's then stored on the server and pulled back when the same visitor returns to carry on his session. There's a complete source code example
[here] - an application which lets each user make memo notes, and repeats those notes back each subsequent time the page is called up while the session lasts. Vitally, each user's notes are independent of each other's.
Sessions will be lost when the browser is closed, and expired off the server in a timeout controlled by the
web.xml file on the Tomcat Server. If you want your data to be retained longer,
per user / browser, then you might want to use a cookie at a lower level. Examples are a shopping site which welcomes you back after several days or weeks, or a site on which you have set user preferences for colour or font size.
Your Cookie based application will want to check for existing cookies before it sets up any new ones, and to do this you run the getCookies method on your request object. It returns an array of Cookie objects, and you can run getName and getValue on each of those objects to see what existing cookies are set. Once you've read your cookies back and analysed them, you may want to set a new cookie - create yourself a Cookie object, giving a name and a value. Then set any other attributes such as how long it is go live for. And finally add it to the response object so that it gets sent.
There's a (new!) complete example of checking and setting a cookie from a Java servlet on our web site - I wrote it about an hour ago with delegates on today's
Java Bootcamp Class. Oh - I should provide a link ;-) ... it's
[here]
(written 2010-02-26 16:49:37)
Associated topics are indexed under
J901 - Java - Servlets [1909] javax.servlet cannot be resolved - how to solve - (2008-11-26)
A654 - Web Application Deployment - Configuring and Controlling Tomcat [2163] CATALINA_OPTS v JAVA_OPTS - What is the difference? - (2009-05-09)
[2061] Tomcat 6 - Annotated Sample Configuration Files - (2009-03-01)
[2039] The Invoker - (2009-02-13)
[1994] tomcat-users.xml; what a difference a space made - (2009-01-16)
[1943] Port and Glasses - (2008-12-14)
[1762] WEB-INF (Tomcat) and .htaccess (httpd) - (2008-08-20)
[1553] Automatic startup and shutdown of Tomcat - (2008-02-24)
[1503] Web page (http) error status 405 - (2008-01-12)
[1370] Apache Tomcat Performance Tuning - (2007-09-29)
[1351] Compressing web pages sent out from server. Is it worth it? - (2007-09-14)
[914] A practical example of roles - (2006-11-04)
[907] Browser -> httpd -> Tomcat -> MySQL. Restarting. - (2006-10-28)
[837] Tomcat - Shutdown port - (2006-08-18)
J906 - Java - Servlets in More Detail [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2183] Servlet life cycle, and Java Servlet variable scope - (2009-05-16)
[1550] Java (JSP and Servlet examples) live on our server - (2008-02-23)
[1495] Single login and single threaded models - Java and PHP - (2008-01-04)
[479] New servlet from old - (2005-10-28)
Some other Articles
Melksham CarnivalPython - what is going on around me?Java - from applets to servlets. JSPs and moreJava Server Pages - putting it all togetherReading and writing cookies in Java Servlets and JSPsCalculation within objects - early, last minute, or cached?Getting a phone line changed ...Length, size or capacity in Java?Java arrays - are they true arrays or not?Removing duplicates from a MySQL table