Example of Using Cookies

Estudies4you
Using Cookies

Example 7 : Using Cookies
·         index.html
<html>
<body>
<center>
<h1 align="center">Create cookie...</hI.>
<form name="CookieForm" method="post" action="Cookie.do"> Enter your name to store in cookie: <input type="text" name="name"/><br/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>

·         CreateCookie.java: This servlet create a cookie and stores it in client browser
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CreateCookie extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String value_name = request.getParameter("name");
Cookie cookie = new Cookie("FirstCookie", value_name); response.addCookie(cookie);
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.printIn("<B>FirstCookie is created with value as ");
pw. println(value_name);
pw.close();
}
}

·         ReadCookie.java: This servlet read the data that is stored in cookie
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ReadCookie extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException       {
Cookie[] content = request.getCookies();
response. setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.printIn("<B>");
for(int i = 0; i < content.length; i++)         {
String cname = content[i].getName();
String cvalue = content[i].getValue();
pw.println("Content in cookie = " + cname + "; Its value = " + cvalue);
}
pw.close();
}
}

·         web.xml
o   <servlet> tag maps your servlet class with internal name
o   <servlet-mapping> tag maps internal name with url-pattern that your invoke
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>createcookieservlet</servlet-name>
<servlet-class>CreateCookie</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>createcookieservlet</servlet-name>
<url-pattern>/Cookie.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getcookieservlet</servlet-name>
<servlet-class> ReadCookie</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getcookieservlet</servlet-name>
<url-pattern>/ReadCookie.do</url-pattern>
 </servlet-mapping>
</web-app>

javax.servlet.http Package,Interfaces of javax.servlet.http package,Classes of javax.servlet.http package,Method of HttpServletRequest Interface,Methods of HttpServletResponse Interface,Methods of HttpSession Interface,Methods of HttpSessionBindingListener Interface,Reading Initialization Parameters,Reading Servlet Parameters,Methods of ServletRequest Interface,Methods of ServletResponse Interface,ServletResponse Interface,ServletRequest Interface,Methods of Servlet Interface,Methods of ServletConfig Interface,Methods of ServletContext Interface,ServletContext Interface,ServletConfig Interface,Servlet Interface,How to install Tomcat,How to install Tomcat webserver,Instructions to install and configure the Tomcat,steps to install tomcat webserver,process to install tomcat webserver,tomcat webserver installation,tomcat webserver installation steps,tomcat webserver installation steps,estudies4you,Web Technologies,JNTUH R16 Web Technologies Lecture notes pdf,Web Technologies class room notes pdf,Web Technologies notes unit wise,Web Technologies course file,Web Technologies previous questio papers,Web Technologies old question papers,Web Technologies MCQ,jntuh r16 Web Technologies syllabus pdf,Web Technologies study material,examples of tomcat server,example of tomcat server with program,Servlet that checks user details,Interfaces of javax.servlet package,javax.Servlet Package,API to develop Servlet,Handling Http Request & Responses,Using Cookies Session Tracking in web technologies,examples of Using Cookies-Session Tracking,web technologies session tracking,web technologies cookies session tracking
To Top