0% found this document useful (0 votes)
11 views

Using Cookies

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Using Cookies

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT – IV

SERVLET
Using Cookies
Using Cookies
Definition: Cookies are some little information that can be left on your
computer by the other computer when we access an internet.
• Generally this information is left on your computer by some
advertising agencies on the internet. Using the information stored in
the cookies these advertising agencies can keep track of your internet
usage, liking of users.
• For the applications like on-line purchase systems once you enter your
personal information such as your name or your e-mail ID then it can
be remembered by these systems with the help of cookies.
• Sometimes cookies are very much dangerous because by using
information from your local disk some malicious data may be passed
to you. So it is upto you how to maintain your own privacy and
security.
Constructors and Methods
1. The cookie class is used to create cookies in servlet.
2. The syntax for constructor for cookies are.
oCookie()
oCookie(String name, String value)
Method Description
public String getName() It returns the name of the cookie.
public String getValue() It returns the value of the cookie.
public String setName() It sets or changes the name of the cookie.
public String setValue() It sets or changes the value of the cookie.
public void addCookie(Cookie c) The cookie is added in the response object of HttpServletResponse
interface.
public Cookie[] getCookie() All the cookies can be returned using this method with the help of
HttpServletRequest interface.
The servlet following steps are used to support for cookies
Step 1 : Creation of Cookies :
The cookie can be created in Servlet and added to response object using addCookie method
Cookie cookie=new Cookie(“user”,”XYZ”);//Creating cookie object
response.addCookie(cookie);
Step 2 : Reading Cookies :
The value from the cookie can be obtained using the getName() and getValue()
methods.
cookie[] my_cookies = req.getCookies();
int n = my_cookies.length;
for(int i=0;i<n;i++)
{
String name = my_cookies[i].getName();
String value = my_cookies[i].getValue();
}
The servlet following steps are used to support for cookies
Example: A Simple HTML form in which a servlet is invoked. This servlet
creates a cookie by the name My_Cookie and stores the value entered
by you in the textbox of HTML form. You can further get the further get
the information stored in the cookie by another servlet program
getCookieServlet.
Hence we will write three programs –
1. Our normal HTML script in which some value is entered in the
textbox.
2. The servlet program named mycookie servlet which will set a
cookies and take the value entered by you in the HTML form.
3. The another servlet program named get CookeServlet which helps
us to view the cookie.
HTML Program
<html>
<head>
<title> Demo of Cookie</servlet>
</head>
<body>
<form name=“form1 method=“post”
action=http://localhost:8080/examples/example/mycookieservlet>
<h3> Enter the value for my Cookie </h3>
<input type=submit value=“Submit”>
</form>
</body>
</html>
Servlet Program [mycookeservlet.java]
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class mycookieservlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String txt_data = req.getParameter(“txt_data”);
//Create cookie.
Cookie cookie = new Cookie(“My_Cookie”, txt_data);
Servlet Program [mycookeservlet.java]
//Adding cookie to HTTP response.
res.addCookie(cookie);
res.setContentType(“text/html”);
PrintWriter out = res.getWriter();
out.println(“<h2>MyCookie has been set to:”);
out.println(txt_data);
out.println(“<br><br><br>”);
out.println(“This page shows that the cookie has been added”);
out.close();
}
}
Cookie Servlet Program
[mycookeservlet.java]
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException
{
Cookie[] my_cookie = req.getCookies();
res.setContentType(“text/html”);
PrintWriter out=res.getWriter();
out.println(“<b>”);
Cookie Servlet Program
[mycookeservlet.java]
int n = my_cookie[i].length;
for(int i=0;i<n;i++)
{
String name = my_cookies[i].getName();
String value = my_cookies[i].getValue();
out.println(“name=“+name);
out.println(“and value=“+value);
}
out.close();
}
}
Thank you…

You might also like