Page 1 of 1
I need help baking cookies
PostPosted: Tue Dec 20, 2005 9:07 am
by glitch1501
ok, i have pretty much no knowledge of cookies other than what ive read off of cookiecentral.com
on this site i am working on, the first page you get to is a map with regions for this company, i want to make a cookie that will remember which region you click on so that it automatically goes to that region when you go back to the site...i hope its possible
any help would be greatly appreciated
glitch
PostPosted: Tue Dec 20, 2005 10:34 am
by Itachi
Why
PostPosted: Tue Dec 20, 2005 11:34 am
by glitch1501
why what?
PostPosted: Tue Dec 20, 2005 3:47 pm
by Slater
hmm... that might be a bit difficult, though don't take my word for it so readily. What's the site, may I take a look?
PostPosted: Tue Dec 20, 2005 5:10 pm
by Warrior4Christ
I've used cookies before using a Java Servlet and Tomcat... I'm pretty sure you can do the same sort of thing with inline Java code...
Here's how I did it (added stars for emphasis of relevant parts):
- Code: Select all
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
// ProcessNameExt
public class ProcessNameExt extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// get the name parameter from URL rewriting
String name = request.getParameter(LoanCalcExt.nameTag);
// make a cookie to store the name so it can be
// accessed in other parts without URL rewriting.
[b]****Cookie cookie = new Cookie(LoanCalcExt.nameTag, name);****
****response.addCookie(cookie);****[/b]
// dynamically write the html file...
out.println("<html>");
out.println("<head>");
out.println("<title>Thank you for registering.</title>");
out.println("</head>");
out.println("<body bgcolor=#FFFFFF>");
out.println("<h3>Thank you for registering.</h3>");
out.println("Please click <a href=\"LoanCalcExt\">here</a> to use the loan calculator.");
out.println("</body>");
out.println("</html>");
}
// if the method was POST, then make it do the same thing as GET.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
doGet(request, response);
}
}
- Code: Select all
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
// LoanCalcExt
public class LoanCalcExt extends HttpServlet
{
public final static String sizeTag = "principal";
public final static String interestRateTag = "rate";
public final static String durationTag = "duration";
public final static String nameTag = "username";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// get the cookies stored in the HTTP header
[b]****Cookie[] cookies = request.getCookies();****[/b]
String name = null; // make the default null, so we can tell if a cookie name was given
// search through the cookies for the name cookie
[b]******************************
for(int index = 0; index < cookies.length; index++)
{
if(cookies[index].getName().equals(nameTag))
{
// found it, set the name variable to it
name = cookies[index].getValue();
break;
}
}
********************************[/b]
// dynamically write html file...
out.println("<html>");
out.println("<head>");
out.println("<title>Loan Calculator</title>");
out.println("</head>");
out.println("<body bgcolor=#FFFFFF>");
// if no name cookie was found, make the user register first
if(name == null)
{
out.println("<h1>Bank loan calculator - Please Register</h1>");
out.println("You must register to use this service.
");
out.println("Please enter your name in the following form.
");
out.println("<form method=GET action=\"ProcessNameExt\">");
out.println("Your name: <input type=text size=25 name=" + nameTag + ">
");
out.println("<input type=submit>
");
out.println("</form>");
}
// if the user has a name cookie, then carry on with the input form
else
{
out.println("<h1>Bank loan calculator</h1>");
out.println("<h2>Greetings " + name + "</h2>");
out.println("Work out how much interest you would have to pay on a house loan.
");
out.println("Enter the size of the loan, the annual interest rate and the number of years the loan is for.
");
out.println("<form method=GET action=\"CalcInterestExt\">");
out.println("Size of loan (in dollars): <input type=text size=20 name=" + sizeTag + ">
");
out.println("Annual Interest Rate(%): <input type=text size=20 name=" + interestRateTag + ">
");
out.println("Duration of loan (in years): <input type=text size=20 name=" + durationTag + ">
");
out.println("<input type=submit>
");
out.println("</form>");
out.println("<p>Press the submit button to calculate your result.</p>");
out.println("<p>If your name is not correct, then please enter your name in the following form:</p>");
out.println("<form method=GET action=\"ProcessNameExt\">");
out.println("Your name: <input type=text size=25 name=" + nameTag + ">
");
out.println("<input type=submit>
");
out.println("</form>");
}
out.println("</body>");
out.println("</html>");
}
// if the method was POST, then make it do the same thing as GET.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
doGet(request, response);
}
}
...though on second thoughts, you want to store a cookie attached to an URL rather than a previous page (as done here)... so you probably use a different method.
PostPosted: Tue Dec 20, 2005 5:54 pm
by Slater
hmm... Yeah, listen to W4C... he knows more about cookies than I do XD
PostPosted: Thu Dec 22, 2005 11:00 am
by Mithrandir
Huh? That's WAY more complicated than a javascript set cookie function could EVER be. IMHO.
In 5 seconds I found the following on google:
- Code: Select all
function SetCookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
Go here for the complete text:
http://www.javascripter.net/faq/settinga.htm
For information on how to read and set cookies, you can find it here:
http://www.netspade.com/articles/2005/11/16/javascript-cookies/