Implementing Cookie using Servlets

A cookie is a piece of data passed between a Web server and a Web browser. The Web server sends a cookie that contains data it requires th...


A cookie is a piece of data passed between a Web server and a Web browser. The Web server sends a cookie that contains data it requires the next time the browser accesses the server. This is one way to maintain state between a browser and a server.

The CookieServlet (See exaample) demonstrates a servlet which gets and sets a cookie stored at a client. Initially, the browser may not have sent the cookie as part of the request, (for example, the first time it is called), so we just initialize a local calledCount variable to 0. If we are able to get this cookie from the request, we set the local calledCount to the value of the cookie.

The servlet first tries to get the calledCount by iterating through the cookies it received as part of the request. If no cookie contains the calledCount item, then the servlet initializes the calledCount value to 0. This value is then incremented, and a new cookie instance is created for calledCount and added to the response.

If we call this servlet from a URL, we find that the first time we call it, the calledCount is 0. Subsequent calls to the same servlet from this Web browser will show that we keep incrementing the counter, and storing it into the cookie sent back to the browser.

This is one way by which we can maintain state between the Web browser and the server. The major drawback with cookies is that most browsers enable the user at the client machine to deactivate (not accept) cookies.

Example- State tracking using cookies
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CookieServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
int calledCount = 0;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>CookiesServlet</TITLE><BODY>");
out.println("<H2>Servlet Cookie Example:</H2><HR>");
if (getReqCookie(req, out, "calledCount") == null)
calledCount = 0;
else
calledCount = new Integer(getReqCookie(req, out,
"calledCount")).intValue();
out.print("The value of the cookie calledCount sent in on the request: ");
if (calledCount == 0) out.println
("null - value not sent in on request<HR>");
else out.println(calledCount + "<HR>");
calledCount++;
Cookie cookie = new Cookie("calledCount",
new Integer(calledCount).toString());
res.addCookie(cookie);
out.println("The value of the cookie calledCount set on the response: " +
calledCount);
out.println("</BODY><HTML>");
out.close();
}
private String getReqCookie(HttpServletRequest req, PrintWriter out,
String name) {
Cookie[] cookies = req.getCookies();
if (cookies != null && cookies.length > 0) {
for(int i=0; i<cookies.length; i++) {
if (cookies[i].getName().equals(name))
return (cookies[i].getValue());
}
}
return null;
}
}
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: Implementing Cookie using Servlets
Implementing Cookie using Servlets
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/cookie-servlet.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/cookie-servlet.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content