EXP 24
X. Program Code: 2. Write the output of following servlet
1. Write a Program to Create Cookie. code
HTML code:
<html>
<head>
<title>Create a Cookie</title>
</head>
<body>
<h1>Create a Cookie</h1>
<form
action="http://localhost:8081/examples/se
rvlets/servlet/password" method="post">
Cookie Name: <input type="text"
name="cookieName" required><br>
Cookie Value: <input type="text"
name="cookieValue" required><br>
<input type="submit" value="Create
Cookie">
</form>
<br><h4>Performed by Khushi and
Sanika</h4>
</body>
</html>
SERVLET code:
// Get the client's IP address
import java.io.IOException; String ipAddress =
import java.time.LocalDateTime; request.getRemoteAddr();
import
java.time.format.DateTimeFormatter; // Write a response to the client
import javax.servlet.ServletException;
import response.getWriter().println("<h2>Cookie
javax.servlet.annotation.WebServlet; Created</h2>");
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet; response.getWriter().println("<p>Cookie
import Name: " + cookieName + "</p>");
javax.servlet.http.HttpServletRequest;
import response.getWriter().println("<p>Cookie
javax.servlet.http.HttpServletResponse; Value: " + cookieValue + "</p>");
@WebServlet("/password") response.getWriter().println("<p>Current
public class password extends Date and Time: " + formattedDateTime +
HttpServlet { "</p>");
protected void
doPost(HttpServletRequest request, response.getWriter().println("<p>Client IP
HttpServletResponse response) Address: " + ipAddress + "</p>");
throws ServletException,
IOException { response.getWriter().println("<br><h4>Pe
// Get cookie name and value from rformed By Akshay and Prajol</h4>");
the form }
String cookieName = }
request.getParameter("cookieName");
String cookieValue =
request.getParameter("cookieValue");
// Create a new cookie
Cookie cookie = new
Cookie(cookieName, cookieValue);
// Set the cookie age to 1 day (in
seconds)
cookie.setMaxAge(24 * 60 * 60);
// Add the cookie to the response
response.addCookie(cookie);
// Set response type to HTML
response.setContentType("text/html");
// Get the current time and date
LocalDateTime currentDateTime =
LocalDateTime.now();
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-d
d HH:mm:ss");
String formattedDateTime =
currentDateTime.format(formatter);
XIII
1. 2.
HTML code: HTML code:
<html> <!DOCTYPE html>
<head> <html lang="en">
<title>User Information</title> <head>
</head> <meta charset="UTF-8">
<body> <meta name="viewport" content="width=device-width,
<h1>Enter Your Information</h1> initial-scale=1.0">
<form <title>Get Browser Info</title>
action="http://localhost:8081/examples/se </head>
rvlets/servlet/password" method="post"> <body>
Name: <input type="text" name="name" <h1>Get Browser Information</h1>
required><br> <form
Email: <input type="email" name="email" action="http://localhost:8081/examples/servlets/servlet/pa
required><br> ssword" method="get">
<input type="submit" value="Submit"> <input type="submit" value="Get Browser Info"/>
</form> </form>
<br><br><h4>Performed by Akshay and <br><br><h4> Performed by Akshay and Prajol</h4>
Prajol</h4> </body>
</body> </html>
</html>
Servlet Code:
Servlet Code: import java.io.IOException;
import java.io.IOException; import java.text.SimpleDateFormat;
import java.text.SimpleDateFormat; import java.util.Date;
import java.util.Date; import javax.servlet.ServletException;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet;
import import javax.servlet.http.HttpServlet;
javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpSession;
import
javax.servlet.http.HttpServletRequest; @WebServlet("/password")
import public class password extends HttpServlet {
javax.servlet.http.HttpServletResponse; protected void doGet(HttpServletRequest request,
HttpServletResponse response)
@WebServlet("/password") // Ensure the throws ServletException, IOException {
URL mapping is correct // Get browser information from the user-agent
public class password extends String userAgent =
HttpServlet { request.getHeader("User-Agent");
protected void
doPost(HttpServletRequest request, // Get the client's IP address
HttpServletResponse String ipAddress = request.getRemoteAddr();
response)
throws ServletException, // Get the current date and time
IOException { String currentDateTime = new
String name = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new
request.getParameter("name"); Date());
String email =
request.getParameter("email"); // Create or retrieve the session
HttpSession session = request.getSession();
// Get the client's IP address session.setAttribute("browserInfo", userAgent);
String ipAddress = session.setAttribute("ipAddress", ipAddress);
request.getRemoteAddr(); session.setAttribute("currentDateTime",
currentDateTime);
// Get the current date and time
String currentDateTime = new // Set response type to HTML
SimpleDateFormat("yyyy-MM-dd response.setContentType("text/html");
HH:mm:ss").format(new Date());
// Write the response
// Create cookies for name and email response.getWriter().println("<html><body>");
Cookie nameCookie = new response.getWriter().println("<h2>Your Browser
Cookie("userName", name); Information</h2>");
Cookie emailCookie = new response.getWriter().println("<p>Browser Info: " +
Cookie("userEmail", email); userAgent + "</p>");
response.getWriter().println("<p>IP Address: " +
// Set cookie age to 1 day (in ipAddress + "</p>");
seconds) response.getWriter().println("<p>Date and Time: " +
nameCookie.setMaxAge(24 * 60 * currentDateTime + "</p>");
60); response.getWriter().println("<p>This information has
emailCookie.setMaxAge(24 * 60 * been stored in your session.</p>");
60); response.getWriter().println("<p>Performed By
Akshay and Prajol.</p>");
// Add cookies to response response.getWriter().println("</body></html>");
response.addCookie(nameCookie); }
response.addCookie(emailCookie); }
// Set response type to HTML
response.setContentType("text/html");
// Write the response
response.getWriter().println("<h2>Informa
tion Stored</h2>");
response.getWriter().println("<p>Name: "
+ name + "</p>");
response.getWriter().println("<p>Email: "
+ email + "</p>");
response.getWriter().println("<p>IP
Address: " + ipAddress + "</p>");
response.getWriter().println("<p>Date and
Time: " + currentDateTime + "</p>");
response.getWriter().println("<br><br>Per
formed by Akshay and Prajol");
}
}