Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 21-11-2009
Member
 
Join Date: Jan 2009
Posts: 45
what is cookie in java?

Hi All,

I am learning advanced java programming. I don't have any idea regarding cookie.Is it something related to internet?

Can anyone help me to understand concept of the cookie in the java? If possible then explain this cookie concept with syntax and example.

Your help is greatly appreciated.....
Reply With Quote
  #2  
Old 21-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: what is cookie in java?

A cookie is a technique which a CGI server use to hold information within in client of HTTP which is useful for later retrieval to remind itself.

One of the important use of cookie is to track login. The HTTP client sends a an authentication cookie along with every request for reminding to the HTTP server that it is logged in. Following example will clear doubts regarding the cookie in the java:

Quote:
import java.Applet.Applet;

import netscape.javascript.JSObject;

public class cookies extends Applet
{
public void init ()
{
try
{
JSObject window = JSObject.getWindow(this );
JSObject document = (JSObject)window.getMember( "document" );

// write one more new cookie
document.setMember( "cookie", "drink=root beer; expires=Fri, 31-Jan-2008 00:00:01 GMT;" );

// get all the unexpired cookies
String mycookies = (String) document.getMember( "cookie" );
}
catch ( Exception e )
{
}
}
}


//code to set cookies in other browsers. You must set one field at a time.

public class cookies extends Applet
{
public void init ()
{
try
{
JSObject window = JSObject.getWindow(this );
JSObject document = (JSObject)window.getMember( "document" );

// write one more new cookie
document.setMember( "cookie", "drink=root beer;" );
document.setMember( "cookie", "expires=Fri, 31-Jan-2008 00:00:01 GMT;" );

// get all the unexpired cookies
String mycookies = (String) document.getMember( "cookie" );
}
catch ( Exception e )
{
}
}
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #3  
Old 21-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: what is cookie in java?

Hi,

please go through the below information regarding cookie:

*. Part of information which id hold by web browser on the side of the client computer in the .txt file which is retrieved on next access is called as "Cookie".

*. The information of the cookie is hold as pairs of value.
".cookie" property of the java is useful for client side cookie manipulation.

*. "Creation of Cookie","Read Cookie","Delete The Cookie' these are the three steps of the cookie manipulation

I hope it will help you.....
Reply With Quote
  #4  
Old 21-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: what is cookie in java?

Cookie in java is essential to represent status of client which is stored in the web browser

Browser stores the records of the cookie which includes expiry date of the cookie, value of the cookie, name of the cookie.

For syntax of the cookie refer the below java program code, try to run this java code on the your pc and lets see what happens:

Quote:
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {

Cookie cookie = null;
Cookie[] cookies = request.getCookies();
boolean newCookie = false;

if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("mycookie")) {
cookie = cookies[i];
}
}
}
if (cookie == null) {
newCookie = true;
int maxAge;
try {
maxAge = new Integer(getServletContext().getInitParameter(
"cookie-age")).intValue();
} catch (Exception e) {
maxAge = -1;
}

cookie = new Cookie("mycookie", "" + getNextCookieValue());
cookie.setPath(request.getContextPath());
cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title>Information of Cookie</title>");
out.println("</head>");
out.println("<body>");

out
.println("<h2> Information of the cookie named as \"mycookie\"</h2>");

out.println("Value of the Cookie: " + cookie.getValue() + "<br>");
if (newCookie) {
out.println("Max-Age of the Cookie : " + cookie.getMaxAge() + "<br>");
out.println("Path of the Cookie : " + cookie.getPath() + "<br>");
}

out.println("</body>");
out.println("</html>");

out.close();
}

private long getNextCookieValue() {
return new java.util.Date().getTime();

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {

doGet(request, response);
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "what is cookie in java?"
Thread Thread Starter Forum Replies Last Post
i need cookie grabber help on php piroo Software Development 1 24-10-2010 12:30 AM
How to Create a Cookie in PHP? sivaranjan Software Development 4 27-01-2010 10:53 PM
First Party Cookie vs Third party Cookie Ballu Technology & Internet 4 13-08-2009 01:30 PM
What is a Cookie? Dharmavira Technology & Internet 5 18-02-2009 12:33 PM
Deleting Cookie Tobius Windows Software 2 05-11-2008 07:49 PM


All times are GMT +5.5. The time now is 06:07 AM.