Persistent Cookies allocate memory as ‘-1’ in a browser window has no expiry time. These cookies will be destroyed automatically when the browser window is closed.
Non-Persistent Cookies allocate memory as ‘positive number’ in a browser window has an expiry time, These cookies will be destroyed when expiry time is completed.
Note: Persistent cookies are stored in a text file on the clients computer. Non-Persistent cookies are stored in RAM on the client and are destroyed when the browser is closed.Session cookies are created when you create a session object. Session can be created without cookies but that make the URL look crappy.
Every cookie contains name and value as string information.
Cookies cookies = new Cookies("name", "value");
response.addCookies(cookies);
//adding cookie to response
To know max age(Expiry time) of cookies:
cookies.setMaxAge(positive value);
//setting expiry time for cookies
To modify cookies value:
cookies.setValue("name");
To read cookies value
datatype cookies[] = request.getCookies();
//read all cookies
println(“cookiename”+cookies[1].getName());
println(“cookiename”+cookies[1].getValue());
<form action="servlet1" method="get"> Name:<input type="text" name="userName"/> <input type="submit" value="go"/> </form>
Here the developer just created a text box to enter the name and also created a submit button.
web.xml
<web-app> <servlet> <servlet-name>s1</servlet-name> <servlet-class>servletcookies.DemoCookies</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>servletcookies.ShowCookieData</servlet-class> </servlet> <servlet-mapping> <servlet-name>s2</servlet-name> should be same and <strong>URL</strong> name should be match with <strong>HTML</strong> form. <url-pattern>/servlet2</url-pattern> </servlet-mapping> </web-app>
Make sure that
DemoCookies.java
package servletcookies; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DemoCookies extends HttpServlet { public void doGet(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);} } }
The getParameter() returns http request parameters. Those passed from the client to the server. The doGet() method is utilized to send parameters to an URL along with the header information.
ShowCookieData.java
package servletcookies; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ShowCookieData extends HttpServlet { public void doGet(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);} } }
Here created an object to the cookie, getCookies() Returns an array containing all of the Cookie objects the client sent with this request. This method returns null if no cookies were sent.
Output
The form page get displayed as shown below when complied the code. Where enter the message.