0% found this document useful (0 votes)
61 views2 pages

Java JSP Cookies: Store Names

This document provides code to implement cookies to store a user's first and last name using Java server pages. A form page allows input of first and last name, which are then stored in cookies and retrieved on another page.

Uploaded by

Rk Gaming
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)
61 views2 pages

Java JSP Cookies: Store Names

This document provides code to implement cookies to store a user's first and last name using Java server pages. A form page allows input of first and last name, which are then stored in cookies and retrieved on another page.

Uploaded by

Rk Gaming
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

FACULTY OF ENGINEERING AND TECHNOLOGY

Department of Computer Engineering


Advanced Java Programming (3160707)

Experiment 8: Implement cookies to store firstname and lastname using Java server pages.

[Link]

<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>
</head>
<body>
<form action="[Link]" method="post">
First Name: <input type="text" name="firstName"><br>
Last Name: <input type="text" name="lastName"><br>
<input type="submit" value="Submit">
</form>
</body>

[Link] code:

<%@ page import="[Link]" %>


<%@ page import="[Link]" %>
<%
String firstName = [Link]("firstName");
String lastName = [Link]("lastName");

Cookie firstNameCookie = new Cookie("firstName", [Link](firstName,


"UTF-8"));
Cookie lastNameCookie = new Cookie("lastName", [Link](lastName, "UTF-
8"));

[Link](3600); // Set cookie age in seconds


[Link](3600); // Set cookie age in seconds

[Link](firstNameCookie);
[Link](lastNameCookie);

[Link]("[Link]");
%>

[Link] code:

<%@ page import="[Link]" %>


<%@ page import="[Link]" %>
<%
String firstName = "";
String lastName = "";

Cookie[] cookies = [Link]();


if (cookies != null) {
for (Cookie cookie : cookies) {
if ([Link]().equals("firstName")) {
firstName = [Link]([Link](), "UTF-8");
} else if ([Link]().equals("lastName")) {
lastName = [Link]([Link](), "UTF-8");
}
Enrollment: 210570107012 Batch–6EC2 A
FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
Advanced Java Programming (3160707)

}
}
%>
<!DOCTYPE html>
<html>
<head>
<title>Show Name</title>
</head>
<body>
<h2>First Name: <%= firstName %></h2>
<h2>Last Name: <%= lastName %></h2>
</body>
</html>

Output :

(Web page Output)

Enrollment: 210570107012 Batch–6EC2 A

You might also like