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

Ajp Exp 8

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)
50 views2 pages

Ajp Exp 8

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
You are on page 1/ 2

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.

Form.jsp

<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>
</head>
<body>
<form action="saveName.jsp" 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>

saveName.jsp code:

<%@ page import="jakarta.servlet.http.Cookie" %>


<%@ page import="java.net.URLEncoder" %>
<%
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");

Cookie firstNameCookie = new Cookie("firstName", URLEncoder.encode(firstName,


"UTF-8"));
Cookie lastNameCookie = new Cookie("lastName", URLEncoder.encode(lastName, "UTF-
8"));

firstNameCookie.setMaxAge(3600); // Set cookie age in seconds


lastNameCookie.setMaxAge(3600); // Set cookie age in seconds

response.addCookie(firstNameCookie);
response.addCookie(lastNameCookie);

response.sendRedirect("showName.jsp");
%>

showName.jsp code:

<%@ page import="jakarta.servlet.http.Cookie" %>


<%@ page import="java.net.URLDecoder" %>
<%
String firstName = "";
String lastName = "";

Cookie[] cookies = request.getCookies();


if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("firstName")) {
firstName = URLDecoder.decode(cookie.getValue(), "UTF-8");
} else if (cookie.getName().equals("lastName")) {
lastName = URLDecoder.decode(cookie.getValue(), "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