How do I set a cookie value and retrieve the results in a later JSP page?

Todd Boyer

I'll use an example of setting a username as a JSP cookie to use in later pages.

<% 
	String userID = request.getParameter("username");
	Cookie theUsername = new Cookie("username", userID);
    	response.addCookie(theUsername);

%>

Now retrieve the results:

<% 
	Cookie theCookie = null;
	Cookie cookieList[] = request.getCookies();
		for(int i = 0; i < cookieList.length; i++) {
			theCookie = cookieList[i];
				if(theCookie.getName().equals("username")) { %>
		
		Welcome <%= theCookie.getValue() %>!

<% } } %>

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact