Results 1 to 4 of 4

Thread: what is cookie in java?

  1. #1
    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.....

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    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:

    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 )
    {
    }
    }
    }

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    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.....

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    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:

    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);
    }
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,213,854.06223 seconds with 17 queries