A cookie is a small piece of records that is continued among a couple of client requests.
A cookie has a identity, a single value, and non-obligatory attributes together with a comment, course and domain qualifiers, a most age, and a model number.
By default, every request is taken into consideration as a new request. In cookies technique, we upload cookies with reaction from the servlet. So the cookie is saved withinside the cache of the browser. After that if the request is dispatched via way of means of the consumer, cookie is brought with request via way of means of default. Thus, we apprehend the consumer because the antique consumer.
Two types of Cookies are there in Servlets that are:
1.Non Persistent Cookies
2.Persistent Cookies
It is legitimate for a single session only. It is eliminated every time the user closes the browser.
It is legitimate for more than one session . It isn't eliminated every time the user closes the browser. It is eliminated most effectively if user logout or signout.
Javax.servlet.http.Cookie class presents the capability of the usage of cookies. It presents a whole lot of beneficial techniques for cookies.
Constructor | Description |
Cookie() | Establish a cookie. |
Cookie(String name, String value) | Establish a cookie with a defined name and value. |
Some commonly used methods of the Cookie class are Given Below:
Method | Description |
public void setMaxAge(int expiry) | Sets the utmost age of the cookie in seconds. |
public String getName() | Come Back the name of the cookie. The name cannot be alter after creation. |
public String getValue() | Come back the value of the cookie. |
public void setName(String name) | alter the name of the cookie. |
public void setValue(String value) | Comes back the value of the cookie. |
We need some ways provided by other interfaces for adding cookies or getting the value from the cookie. They are:
Simple code to create a cookie is here let’s see:
Cookie ck=new Cookie("user","sonoo jaiswal");//creating cookie object response.addCookie(ck);//adding cookie in the response
It is mainly used to logout or sign out the user. Let’s see Simple code to create a cookie is here
Cookie ck=new Cookie("user","");//deleting value of cookie ck.setMaxAge(0);//changing the maximum age to 0 seconds response.addCookie(ck);//adding cookie in the response
Easy code to get the cookies is here let’s see
Cookie ck[]=request.getCookies(); for(int i=0;i<ck.length;i++){ out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());//printing name and value of cookie }
In this example, we are storing the name of the user within the cookie object and accessing it in another servlet. As we all know well that session corresponds to the actual user. So if you access it from too many browsers with different values, you'll get various values.
Index.html
<form action="servlet1" method="post"> Name:<input type="text" name="userName"/><br/> <input type="submit" value="go"/> </form>
FirstServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String n=request.getParameter("userName"); out.print("Welcome "+n); Cookie ck=new Cookie("uname",n);//creating cookie object response.addCookie(ck);//adding cookie in the response //creating submit button out.print("<form action='servlet2'>"); out.print("<input type='submit' value='go'>"); out.print("</form>"); out.close(); }catch(Exception e){System.out.println(e);} } }
SecondServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SecondServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); Cookie ck[]=request.getCookies(); out.print("Hello "+ck[0].getValue()); out.close(); }catch(Exception e){System.out.println(e);} } }
Web.xml
<web-app> <servlet> <servlet-name>s1</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>s1</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> <servlet> <servlet-name>s2</servlet-name> <servlet-class>SecondServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>s2</servlet-name> <url-pattern>/servlet2</url-pattern> </servlet-mapping> </web-app>
Output