Servlet Cookies Tutorial

 

Using  Cookie in Java Servlet

 

 

ServletsHome

 

e-trainings-servlet-cookie-example
What is Cookie?

The Server stores client information in a client machine.

So that, next time,  the server can identify the client with help of cookie information that is created previously.

Cookies are little packets of information that are stored locally on a user system when  he visits a same website next time, then the site uses them. Cookies are  part of HTTP header, so cookies must be set before sending any value to the browser (i.e. before HTML tag).

 

[ Cookies can be useful to store identifiable information about a user on their system. So that the website can find it later, even after your computer had turned off, and you may continue from where you  stopped. ]

This tutorial  going to  explain to you how to set, read and remove cookies from inside Java servlets. To deal with cookies, there is a class called Cookie. It is available in  Class javax.servlet.http package. To use it, import this class first and use the following syntax for creating the cookie.

import javax.servlet.http.Cookie

Syntax:


Cookie c = new Cookie(name, value); -------> craeting cookie
Cookie[] cookies = request.getCookies(); ----getting cookies 

Methods
 
getName()
getValue();



Creating cookie


Source Code for Cookie
 
Cookie Example:


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CookieDemo extends HttpServlet {
     
public void doGet( HttpServletRequest request,
    HttpServletResponse response )throws ServletException, IOException {
 
  PrintWriter out; 
 
 response.setContentType("text/html");

out = response.getWriter(); 

// Creating Cookies

Cookie cookie1 = new Cookie("userid","999");
cookie1.setMaxAge(1 * 60 * 60);  // 1 hour. );     
response.addCookie(cookie1); 

				 
Cookie cookie2 = new Cookie("Name","John");
cookie2.setMaxAge(24 * 60 * 60);  // 24 hours. );     
response.addCookie(cookie2); 

out.println ("<h1> Cookie are Created </h1>");
out.close();
  
    }
} 

 

 URL:

http://localhost:8080/Webapp/CookieDemo

 

Output: 


e-trainings-servlet-cookie-demo




Reading All the  Cookies in Java Servlet

You can read the cookies via the HttpServletRequest that are sent From the Browser

 

 

Example:


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Readcookiesdemo extends HttpServlet {
 
 
 public void doGet( HttpServletRequest request,
    HttpServletResponse response )throws ServletException, IOException {
 
 
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();

// print out cookies

Cookie[] cookies = request.getCookies();

for (int i = 0; i < cookies.length; i++) {
    Cookie c = cookies[i];
    String name = c.getName();
    String value = c.getValue();
    out.println(name + " = " + value + "<br>");
}
 
 
  out.close();
    }
} 
 
 
Output: 
 
coding-zon-servlet-cookie-read

 
 
 
 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Shoppingcart using PHP Sessions - Miniproject

Export Data to Excel with PHP and MySQL