Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

JSP - display cookies not working



</code> 
Hi,

I've been trying to get the jsp to display a table to show the name enter from a form.

However, it doesn't show the table which will display the Name, Value and Comments.

Hope someone can advise me where have I gone wrong with my codes. It's quite messy because I'm tyring to follow examples from internet.  So please be patient with me.  Million Thanks.

<code><%@ page language="java" %>
<%
            String cookieName = "username";
            Cookie cookies[] = request.getCookies();
            Cookie c = null;%>
<%out.println("<html><head><title>Servlet Cookie2St Information</title>"
                     + "</head><body>");%>
<%if ((cookies == null) || (cookies.length == 0)) {%>
<%out.println("No cookies found");%>
<%} else {%>
<center><h1>Cookies found in the request</h1></center>
<br><table border><tr><th>Name</th> <th>Value</th><th>Comment</th>
    + <th>Max Age</th></tr>);

    <%for (int i = 0; i < cookies.length; i++) {%>
    <tr><td> c.getName()%> </td>
        <td>  c.getValue() %></td>
        <td> c.getComment()%> </td>
        <td> Integer.toString(c.getMaxAge())%>  </td></tr>;
    out.println</table>;%>
<%@ page language="java" %>
<%
            String cookieName = "username";
            Cookie cookies[] = request.getCookies();
            Cookie c = null;%>
<%out.println("<html><head><title>Servlet Cookie2St Information</title>"
                     + "</head><body>");%>
<%if ((cookies == null) || (cookies.length == 0)) {%>
<%out.println("No cookies found");%>
<%} else {%>
<center><h1>Cookies found in the request</h1></center>
<br><table border><tr><th>Name</th> <th>Value</th><th>Comment</th>
    + <th>Max Age</th></tr>);

    <%for (int i = 0; i < cookies.length; i++) {%>
    <tr><td> c.getName()%> </td>
        <td>  c.getValue() %></td>
        <td> c.getComment()%> </td>
        <td> Integer.toString(c.getMaxAge())%>  </td></tr>;
    out.println</table>;%>

%>  
<html>  
<head>  
<title>Show Saved Cookie</title>  

 
<%  
if (c == null) {  
%>  
No Cookie found with the name <%=cookieName%>  
<%  
} else {  
%>  
 
<center><h1>Cookies found in the request</h1></center>  
        + "<table border><tr><th>Name</th> <th>Value</th><th>Comment</th>" 
        + "<th>Max Age</th></tr></table>");  
                out.println("<tr><td>" + c.getName() + "</td>" +  
        "<td>" + c.getValue() + "</td>" +  
        "<td>" + c.getComment() + "</td>" +  
        "<td>" + Integer.toString(c.getMaxAge()) + "</td>"+  
                 %>  
<%  
}  
%>  
</body>  
</html>