| | |
Cookies and JSP website
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
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 2:21 pm.
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
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 3:36 pm.
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
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 9:04 pm.
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).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Other Threads in the JSP Forum
- Previous Thread: Passing parameters between jsp pages
- Next Thread: Problem Tranforming xml using jstl
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






