Thursday 2 March 2017

Cookies in Servlet.

*What is Cookies?

~cookie is a small piece of information that is persisted between the multiple client requests.


*Method of Cookies.

~public void getName()
:-return the name of the Cookie The Name Cannot be change after Creation.

~public String getValue()
:-return the value of Cookie.

~public void setValue(String name)
:-Change the name of the Cookie.

~public void setValue(String Value)
:-Change the value of the Cookie.

~public void setDomain(String pattern)
:-This method sets the domain to which Cookies applies.

~public String getDomain()
:-This method gets the domain to which Cookie applies.

~public void setMaxAge(int expiry)
:-This method sets how mach time (in seconds) should elapse before the Cookie expires.If you don't set this the cookie will last only for the current session.

~public int getMaxAge()
:-This Method return the maximum age of the cookie ,specified in second,by Default,-1,indicating the cookie will persist until browser shutdown.

~public String getPath()
:-This method gets the path to which this Cookie applies.

~public void setpath(String uri)
:-This method sets the path to which this Cookie applies.if you don't specify a path the cookie is return for all URLS in the same directory as the current page as well as all subdirectories.

~public void setSecure(boolean flag)
:-This method sets the boolean indicating whether cookie should only be set over encrypted connection.



*Other methods required for using Cookies


~public void addZCookie(cookie ck)
:-method of httpServletResponse interface is used to add Cookie in response object.

~public Cookie[] getCookies()
:-Method of httpServletRequest is used to return all the Cookied Form the Browser.

*Constructor.

~Cookie() 
:-Contructor a Cookie.

~Cookie(String name,String value)
:-Contruct a cookie with Specified name and value.



*Example:

index.html


<html>
    <head></head>
    <body>
         <form action="servlet1" method="post">  
            Name:<input type="text" name="userName"/><br/>  
            <input type="submit" value="go"/>  
        </form> 
    </body
</html>



servlet1.java


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 servlet1 extends HttpServlet {

       protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            
           
                response.setContentType("text/html");
                PrintWriter out=response.getWriter();
                
                String name=request.getParameter("userName");
                
                Cookie ck=new Cookie("un",name);
                response.addCookie(ck);
                
              out.print("<form action='servlet2' method='post'>");  
    out.print("<input type='submit' value='go'>");  
    out.print("</form>");  
                
                out.close();
    }
}


Servlet2.java



import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/servlet2"})
public class servlet2 extends HttpServlet {

     protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        
        Cookie ck[]=request.getCookies();
        out.println("hellow"+ck[0].getValue());
        out.close();
      
    }
 }


  output



Second Page

hellow queue




Thanks ;)
Happy Coding 










1 comment: