•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 401,483 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,225 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 593 | Replies: 5
![]() |
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Hi everyone,
I am trying to create a simple website using cookies to remember the last 2 links visited on my site. It is a very simple 2 column website:
Right now, cookies have not been implemented. I am simply organizing the flow of the website.
Now my question is: How do I add cookies to make my website remember the last 2 or 3 links visited on the website? As you can see, I just entered Site One, Site Two, and Site Three for intuitive purposes and they are static.
I am not very familiar with implementing cookies, but I do understand how they work.
Thanks in advance!
I am trying to create a simple website using cookies to remember the last 2 links visited on my site. It is a very simple 2 column website:
JSP Syntax (Toggle Plain Text)
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Testing Site</title> </head> <body> <table border="1" width="800" valign="top"> <tr> <td width="300" valign="top"> <h2>Navigation</h2> <% if(request.getParameter("genre") == null) { %> <p>Please select a music genre:</p> <form action="index.jsp"> <input type="radio" name="genre" value="rock" /> Rock <br /> <input type="radio" name="genre" value="pop" /> Pop <br /> <input type="submit" value="Search" /> </form> <% } if (request.getParameter("genre") != null) { if (request.getParameter("genre").equals("rock")) { %> <p>You have chosen <%=request.getParameter("genre")%>. Here is a list of subgenres:</p> <form action="index.jsp"> <input type="radio" name="subgenre" value="classic rock" /> Classic Rock<br /> <input type="radio" name="subgenre" value="hard rock" /> Hard Rock<br /> <input type="submit" value="Search" /> </form> <% } if (request.getParameter("genre").equals("pop")) { %> <p>You have chosen <%=request.getParameter("genre")%>. Here is a list of subgenres:</p> <form action="index.jsp"> <input type="radio" name="subgenre" value="classic pop" /> Classic Pop <br /> <input type="radio" name="subgenre" value="jazz" /> Jazz <br /> <input type="submit" value="Search" /> </form> <% } } %> </td> <td width="600" valign="top"> <% if (request.getParameter("subgenre") == null) { %> <h2>Welcome to My Website</h2> <p>Please use the left menu to select your music genre and subgenre.</p> <p>The last three sites you visted are: </p> <ol> <li>Site One</li> <li>Site Two</li> <li>Site Three</li> </ol> <% } if (request.getParameter("subgenre") != null) { if (request.getParameter("subgenre").equals("classic rock")) { %> <p>You have chosen <%=request.getParameter("subgenre")%>. Here is a list of bands:</p> <ul> <li>The Beatles</li> <li>Pink Floyd</li> <li>AC/DC</li> <li>Aerosmith</li> </ul> <p>Click <a href="index.jsp">here</a> to go back to home</p> <% } if (request.getParameter("subgenre").equals("hard rock")) { %> <p>You have chosen <%=request.getParameter("subgenre")%>. Here is a list of bands:</p> <ul> <li>Led Zeppelin</li> <li>Jimi Hendrix</li> <li>Metallica</li> <li>The Who</li> </ul> <p>Click <a href="index.jsp">here</a> to go back to home</p> <% } if (request.getParameter("subgenre").equals("classic pop")) { %> <p>You have chosen <%=request.getParameter("subgenre")%>. Here is a list of bands:</p> <ul> <li>David Bowie</li> <li>Elton John</li> <li>The Police</li> <li>Buddy Holly</li> </ul> <p>Click <a href="index.jsp">here</a> to go back to home</p> <% } if (request.getParameter("subgenre").equals("jazz")) { %> <p>You have chosen <%=request.getParameter("subgenre")%>. Here is a list of bands:</p> <ul> <li>Duke Ellington</li> <li>Kim Waters</li> <li>Igor</li> <li>Peter White</li> </ul> <p>Click <a href="index.jsp">here</a> to go back to home</p> <% } } %> <br /> <br /> </td> </tr> </table> </body> </html>
Right now, cookies have not been implemented. I am simply organizing the flow of the website.
Now my question is: How do I add cookies to make my website remember the last 2 or 3 links visited on the website? As you can see, I just entered Site One, Site Two, and Site Three for intuitive purposes and they are static.
I am not very familiar with implementing cookies, but I do understand how they work.
Thanks in advance!
Last edited by sorter : Apr 11th, 2008 at 1:21 pm.
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Okay question 1:
How do I make a cookie persists through a refresh? I tried setMaxAge, but it does not work.
Here is the code I used for a basic cookie, but when I refresh the page, the cookie becomes null again. I understand that is because I set Cooke c = null; however, if I don't, my program wouldn't run.
I need some help please ?
How do I make a cookie persists through a refresh? I tried setMaxAge, but it does not work.
jsp Syntax (Toggle Plain Text)
<% Cookie c = null; if (request.getParameter("subgenre") == null) { c = new Cookie ("abc", "none"); } else { c = new Cookie ("subgenre", request.getParameter("subgenre")); } c.setMaxAge(250000); %>
Here is the code I used for a basic cookie, but when I refresh the page, the cookie becomes null again. I understand that is because I set Cooke c = null; however, if I don't, my program wouldn't run.
I need some help please ?
Last edited by sorter : Apr 11th, 2008 at 2:36 pm.
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Well.... I guess cookie's too difficult for everyone else too eh?
I feel a little bit better knowing that nobody on a JSP forum can solve the problem I (a beginner) can't solve.
EDIT: If you think I am trying to anger you to induce a reply, you are correct. PLEASE HELP!
I feel a little bit better knowing that nobody on a JSP forum can solve the problem I (a beginner) can't solve.
EDIT: If you think I am trying to anger you to induce a reply, you are correct. PLEASE HELP!
Last edited by sorter : Apr 11th, 2008 at 8:04 pm.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
that's been explained many times already.
JSP are meant to be maintained by people with no Java skills and to be solely a display technology. Use tag libraries (especially JSTL and/or JSF) instead, pull all business logic (which includes ALL database logic) to servlets (or even better, into classes and EJBs called from servlets).
JSP are meant to be maintained by people with no Java skills and to be solely a display technology. Use tag libraries (especially JSTL and/or JSF) instead, pull all business logic (which includes ALL database logic) to servlets (or even better, into classes and EJBs called from servlets).
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JSP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the JSP Forum
- Previous Thread: Passing parameters between jsp pages
- Next Thread: Problem Tranforming xml using jstl



Linear Mode