A Short Guide To Learn About Cookies In Java
Posted By : Sudhakar Pandey | 30-Apr-2018
A cookie is a token which contains sub information in the form key-value pair, it is generated by the server and is made available to client So that a client sent it back to the server as part of subsequence request. It provides a simple mechanism of maintaining user information between request.
Cookies can be two types-
1) persistent Cookies
2) Non persistent cookies
1) Persistent cookies remain value for multiple session, they are stored in a text file by the browser on the client machine.
2) Nonpersistent cookies remain valid only for a single session. They are stored by the browser delete cache, they are discarded when the browser is closed. By default each cookie non- persistent.
Cookies in Java:
Servlet API provide a class name cookies for represents these(cookies) as objects. Cookies object can be created as follows--
>Public cookie(String name, String value);
Commonly used methods of cookies class-
1) getName()- is used to obtain value of cookie.
Public String getName();
2) setMaxAge()- is used to set the value for time of cookie, when valid time is associated to a cookie, the cookie become persistent.
Public void setMaxAge(int seconds);
3) addCookie()- of http servlet response is used to send a cookie as part of the response.
public void addCookie(Cookie ch);
4) getCookies()- method of http servlet request is used to received cookies which are send by browser as part of request.
Public Cookie[] getCookies();
Ex- Below java Class is used for set cookie in browser-
public class CookieSetExample extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{
String name= req.getParameter("name");
Cookie ck = new Cookie("user",name);
ck.setMaxAge(600);
response.addCookie(ck);
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("
Welcome "+name+"
"); out.close(); }
Below java Class is used for get cookies froom browser-
public class CookieGetExample extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{
String name= "Gust";
Cookie ck[] = getCookies();
if(Optional.ofNullable(ck).ifPresent())
{
name = ck[0].value();
}
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("
Welcome "+name+"
"); out.close(); }
Limitations of Cookie-
1) A cookie can be disabled in the browser, i.e. It is not reliable.
2) Persistent doesn't differentiate between user and the server.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sudhakar Pandey
Sudhakar pandey is java developer and passoinate about learning to new technology, He have exposure about spring, hibernate, REST services, Multithreading , JSP/ Servlet. On the database side He have exposure about MySql, Oracle, Postgres.