Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Reading and writing cookies in Java Servlets and JSPs

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)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J906 - Java - Servlets in More Detail
  [479] New servlet from old - (2005-10-28)
  [1495] Single login and single threaded models - Java and PHP - (2008-01-04)
  [1550] Java (JSP and Servlet examples) live on our server - (2008-02-23)
  [1909] javax.servlet cannot be resolved - how to solve - (2008-11-26)
  [2183] Servlet life cycle, and Java Servlet variable scope - (2009-05-16)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [3044] Changing a Servlet - more that just editing and compiling - (2010-11-12)
  [3293] Distributing the server load - yet ensuring that each user return to the same system (Apache httpd and Tomcat) - (2011-05-18)
  [4431] A Java servlet that is also a stand alone program. And a server that is also a web client. - (2015-02-19)
  [4432] Java web application for teaching - now with sessions and clustering / load balancing demonstrations - (2015-02-20)

J901 - Java - Servlets
  [3997] Servlet v JSP (Java Server Page). What is the difference? - (2013-02-06)

A654 - Web Application Deployment - Configuring and Controlling Tomcat
  [837] Tomcat - Shutdown port - (2006-08-18)
  [907] Browser -> httpd -> Tomcat -> MySQL. Restarting. - (2006-10-28)
  [914] A practical example of roles - (2006-11-04)
  [1351] Compressing web pages sent out from server. Is it worth it? - (2007-09-14)
  [1370] Apache Tomcat Performance Tuning - (2007-09-29)
  [1503] Web page (http) error status 405 - (2008-01-12)
  [1553] Automatic startup and shutdown of Tomcat - (2008-02-24)
  [1762] WEB-INF (Tomcat) and .htaccess (httpd) - (2008-08-20)
  [1943] Port and Glasses - (2008-12-14)
  [1994] tomcat-users.xml; what a difference a space made - (2009-01-16)
  [2039] The Invoker - (2009-02-13)
  [2061] Tomcat 6 - Annotated Sample Configuration Files - (2009-03-01)
  [2163] CATALINA_OPTS v JAVA_OPTS - What is the difference? - (2009-05-09)
  [3043] Gathering information - logging - with log4j. First steps. - (2010-11-12)


Back to
Calculation within objects - early, last minute, or cached?
Previous and next
or
Horse's mouth home
Forward to
Java Server Pages - putting it all together
Some other Articles
Melksham Carnival
Python - what is going on around me?
Java - from applets to servlets. JSPs and more
Java Server Pages - putting it all together
Reading and writing cookies in Java Servlets and JSPs
Calculation 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
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2652_.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb