Using Cookies
Now, let's develop a servlet that illustrates how to use cookies. The servlet is invoked
when a form on a Web page is submitted. The example contains three files, as
summarized here:
File Description
AddCookie.htm
Allows a user to specify a value for the cookie named
M yCookie
AddCookieServlet.java
P rocesses the submission of AddCookie.htm
GetCookiesServlet.java
D
isplays cookie values
The HTML source code for AddCookie.htm is shown in the following listing. This page
contains a text field in which a value can be entered. The page also includes a submit
button. When this button is pressed, the value in the text field is sent to
AddCookieServlet via an HTTP POST request.
<html>
<body>
<center>
<form name="Form1"
method="post"
action="http://localhost:8080/servlet/AddCookieServlet">
<B>Enter a value for MyCookie:</B>
<input type=textbox name="data" size=25 value="">
<input type=submit value="Submit">
</form>
</body>
</html>
The source code for AddCookieServlet.java is shown in the following listing. It gets the
value of the parameter named "data". It then creates a Cookie object that has the name
"MyCookie" and contains the value of the "data" parameter. The cookie is then added to
the header of the HTTP response via the addCookie( ) method. A feedback message is
then written to the browser.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AddCookieServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Get parameter from HTTP request
String data = request.getParameter("data");
Cookie cookie = new Cookie("MyCookie", data);
// Add cookie to HTTP response
response.addCookie(cookie);
// Write output to browser
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>MyCookie has been set to");
pw.println(data);
pw.close();
}
}
The source code for GetCookiesServlet.java is shown in the following listing. It invokes
the getCookies( ) method to read any cookies that are included in the HTTP GET
request. The names and values of these cookies are then written to the HTTP response.
Observe that the getName( ) and getValue( ) methods are called to obtain this
information.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetCookiesServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Get cookies from header of HTTP request
Cookie[] cookies = request.getCookies();
// Display these cookies
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>");
for(int i = 0; i < cookies.length; i++) {
String name = cookies[i].getName();
String value = cookies[i].getValue();
pw.println("name = " + name +
"; value = " + value);
}
pw.close();
}
}
The following servlet illustrates how to use session state. The getSession( ) method gets
the current session. A new session is created if one does not already exist. The
getValue( ) method is called to obtain the object that is bound to the name "date". That
object is a Date object that encapsulates the date and time when this page was last
accessed. (Of course, there is no such binding when the page is first accessed.) A Date
object encapsulating the current date and time is then created. The putValue( ) method
is called to bind the name "date" to this object.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DateServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Get the HttpSession object
HttpSession hs = request.getSession(true);
// Get writer
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.print("<B>");
// Display date/time of last access
Date date = (Date)hs.getValue("date");
if(date != null) {
pw.print("Last access: " + date + "<br>");
}
// Display current date/time
date = new Date();
hs.putValue("date", date);
pw.println("Current date: " + date);
}}
Source Code for Cookie Example
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CookieExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// print out cookies
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
Cookie c = cookies[i];
String name = c.getName();
String value = c.getValue();
out.println(name + " = " + value);
}
// set a cookie
String name = request.getParameter("cookieName");
if (name != null && name.length() > 0) {
String value = request.getParameter("cookieValue");
Cookie c = new Cookie(name, value);
response.addCookie(c);
}
}
}
OUTPUT:
Cookies Example
Your browser is sending the following cookies:
Cookie Name: ANANTH
Cookie Value: 876543
Cookie Name: RAM
Cookie Value: 1234567
You just sent the following cookie to your browser:
Name: GURUNANAK
Value: 5000060
Create a cookie to send to your browser
Name:
Value:
Write web program to implement Hello word
<Web-app>
<servlet>
<servlet-name>simple servlet </servlet-name>
<servlet-class> Hello Word</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simple servlet</servlet-name>
<un-pattern>servlet<un-pattern>
</Web-app>
OUTPUT : Hello Word
No comments:
Post a Comment