Cookies are small text files that are stored by an application server in the client
browser to keep track of all the users. A cookie has values in the form of name/value
pairs. For example, a cookie can have a name, user with the value, Michael. They are
created by the server and are sent to the client with the HTTP response headers. The
client saves the cookies in the local hard disk and sends them along with the HTTP
request headers to the server. A Web browser is expected to support 20 cookies per
host and the size of each cookie can be a maximum of 4 bytes each. Various
characteristics of cookies are:
Coockie.html
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="cookieServlet" method="post">
Name:<input type="text" name="userName"/><br/>
password:<input type="password" name="userPass"/><br/>
<input type="submit" value="go"/>
</form>
</body>
</html>
cookieServlet.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Code;
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;
/**
*
* @author Dilip
*/
public class cookieServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String n=request.getParameter("userName");
String p=request.getParameter("userPass");
out.println("Welcome "+n);
Cookie ck=new Cookie("uname",n);//creating cookie object
ck.setMaxAge(120);
response.addCookie(ck);//adding cookie in the response
Cookie ck1=new Cookie("upass",p);//creating cookie object
ck1.setMaxAge(120);
response.addCookie(ck1);
//creating submit button
out.print("Cookie containing user name and password is stored in your browser.");
out.print("<form action='cookieServlet1' method='post'>");
out.print("<input type='submit' value='go'>");
out.print("</form>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
cookieServlet1.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Code;
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;
/**
*
* @author Dilip
*/
public class cookieServlet1 extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String username=null;
String userpass=null;
Cookie ck[]=request.getCookies();
//out.print("Hello "+ck[0].getValue());
if (ck!=null)
{
for (int i=0; i<ck.length; i++)
{
/* Retrieve username from the cookie. */
if (ck[i].getName().equals("uname"))
{
username = ck[i].getValue();
out.println("User Name : "+username);
}
if (ck[i].getName().equals("upass"))
{
userpass = ck[i].getValue();
out.println(" Password : "+userpass);
}
}
}
else
{
out.println("No cookies found");
}
}catch(Exception e)
{
out.println("Error"+e.toString());
}
finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Output
Comments
Post a Comment