This site javatpoint is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.

Monday, 14 October 2019

COOKIES IN JAVA

Cookies in java

HTTP protocol  is a  stateless.This mean that every HTTP request is different fro others. Sometimes it is necessary to keep track of  the sequence of related request sent by by a client to perform some task this  process is called session tracking . Cookies are one of the technique of  the session tracking.
A Cookie is a [key value] form of key-value  pair created by the server and is installed in the client browser when the client ,makes a request for the first time.A browser is  also maintains the list of cookies install in it and send it to the server as a part of subsequent HTTP request.
A cookie is represented by using the javax.servlet.http.Cookie  class and created using the following constructor.
Cookies(String key, String value) .

A cookie is added by the help of addCookie() method of the HttpServletResponse class. . Similarly ,  server can get all the cookies sent by the browser by using getCookies() method of HttpServletRequest class. There are two types of cookies are available.  Non-persistent cookie and persistent cookies. A non persistent cookie  valid for a single session and it  removes each time the user close the browser . A persistent cookies however is valid for multiple sessions and it is removed when the user close the browser. A cookie is defined as a small piece of information.

Example:In this example we will transfer  data from one file to another file using cookies in Servlets in java.

Components: 

1. cookie.html 
2 First servlet.java
3. Second servlet.java


1.Cookie.html File:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method="get" action="./FirstServlet">
UserName:<input type="text" name="uname"><br><br>
Password:<input type="password" name="upwd"><br><br>
<input type="submit" value="submit">
</form>

</body>
</html>



2. FirstServlet.java File:



package com.coo;



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

    
    public FirstServlet() {
       
    }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html");
try
{
PrintWriter pw=response.getWriter();
pw.println("<br>");
String uname= request.getParameter("uname");
String upwd= request.getParameter("upwd");
pw.println("userName:"+ uname);
pw.println("<br>");
Cookie ck1=new Cookie("user1",uname);
Cookie ck2=new Cookie("user2",upwd);

pw.println("password:"+ upwd);
response.addCookie(ck1);
response.addCookie(ck2);
pw.println("<form action='./SecondServlet' method='get'>");
pw.println("<input type='submit'  value='submit'>");
pw.println("</form>");
pw.close();
}
catch(IOException e)
{
}
}


}


3.SecondServlet.java File:




package com.dj;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SecondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    
    public SecondServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
try {
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
Cookie ck[]=request.getCookies();
pw.println("userName:"+ ck[0].getValue());
Cookie ck1[]=request.getCookies();
pw.println("<br>");
pw.println("password:"+ck[1].getValue());
pw.close();

}
catch (Exception e) {
}
}

}


Output:

When we enter the data in the  FirstServlet.java  file then it will transfer the data into Second Servlet.java' file by  using cookies. 




























































Adbox