Updated April 4, 2023
Introduction to Cookies in JSP
Generally Cookies are defined as the text files it will be stored in the computer mainly in the client machine for tracking purposes like login details, file transfer, etc. We can also saw the cookies in web browsers in web applications using debug mode choose “Application ->Storage-. Cookies” we can see whatever we see the web applications or websites in the cookies column. In JSP we handline the cookies concept like cookies handling JSP transparently supports for the cookies using HTTP cookies and JSP has some predefined methods for handling the cookies. The client-server interactions like request and response methods using like request. get cookies() used to return the array of the cookie instance.
Syntax:
The syntax for the cookies using JSP methodology as below.
<html>
<body>
<%
--create cookie instance using cookie class—
---write the JSP logic codes using user sessions----
%>
</body>
</html>
How to Handle Cookie in JSP?
In JSP we can store the user details in the cookie file and it has been referenced with the help of session-id that has been passed back and come forth between the client and server. It has also been stored in the server-side applications to store information in the client browsers with the help of cookies it can create the specific variables for each client browsers it can store the user details in the last accessed date. Cookies are provided with a persistence technique that can be composite with the HTTP protocol using stateless.
Just using the methods like getting cookies() for sending the request and get the response data for the server using an array of cookie instances. It will store some small piece of data that is related to the user with the help of Web browsers. The cookies are named with the single value but it may have the optional attributes it includes with the common path, domains qualifiers also hostname stored in the cookie it specifies the maximum time count, age count, and versions.
In web applications after the first transaction of the client and server next time the same client browser will send any type of the request to the web server the same web server will use the user’s data it can be used for some other purposes. Most web browsers accepted cookies but some of what related Informations there are related through the browsers session will contradict or accept the cookies features.
Each cookie has an individual life span and flushed out by the user browsers at the end of the life span period. The usage of the cookie is to recognize the users on the server. We can create cookies using the cookie class in the servlet API and also additionally cookies added to the response instance using the add cookie() method. Using Http Cookies to perform the user session management and the web containers to store the session ids on the individual user machine using the getSession() method we can calculate the user session on the web containers.
Some user browsers do not support the cookie technique and some of them disable the cookie in the browsers in that time cookie is based upon the session management fails on the servers. We can identify the cookies with the help of the HttpSession strategy management “isRequestSessionIdFromCookie” method in the HTTP servlet interface for the request session it always returns true if already cookies are used in the web browsers. Same as the “isRequestSessionIdFromURL” method it also returns true if the URL rewriting technique is used for the session management.
Whenever the user opened the web page for the first time the server received no cookies from the browser’s web request. But always the JSP code added default for cookie named “Cookie_0” to the response and the JSP servers added default session-id for the cookie name called “JSessionID” if I click the browser refresh button it will also sent the two cookies back to the server in each request. Cookies once come with the client after server sending the response they will allocate memory automatically in client machine 1. In Memory cookies,/per-session cookies and 2.Persistent cookies are the two types of cookies in JSP. Cookies without expired time are called as memory cookies, with expiry time is known as persistent cookies.
Examples to Implement Cookies in JSP
Below are some examples mentioned:
Example #1
Code:
<html>
<body>
<form action = "custom.jsp" method = "post">
Name: <input type = "text" name = "name">
<br />
city: <input type = "text" name = "city" />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
<%
Cookie Name = new Cookie("name", request.getParameter("name"));
Cookie city = new Cookie("city", request.getParameter("city"));
Name.setMaxAge(54*61*12);
city.setMaxAge(54*61*12);
response.addCookie( Name );
response.addCookie( city );
%>
<html>
<body>
<ul>
<li><p><b>Name:</b>
<%= request.getParameter("name")%>
</p></li>
<li><p><b>City:</b>
<%= request.getParameter("city")%>
</p></li>
</ul>
</body>
</html>
Output:
Example #2
Code:
<html>
<body>
<form action = "custom.jsp" method = "post">
Name: <input type = "text" name = "name">
<br />
city: <input type = "text" name = "city" />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
<html>
<body>
<%
Cookie c = null;
Cookie[] c1 = null;
c1 = request.getCookies();
if( c1 != null ) {
out.println("<h2> Cookies/h2>");
for (int i = 0; i < c1.length; i++) {
c = c1[i];
out.print("name : " + c.getName( ) + ", ");
out.print("city: " + c.getValue( )+" <br/>");
}
} else {
out.println("<h2>No cookies</h2>");
}
%>
</body>
</html>
Output:
Example #3
Code:
<html>
<body>
<%
Cookie c = null;
Cookie[] c1 = null;
c1 = request.getCookies();
if( c1 != null ) {
out.println("<h2>Cookies</h2>");
for (int i = 0; i < c1.length; i++) {
c = c1[i];
if((c.getName( )).compareTo("name") == 0 ) {
c.setMaxAge(0);
response.addCookie(c);
out.print("Deleted cookies: " +
c.getName( ) + "<br/>");
}
out.print("Name : " + c.getName( ) + ", ");
out.print("Value: " + c.getValue( )+" <br/>");
}
} else {
out.println(
"<h2>No cookies</h2>");
}
%>
</body>
</html>
Output:
Conclusion
The Cookies are mainly lead with the higher number of quality web applications not only in JSP cookies are accepted and followed with the servlet and JSF(Java server faces) it’s under the java community process because its integration with the standard quality application frameworks integrate with the Apache servers
Recommended Articles
This is a guide to Cookies in JSP. Here we discuss an introduction to Cookies in JSP, syntax, how to handle it, examples with code and output. You can also go through our other related articles to learn more –