Cookies are small blocks of data created by a web server while a user is browsing a website and placed on the user's computer or other devices by the user's web browser. Cookies are placed on the device used to access a website, and more than one cookie may be placed on a user's device during a session.

The term "cookie" was coined by web-browser programmer Lou Montulli. It was derived from the term "magic cookie", a packet of data a program receives and sends back unchanged, used by Unix programmers. The term magic cookie derives from the fortune cookie, a cookie with an embedded message.(Wikipedia)

 

Usually, cookies are issued to connect the server and the client. The picture following is how the cookies work. 

Source: blog.csdn.net

The cookie is a concept used by servers and clients to maintain a connection state (login). Usually, if the login is successful, the server issues cookies and the cookies are issued by the client. The file is created in the temporary folder. On the server side, as you navigate the page, you can refer to the cookie information issued to the client's temporary folder. It is to maintain the connection status (login) between the server and the client. You don't have to take your membership information while moving the page with cookies.

 

Here are some basics of cookies in JSP.

To create cookies

Cookie cook = new Cookie (String name, String value);
Cookie cook = new Cookie ("id", "test");

To add cookies

response.addCookie(cook);

To edit the value of cookies

cook.setValue(newValue);

To set expired time

cook.setMaxAge(int expiry); // seconds
cook.setMaxAge(60*60); // an hour

To read cookies

Cookie[] cook = request.getCookie();

Name : cook.getNmae()
Value : cook.getValue()

If you feel you are more familiar with cookies, check out an example!

 

makeCookie.jsp

<%@ page contentType="text/html; charset=euc-kr" %>

<html>
	<head>
		<title>To create cookies</title>
	</head>

<%
   String cookieName = "id";
   Cookie cookie = new Cookie(cookieName, "sheriff");
   cookie.setMaxAge(60*2); 
   cookie.setValue("guardian");
   response.addCookie(cookie);
%>

	<body>
	<h2>Cookie example</h2>
	<P>

"<%=cookieName%>" Cookie has been created.<br>

		<input type="button" value="Check Cookies" onclick="javascript:window.location='useCookie.jsp'">
	</P>
	</body>
</html>

useCookie.jsp

<%@ page contentType="text/html; charset=euc-kr" %>

<html>
	<head>
		<title>Brining cookies from a browser</title>
	</head>
	<body>
	<h2>Brining cookies from a browser</h2>

<%
	Cookie[] cook = request.getCookies();

	if( cook != null ){
		for(int i=0; i<cook.length;++i){
			if(cook[i].getName().equals("id")){
%>

		Name of the cookie is "<%=cook[i].getName()%>" and 
		the value is "<%=cook[i].getValue()%>" .

<%
			}
		}
	}
%>

	</body>
</html>

To check the cookies that are created, on the browser that you want to check the cookies, enter F12 and see the cookies in the Application tap. The cookies will be there until the expired time you set. 

Cookies track your behavior on websites and browsers, and they can target with ever more personalized content based on users’ engagement on web pages. Cookies can also be used to identify unique and returning visitors to a website. When a user visits a website, the site creates a cookie that stores data that identifies that user. If the user leaves the website and visits again, the website will recognize the user as a returning user. A good example of why this can be a huge help would be an e-commerce website that remembers the products you added to your cart the last time you visited, even though you didn't complete the purchase. However, when you let people use your browser account, they can see what kinds of sites you visit by peeking at your browser file — unless you get rid of them.(AVG)

 

Here are some more examples of cookies. 

To create cookies

<%@ page contentType = "text/html; charset=euc-kr" %>
<%@ page import = "java.net.URLEncoder" %>

<% 
    Cookie cookie = new Cookie("name", URLEncoder.encode("Meadow"));
    response.addCookie(cookie); // To create cookies
%>

<html>
	<head><title>Creating Cookies</title></head>
	<body>

<%= cookie.getName() %> Value = "<%= cookie.getValue() %>"

	</body>
</html>

To view the cookies list

<%@ page contentType = "text/html; charset=euc-kr" %>
<%@ page import = "java.net.URLDecoder" %>

<html>
	<head><title>Cookies List</title></head>
	<body>
	Cookies List<br>

<%
    Cookie[] cookies = request.getCookies();

    if (cookies != null && cookies.length > 0) {
        for (int i = 0 ; i < cookies.length ; i++) {
%>

			<%= cookies[i].getName() %> = 
			<%= URLDecoder.decode(cookies[i].getValue()) %><br>

<%
        }//for end

    } else {
%>

		There is no cookies.
<%
    }
%>

	</body>
</html>

If you didn't set the expired time, the cookies will be deleted once you close the window. So, it would help if you opened the window to see the cookies. 

There are two ways to modify the values of the cookies.

To modify value1

<%@ page contentType = "text/html; charset=euc-kr" %>
<%@ page import = "java.net.URLEncoder" %>

<%
    Cookie[] cookies = request.getCookies();

    if (cookies != null && cookies.length > 0) {
        for (int i = 0 ; i < cookies.length ; i++) {

            if (cookies[i].getName().equals("name")) {
                Cookie cookie = new Cookie("name", URLEncoder.encode("JSP Programming"));
                response.addCookie(cookie);

            }// if end

        }//for end
    }
%>

<html>
	<head><title>Modifying Cookies</title></head>
	<body>
		Change the value of cookie "name".
	</body>
</html>

To modify value2

<%@ page contentType = "text/html; charset=euc-kr" %>
<%@ page import = "java.net.URLEncoder" %>

<%
    Cookie[] cookies = request.getCookies();

    if (cookies != null && cookies.length > 0) {
        for (int i = 0 ; i < cookies.length ; i++) {

            if (cookies[i].getName().equals("name")) {
                cookies[i].setValue(URLEncoder.encode("Java and JSP"));
                response.addCookie(cookies[i]);
            }// if end

        }//for end
    }
%>

<html>
	<head><title>Modifying Cookies</title></head>
	<body>
		Change the value of cookie "name".
	</body>
</html>

Unlike creating cookies, there is no function to delete cookies. However, there is another way to delete them.

You will create a cookie with the name with a null value. Set the expired time '0' and use the response object.

To delete cookies

<%@ page contentType = "text/html; charset=euc-kr" %>
<%@ page import = "java.net.URLEncoder" %>

<%
    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length > 0) {
        for (int i = 0 ; i < cookies.length ; i++) {

            if (cookies[i].getName().equals("name")) {
                Cookie cookie = new Cookie("name", "");
                cookie.setMaxAge(0);
                response.addCookie(cookie);
            }//if end

        }//for end
    }
%>

<html>
	<head><title>Delete Cookies</title></head>
	<body>
		Delete cookie "name".
	</body>
</html>

 

No cookies in the list.

To set the expired time.

<%@ page contentType = "text/html; charset=euc-kr" %>

<%
    Cookie cookie = new Cookie("oneh", "1time");
    cookie.setMaxAge(60 * 60); // 60sec * 60 = 1 hour
    response.addCookie(cookie);
%>

<html>
	<head><title>Cookie's expired time</title></head>
	<body>

	In one hour, this cookie will be deleted.

	</body>
</html>

 

+ Recent posts