<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; Cookie Example</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/cookies-interview-questions/cookie-example/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Cookie Examples</title>
		<link>http://www.bestjavainterviewquestions.com/cookie-examples-2/</link>
		<comments>http://www.bestjavainterviewquestions.com/cookie-examples-2/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 17:23:19 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Cookie Example]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=286</guid>
		<description><![CDATA[2. Cookies It is sometimes necessary to use cookies for storing information on the client side. A cookie can be accessed from within an element, if the element definition contains an incookie or outcookie tag. An incookie is a cookie that can be read, and an outcookie can obviously be set. Let&#8217;s show how it [...]]]></description>
			<content:encoded><![CDATA[<p>2. Cookies</p>
<p>It is sometimes necessary to use cookies for storing information on the client side. A cookie can be accessed from within an element, if the element definition contains an incookie or outcookie tag. An incookie is a cookie that can be read, and an outcookie can obviously be set.</p>
<p>Let&#8217;s show how it works with a small example where we store the time when the user last visited a certain page. This is a typical real world example, and could be used for example to determine which news items are new since the last visit on a news site.</p>
<p>If we call our cookie last_visit and want to let the element NewsList get and set the cookie, the element definition would look like this:</p>
<p>Example 9.4. Defining an element with a cookie</p>
<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt;<br />
&lt;!DOCTYPE element SYSTEM &#8220;/dtd/element.dtd&#8221;&gt;</p>
<p>&lt;element implementation=&#8221;NewsList&#8221;&gt;<br />
&lt;incookie name=&#8221;last_visit&#8221;/&gt;</p>
<p>&lt;outcookie name=&#8221;last_visit&#8221;/&gt;<br />
&lt;/element&gt;</p>
<p>Now that the element is allowed to access the cookie, we can write the Java code to read and write the cookie.</p>
<p>Example 9.5. NewsList.java: Using cookies from Java</p>
<p>package org.rifers.elephant.elements;</p>
<p>import com.uwyn.rife.engine.Element;</p>
<p>import javax.servlet.http.Cookie;<br />
import java.util.*;<br />
import java.text.*;</p>
<p>public class NewsList extends Element<br />
{<br />
public void showNews(Date lastVisited)<br />
{<br />
// Print new news items<br />
}</p>
<p>public void processElement()<br />
{<br />
Date date = Calendar.getInstance().getTime();</p>
<p>if (hasCookie(&#8220;last_visit&#8221;))<br />
{<br />
Cookie cookie = getCookie(&#8220;last_visit&#8221;);</p>
<p>try<br />
{<br />
Date last = DateFormat.getInstance().parse(cookie.getValue());<br />
showNews(last);<br />
}<br />
catch (ParseException e)<br />
{<br />
// Handle exception<br />
}<br />
}<br />
else<br />
{<br />
showNews(null);<br />
}</p>
<p>setCookie(new Cookie(&#8220;last_visit&#8221;,<br />
DateFormat.getInstance().format(date)));<br />
}<br />
}</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>September 8, 2008 -- <a href="http://www.bestjavainterviewquestions.com/cookie-examples/" title="Cookie Examples">Cookie Examples</a> (0)</li><li>September 2, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-session/" title="Define Session">Define Session</a> (0)</li><li>December 10, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-event-delegation-model/" title="What is Event Delegation Model">What is Event Delegation Model</a> (0)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/java-lang-package-interview-questions/" title="Java lang package interview questions">Java lang package interview questions</a> (2)</li><li>December 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-abstract-classexplan/" title="What is Abstract Class?Explan">What is Abstract Class?Explan</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/cookie-examples-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

