-
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
-
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.
-
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
-
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?
-
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks