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:

<%@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!

Recommended Answers

All 5 Replies

Okay question 1:

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

<%
         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 ? :(

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!

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.

Why shouldn't I use java code in JSP?

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.