0% found this document useful (0 votes)
149 views4 pages

Cookies Example in Servlets: A) Index - HTML

This document discusses cookies in servlets using three Java classes - index.html, CookieCreate.java, and CookieRead.java. Index.html contains a form to input code and model data. CookieCreate.java creates cookies storing the code and model on form submit, setting the model cookie to expire in 10 minutes. CookieRead.java reads all cookies and prints the name and value of each cookie, or indicates no cookies found. The document provides an example of creating, setting attributes, and reading cookies in servlets.

Uploaded by

Joythi Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views4 pages

Cookies Example in Servlets: A) Index - HTML

This document discusses cookies in servlets using three Java classes - index.html, CookieCreate.java, and CookieRead.java. Index.html contains a form to input code and model data. CookieCreate.java creates cookies storing the code and model on form submit, setting the model cookie to expire in 10 minutes. CookieRead.java reads all cookies and prints the name and value of each cookie, or indicates no cookies found. The document provides an example of creating, setting attributes, and reading cookies in servlets.

Uploaded by

Joythi Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

-by RAGHU SIR [ SATHYA TECHNOLOGIES , AMEERPET, HYDERABAD]

Cookies Example in Servlets

a) index.html:-
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Welcome to Cookies Creation Page </h1>
<form action="create" method="post">
<pre>
CODE : <input type="text" name="code"/>
MODEL : <input type="text" name="model"/>
<input type="submit" value="Create"/>
</pre>
</form>
</body>
</html>

b)CookieCreate.java:-

package com.app;

import java.io.IOException;
import java.io.PrintWriter;

1|P ag e
-by RAGHU SIR [ SATHYA TECHNOLOGIES , AMEERPET, HYDERABAD]

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/create")
public class CookieCreate
extends HttpServlet
{

@Override
protected void doPost(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException {
//1. read form data
String code=req.getParameter("code");
String model=req.getParameter("model");

//2. create Cookies


Cookie c1=new Cookie("pcode", code);
Cookie c2=new Cookie("pmodel", model);

//3. set Lifetime 10 mins


c2.setMaxAge(600);

//4. send to Browser


resp.addCookie(c1);
resp.addCookie(c2);

//5. print message


PrintWriter out=resp.getWriter();

2|P ag e
-by RAGHU SIR [ SATHYA TECHNOLOGIES , AMEERPET, HYDERABAD]

out.println("Cookies Created !!check


browser!!");

}
}

c)CookieRead.java:-
package com.app;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/view")
public class CookieRead
extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException {
//1. read all cookies from req

Cookie[] cks=req.getCookies();

3|P ag e
-by RAGHU SIR [ SATHYA TECHNOLOGIES , AMEERPET, HYDERABAD]

//2. print cookies


PrintWriter out=resp.getWriter();

if(cks!=null && cks.length>0) {


for(Cookie c:cks) {

out.println(c.getName()+"="+c.getValue());
}
}else {
out.println("No Cookies found");
}

FB:

https://www.facebook.com/groups/thejavatemple
Email:
[email protected]

4|P ag e

You might also like