Join us on Facebook

Please wait..10 Seconds Cancel

11.28.2013

// // Leave a Comment

Create two cookie from one servlet and display it's all information on browser. After 1 hour that cookie will expired.

Cookie

public class Cookie
extends java.lang.Object
implements java.lang.Cloneable

Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.
A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. Some Web browsers have bugs in how they handle the optional attributes, so use them sparingly to improve the interoperability of your servlets.
The servlet sends cookies to the browser by using the HttpServletResponse.addCookie(javax.servlet.http.Cookie) method, which adds fields to HTTP response headers to send cookies to the browser, one at a time. The browser is expected to support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each.
The browser returns cookies to the servlet by adding fields to HTTP request headers. Cookies can be retrieved from a request by using the HttpServletRequest.getCookies() method. Several cookies might have the same name but different path attributes.
Cookies affect the caching of the Web pages that use them. HTTP 1.0 does not cache pages that use cookies created with this class. This class does not support the cache control defined with HTTP 1.1.

Register.java

package view; 
import java.io.*; 
import java.net.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
public class Register extends HttpServlet  
{ 
    protected void processRequest(HttpServletRequest request, HttpServletResponse 
response) 
    throws ServletException, IOException { 
        response.setContentType("text/html;charset=UTF-8"); 
        PrintWriter out = response.getWriter(); 
        try 
          { 
            out.println("<html>"); 
            out.println("<head>"); 
            out.println("<title>Servlet Register</title>");   
            out.println("</head>"); 
            out.println("<body>"); 
            out.println("<h1>Please fill the details:</h1>"); 
            out.println("<form action=Validate method=GET>"); 
            out.println("<table>" + 
                    "<tr>" + 
                    "<td>Name</td><td><input type=text name=name></input></td></tr>" + 
                    "<tr>" + 
                    "<td>Surname</td><td><input type=text 
name=surname></input></td></tr>" + 
                    "<tr>" + 
                    "<td>City</td><td><input type=text name=city></input></td></tr>" + 
                    "<tr>" + 
                    "<td>State</td><td><input type=text name=state></input></td></tr>" 
+ 
                    "<tr>" + 
                    "<td></td><td><input type=reset 
value=Reset></input>&nbsp&nbsp<input type=submit  Value=Submit></input></td>" + 
                    "</tr>" + 
                    "</table>"); 
            out.println("</form>"); 
            out.println("</body>"); 
            out.println("</html>"); 
        }  
       finally{  
            out.close(); 
        } 
    }  
   protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException  
    { 
        processRequest(request, response); 
    }  
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException  
    { 
        processRequest(request, response); 
    } 
    public String getServletInfo()  
    { 
        return "Short description"; 
    } 
} 

show.java

 
package view; 
 
import java.io.*; 
import java.net.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
public class Show extends HttpServlet 
{       
protected void processRequest(HttpServletRequest request, HttpServletResponse 
response) 
throws ServletException, IOException 
{ 
        response.setContentType("text/html;charset=UTF-8"); 
        PrintWriter out = response.getWriter(); 
        try { 
            Cookie[] c=request.getCookies(); 
            out.println("<html>"); 
            out.println("<head>"); 
            out.println("<title>Servlet Show</title>");   
            out.println("</head>"); 
            out.println("<body>"); 
            if(c.length == 1) 
            { 
                out.println("<h4>The information you enter only lasts " + 
                        "10 seconds in history.</h4>"); 
                out.println("Please <a href=\"Register\">Click Here</a> to submit 
again."); 
            } 
            else 
            { 
                out.println("<h3>Your Information is:</h3>"); 
                out.println("<table>" + 
                    "<tr>" + 
                    "<td>Name</td><td>"+c[1].getName()+"</tr>" + 
                    "<tr>" + 
                    "<td>Surname</td><td>"+c[1].getValue()+"</td></tr>" + 
                    "<tr>" + 
                    "<td>City</td><td>"+c[2].getName()+"</td></tr>" + 
                    "<tr>" + 
                    "<td>State</td><td>"+c[2].getValue()+"</td></tr>" + 
                    "</table>"); 
                out.println("<br /><a href=\"Show\">Click Here</a> to refresh page."); 
                out.println("<h4>The information you enter only lasts " + 
                        "10 seconds in history.</h4>"); 
                out.println("Please <a href=\"Register\">Click Here</a> to submit 
again."); 
            } 
            out.println("</body>"); 
            out.println("</html>"); 
 
        } finally {  
            out.close(); 
        } 
    }  
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException 
    { 
        processRequest(request, response); 
    }  
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException 
    { 
        processRequest(request, response); 
    } 
 
    public String getServletInfo() 
    { 
        return "Short description"; 
    } 
} 

Validate.java

 
package control; 
 
import java.io.*; 
import java.net.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
public class Validate extends HttpServlet { 
    
protected void processRequest(HttpServletRequest request, HttpServletResponse 
response) 
    throws ServletException, IOException { 
        response.setContentType("text/html;charset=UTF-8"); 
        PrintWriter out = response.getWriter(); 
        try { 
            String name=request.getParameter("name"); 
            String surname=request.getParameter("surname"); 
            String city=request.getParameter("city"); 
            String state=request.getParameter("state");             
            Cookie c1=new Cookie(name,surname); 
            Cookie c2=new Cookie(city,state); 
            c1.setMaxAge(10); 
            c2.setMaxAge(10); 
            response.addCookie(c1); 
            response.addCookie(c2);  
            out.println("<html>"); 
            out.println("<head>"); 
            out.println("<title>Servlet Validate</title>");   
            out.println("</head>"); 
            out.println("<body>"); 
            out.println("<h1>Information Stored sucessfully!!!</h1>"); 
            out.println("<br />Please <a href=\"Show\">Click Here</a> to go to next 
page."); 
            out.println("<br /> Page will be expired in 10 seconds."); 
            out.println("</body>"); 
            out.println("</html>"); 
        } finally {  
            out.close(); 
        } 
    }  
   protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException 
    { 
        processRequest(request, response); 
    }  
  
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException 
    { 
        processRequest(request, response); 
    } 
    public String getServletInfo() 
    { 
        return "Short description"; 
    } 
} 

0 comments:

Post a Comment