Problems with Cookies


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Problems with Cookies

  1. #1
    Join Date
    Apr 2004
    Posts
    1

    Unhappy Problems with Cookies

    I'm having problems with building cookies in my java file.

    Here are my problems:

    1)How to I create a cookie?
    2)How to check if a cookie is created?(if created,how to write into that particular cookie)
    3)Where are the cookies stored when I run test?

    Below is my codes(I'm not sure if I did it right)

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;

    public class Prac4Task2 extends HttpServlet {

    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    response.setContentType("text/html");

    PrintWriter out = response.getWriter();

    String title = "Reading Request Parameters and HTTP Headers";

    // Generate HTML document head
    out.println("<html>\n<head>\n" +
    "<title>" + title + "</title>\n</head>");

    // Display a title in teh HTML document
    out.println("<body bgcolor=\"#FDF5E6\">\n" +
    "<h1 align=\"center\">" + title + "</h1>\n");

    Cookie[] cookies = request.getCookies();
    if(cookies!=null)
    {
    for(int i=0;i<cookies.length;i++){
    if(cookies[i].getName().equals(request.getParameter("StudentName"))
    out.println(cookies[i].getValue());
    }
    }
    else{
    Cookie userCookie=new Cookie(request.getParameter("StudentName"),request.getParameter("StudentNumber"));
    response.addCookie(userCookie);
    }
    // Close teh HTML docuemnt
    out.println("</body></html>");
    }

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

    doGet(request, response);

    }
    }
    nicwan

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    to make a cookie is simple. It will be something like the following
    Code:
    Cookie theCookie = new Cookie("YourCookieName", "YourCookieValue");
    where YourCookieName is the name of the cookie which you use to distinguish it from any other cookie the user has, and YourCookieValue is the value you want the cookie to store. To check if a particular cookie exists you have to loop through the array of cookies you got from the HttpServletRequest object, and if there is a cookie with the name that you're looking for then it exists. To get the name of a cookie you use the getName() method. The cookie isn't stored anywhere when you create it, it exists just like any other object you create. To store the cookie on the users computer you need to add it to the response object using the HttpServletResponse method addCookie(someCookie). This will store the cookie as a text file on the users computer.

  3. #3
    Join Date
    Feb 2004
    Posts
    808

    Re: Problems with Cookies

    >1)How to I create a cookie?

    your code already does it

    >2)How to check if a cookie is created?(if created,how to write into that particular cookie)

    im not sure i understand the question
    Cookie c = new Cookie("hello", "world")

    definitely creates a cookie. are you asking how to check if the users computer accepts cookies?

    >3)Where are the cookies stored when I run test?

    what is "test"? i see no code here called test, so i cannot answer your question
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    by "how do i check if a cookie is created?" i think he/she means how do I tell if a particular cookie was sent with the request object?

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by mikeBarr81
    by "how do i check if a cookie is created?" i think he/she means how do I tell if a particular cookie was sent with the request object?
    you musta got near 100% in lateral thinking class
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center