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

Coockies

The document contains a simple web application that allows users to enter their name, which is then stored in a cookie using a servlet. It includes two servlets: SetCookieServlet for saving the name and GetCookieServlet for retrieving and displaying the name. The application is configured with a web.xml file for servlet mapping.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Coockies

The document contains a simple web application that allows users to enter their name, which is then stored in a cookie using a servlet. It includes two servlets: SetCookieServlet for saving the name and GetCookieServlet for retrieving and displaying the name. The application is configured with a web.xml file for servlet mapping.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

<!

DOCTYPE html>

<html>

<head>

<title>Cookie Example</title>

</head>

<body>

<h2>Enter Your Name</h2>

<form action="setCookieServlet" method="POST">

<label>Your Name: </label>

<input type="text" name="userName"><br><br>

<input type="submit" value="Save Name">

</form>

</body>

</html>

import java.io.*;
import javax.servlet.*;

import javax.servlet.http.*;

public class SetCookieServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,


HttpServletResponse response) throws ServletException, IOException {

// Get the name entered by the user

String userName = request.getParameter("userName");

// Create a cookie to store the user's name

Cookie userCookie = new Cookie("userName", userName);

// Set the cookie's expiry time to 1 day (86400 seconds)

userCookie.setMaxAge(86400); // 1 day

// Add the cookie to the response

response.addCookie(userCookie);

// Set content type for response

response.setContentType("text/html");

// Output response

PrintWriter out = response.getWriter();

out.println("<html><body>");

out.println("<h2>Hello, " + userName + "! Your name has been


saved in a cookie.</h2>");

out.println("<a href='getCookieServlet'>Click here to see your saved


name.</a>");

out.println("</body></html>");

import java.io.*;
import javax.servlet.*;

import javax.servlet.http.*;

public class GetCookieServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {

// Get all cookies from the request

Cookie[] cookies = request.getCookies();

String userName = null;

// Loop through the cookies to find the "userName" cookie

if (cookies != null) {

for (Cookie cookie : cookies) {

if (cookie.getName().equals("userName")) {

userName = cookie.getValue();

break;

// Set content type for response

response.setContentType("text/html");

// Output response

PrintWriter out = response.getWriter();

out.println("<html><body>");

if (userName != null) {

out.println("<h2>Welcome back, " + userName + "!</h2>");


} else {

out.println("<h2>No name found in cookies. Please enter your


name first.</h2>");

out.println("</body></html>");

Web.xml

</web-app>

<servlet>

<servlet-name>SetCookieServlet</servlet-name>

<servlet-class>SetCookieServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SetCookieServlet</servlet-name>

<url-pattern>/setCookieServlet</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>GetCookieServlet</servlet-name>

<servlet-class>GetCookieServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>GetCookieServlet</servlet-name>

<url-pattern>/getCookieServlet</url-pattern>

</servlet-mapping>

</web-app>

You might also like