URDINESH: Cookies in Java
Showing posts with label Cookies in Java. Show all posts
Showing posts with label Cookies in Java. Show all posts

Saturday, July 11, 2015

JAVA


Tuesday, May 20, 2014

JAVA CODE FOR SESSION TRACKING USING COOKIES


COOKIE1.JAVA

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Cookie1 extends HttpServlet
{
   public void service(HttpServletRequest req,HttpServletResponse res)
   throws ServletException,IOException
          {
                        res.setContentType("text/html");
                        PrintWriter pw=res.getWriter();
                        String color=req.getParameter("s1");
                        pw.println("<html><body bgcolor="+color+">");
                        pw.println("<h1> <center> Session");
pw.println("<form method=get action=\"http://localhost:8080/servlet/Cookie2\">");
                        pw.println("Name : <input name=name type=text> <br>");
                        pw.println("<input name=bcolor type=hidden value="+color+"><br>");
                        pw.println("<input name=Submit type=submit value=submit>");
                        pw.println("<input name=Reset type=reset value=reset>");
                        Cookie c1= new Cookie("cd",color);
                         Cookie  c2 = new Cookie("id","second page");
                          res.addCookie(c1);
                        res.addCookie(c2);
                        pw.println("</body></html>");
                        pw.close();
          }  
}
           

COOKIE2.JAVA

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Cookie2 extends HttpServlet
{
   public void doGet(HttpServletRequest req,HttpServletResponse res)
   throws ServletException,IOException
          {
                        res.setContentType("text/html");
                        PrintWriter pw=res.getWriter();
                        String color=req.getParameter("bcolor");
                        pw.println("<html><body bgcolor="+color+">");
                        Cookie c[]=req.getCookies();
                        String s1[]= new String[c.length];
                        String v1[]= new String[c.length];
                        for(int i=0;i<c.length;i++)
                        {
                                    s1[i]=c[i].getName();
                                    v1[i]=c[i].getValue();
                                    pw.println(s1[i]+"</br>");  
                                    pw.println(v1[i]+"</br>");
                        }   
                        String name=req.getParameter("name");
                        pw.println("<h1> Welcome "+name+"! </h1>");
                        pw.println("</body></html>");
                        pw.close();
          } 
}

            

Followers