Cookies and JSP website

Reply

Join Date: Apr 2008
Posts: 4
Reputation: sorter is an unknown quantity at this point 
Solved Threads: 0
sorter sorter is offline Offline
Newbie Poster

Cookies and JSP website

 
0
  #1
Apr 11th, 2008
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:

  1. <%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.*"%>
  2.  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  4. "http://www.w3.org/TR/html4/loose.dtd">
  5.  
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>Testing Site</title>
  10. </head>
  11.  
  12. <body>
  13.  
  14. <table border="1" width="800" valign="top">
  15.  
  16. <tr>
  17. <td width="300" valign="top">
  18.  
  19. <h2>Navigation</h2>
  20.  
  21. <%
  22. if(request.getParameter("genre") == null) { %>
  23.  
  24. <p>Please select a music genre:</p>
  25.  
  26. <form action="index.jsp">
  27. <input type="radio" name="genre" value="rock" /> Rock <br />
  28. <input type="radio" name="genre" value="pop" /> Pop <br />
  29. <input type="submit" value="Search" />
  30. </form>
  31. <% }
  32. if (request.getParameter("genre") != null) {
  33. if (request.getParameter("genre").equals("rock")) { %>
  34. <p>You have chosen <%=request.getParameter("genre")%>. Here is a list of subgenres:</p>
  35. <form action="index.jsp">
  36. <input type="radio" name="subgenre" value="classic rock" /> Classic Rock<br />
  37. <input type="radio" name="subgenre" value="hard rock" /> Hard Rock<br />
  38. <input type="submit" value="Search" />
  39. </form>
  40. <%
  41. }
  42. if (request.getParameter("genre").equals("pop")) { %>
  43. <p>You have chosen <%=request.getParameter("genre")%>. Here is a list of subgenres:</p>
  44. <form action="index.jsp">
  45. <input type="radio" name="subgenre" value="classic pop" /> Classic Pop <br />
  46. <input type="radio" name="subgenre" value="jazz" /> Jazz <br />
  47. <input type="submit" value="Search" />
  48. </form>
  49. <%
  50. }
  51. } %>
  52.  
  53. </td>
  54.  
  55. <td width="600" valign="top">
  56.  
  57. <%
  58.  
  59. if (request.getParameter("subgenre") == null) { %>
  60. <h2>Welcome to My Website</h2>
  61. <p>Please use the left menu to select your music genre and subgenre.</p>
  62. <p>The last three sites you visted are: </p>
  63.  
  64. <ol>
  65. <li>Site One</li>
  66. <li>Site Two</li>
  67. <li>Site Three</li>
  68. </ol>
  69. <%
  70. }
  71.  
  72. if (request.getParameter("subgenre") != null) {
  73. if (request.getParameter("subgenre").equals("classic rock")) { %>
  74.  
  75. <p>You have chosen <%=request.getParameter("subgenre")%>. Here is a list of bands:</p>
  76. <ul>
  77. <li>The Beatles</li>
  78. <li>Pink Floyd</li>
  79. <li>AC/DC</li>
  80. <li>Aerosmith</li>
  81. </ul>
  82. <p>Click <a href="index.jsp">here</a> to go back to home</p>
  83. <%
  84. }
  85. if (request.getParameter("subgenre").equals("hard rock")) { %>
  86.  
  87. <p>You have chosen <%=request.getParameter("subgenre")%>. Here is a list of bands:</p>
  88. <ul>
  89. <li>Led Zeppelin</li>
  90. <li>Jimi Hendrix</li>
  91. <li>Metallica</li>
  92. <li>The Who</li>
  93. </ul>
  94. <p>Click <a href="index.jsp">here</a> to go back to home</p>
  95. <%
  96. }
  97. if (request.getParameter("subgenre").equals("classic pop")) { %>
  98.  
  99. <p>You have chosen <%=request.getParameter("subgenre")%>. Here is a list of bands:</p>
  100. <ul>
  101. <li>David Bowie</li>
  102. <li>Elton John</li>
  103. <li>The Police</li>
  104. <li>Buddy Holly</li>
  105. </ul>
  106. <p>Click <a href="index.jsp">here</a> to go back to home</p>
  107. <%
  108. }
  109. if (request.getParameter("subgenre").equals("jazz")) { %>
  110.  
  111. <p>You have chosen <%=request.getParameter("subgenre")%>. Here is a list of bands:</p>
  112. <ul>
  113. <li>Duke Ellington</li>
  114. <li>Kim Waters</li>
  115. <li>Igor</li>
  116. <li>Peter White</li>
  117. </ul>
  118. <p>Click <a href="index.jsp">here</a> to go back to home</p>
  119. <%
  120. }
  121. }
  122.  
  123. %>
  124. <br />
  125. <br />
  126.  
  127. </td>
  128. </tr>
  129. </table>
  130.  
  131. </body>
  132. </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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 4
Reputation: sorter is an unknown quantity at this point 
Solved Threads: 0
sorter sorter is offline Offline
Newbie Poster

Re: Cookies and JSP website

 
0
  #2
Apr 11th, 2008
Okay question 1:

How do I make a cookie persists through a refresh? I tried setMaxAge, but it does not work.

  1.  
  2. <%
  3. Cookie c = null;
  4. if (request.getParameter("subgenre") == null) {
  5. c = new Cookie ("abc", "none");
  6. } else {
  7. c = new Cookie ("subgenre", request.getParameter("subgenre"));
  8. }
  9. c.setMaxAge(250000);
  10.  
  11. %>

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 4
Reputation: sorter is an unknown quantity at this point 
Solved Threads: 0
sorter sorter is offline Offline
Newbie Poster

Re: Cookies and JSP website

 
0
  #3
Apr 11th, 2008
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!
Last edited by sorter; Apr 11th, 2008 at 9:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Cookies and JSP website

 
0
  #4
Apr 12th, 2008
Cookies are easy as well as tasty.
But you shouldn't use Java code in a JSP, so we're not going to help you do that.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 4
Reputation: sorter is an unknown quantity at this point 
Solved Threads: 0
sorter sorter is offline Offline
Newbie Poster

Re: Cookies and JSP website

 
0
  #5
Apr 12th, 2008
Why shouldn't I use java code in JSP?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Cookies and JSP website

 
0
  #6
Apr 13th, 2008
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).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JSP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC