Home | | Web Programming | Using Cookies - Servlets

Chapter: Java The Complete Reference : Applying Java : Introducing Servlets

Using Cookies - Servlets

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.

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.html : Allows a user to specify a value for the cookie named MyCookie.

AddCookieServlet.java : Processes the submission of AddCookie.html.

GetCookiesServlet.java : Displays cookie values.

 

The HTML source code for AddCookie.html is shown in the following listing. This page contains a text field in which a value can be entered. There is also a submit button on the page. 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/examples/servlets/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");

 

// Create cookie.

 

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();

 

}

 

}

Compile the servlets. Next, copy them to the appropriate directory, and update the web.xml file, as previously described. Then, perform these steps to test this example:

 

            Start Tomcat, if it is not already running.

 

            Display AddCookie.html in a browser.

 

            Enter a value for MyCookie.

 

            Submit the web page.

 

After completing these steps, you will observe that a feedback message is displayed by the browser.

Next, request the following URL via the browser:

 

http://localhost:8080/examples/servlets/servlet/GetCookiesServlet

 

Observe that the name and value of the cookie are displayed in the browser.

 

In this example, an expiration date is not explicitly assigned to the cookie via the setMaxAge( ) method of Cookie. Therefore, the cookie expires when the browser session ends. You can experiment by using setMaxAge( ) and observe that the cookie is then saved on the client machine.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : Applying Java : Introducing Servlets : Using Cookies - Servlets |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.