Servlet Develpment Using Cookies
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:
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.
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.
The source code for GetCookiesServlet.java is shown in following listing. It invokes the getCookies() method to read any cookies that are included in the HTTP GET request. The name and values of these cookies are then written to the HTTP response. Observe that the getName() and getValue() method are called to obtain this information.
Compile the servlet, Next, copy it to the appropriate directory,and update the web.xml file, as previously described. then, perform these steps to text this example.
1. Start Tomcat (if is not already running).
2. Display AddCookie.html in Browser.
3. Enter a value for MyCookie.
4. Submit the web page.
After following these steps, you will observe that a feedback message is displayed by the browser.
File | Description |
AddCookie.html | Allow 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.
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.
The source code for GetCookiesServlet.java is shown in following listing. It invokes the getCookies() method to read any cookies that are included in the HTTP GET request. The name and values of these cookies are then written to the HTTP response. Observe that the getName() and getValue() method are called to obtain this information.
Compile the servlet, Next, copy it to the appropriate directory,and update the web.xml file, as previously described. then, perform these steps to text this example.
1. Start Tomcat (if is not already running).
2. Display AddCookie.html in Browser.
3. Enter a value for MyCookie.
4. Submit the web page.
After following these steps, you will observe that a feedback message is displayed by the browser.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment
Please Write a Message for Programming or something Related issues.