Class 2 Intermediate Java 30-IT-397  

Cookies, the old fashioned way

<%@page contentType="text/html"%>
<html>
<head><title>JSP Page</title></head>
<body>

<%-- <jsp:useBean id="beanInstanceName" scope="session" class="package.class" /> --%>
<%-- <jsp:getProperty name="beanInstanceName"  property="propertyName" /> --%>

<%
 

// Get an array of cookies that exist.
Cookie cookies[] = request.getCookies();

// Loop through that array.
for (int i = 0; i < cookies.length; i++) {
    // If we find one with a name userName, print a welcome greeting.
    if (cookies[i].getName().equals("userName")) {
        out.print("Welcome " + cookies[i].getValue());
    }
}

// Get the name, if passed in.
String name = request.getParameter("name");

// Write the name to a cookie for the next refresh.
if (name != null) {
    Cookie cookie = new Cookie("userName", name);
    response.addCookie(cookie);
}
%>

<form name = "welcome" action = "cookiesjsp.jsp" method=GET>

What's your name, dude?
<input type = "text" name = "name">
<input type = "submit">

</form>
</body>
</html>

Newfangled Sessions

Created by:  Brandan Jones December 17, 2001