Java interview questions: – Define cookies in Java?

Cookies are piece of data which are stored at the client’s browser to track information regarding the user usage and habits. Servlets sends cookies to the browser client using the HTTP response headers. When client gets the cookie information it’s stored by the browser in the client’s hard disk. Similarly client returns the cookie information using the HTTP request headers.

java

Figure: – Cookies in action

java

Figure: – Cookies code walkthrough

Above is a simple snippet which shows how to use cookies. To create a cookie you need to use the “Cookie” class. In the above snippet step1 creates a cookie using the “Cookie” class. In this case “favoritecookiebook” is the name of the cookie. In Step 2 you can see how a cookie has been added to the response. Step 3 code shows how to get all the cookies which has come in the current request header. Step 4 shows the way to get a cookie from a collection in this case we wanted to retrieve “favoritecookiebook” from the cookies collection.

See the following video on Java which describes Batch processing in Hibernate:


Click for more Java interview questions

Regards,

Visit for more author’s blog on Java interview questions

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s