Untitled
unknown
plain_text
7 months ago
3.9 kB
5
Indexable
profile
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="jakarta.servlet.http.Cookie" %>
<!DOCTYPE html>
<html>
<head>
<title>Profile</title>
</head>
<body>
<%
// Get all cookies from the request
Cookie[] cookies = request.getCookies();
String username = null;
String Piyush = null;
// Loop through cookies to find the one named "username"
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
username = cookie.getValue();
break;
}
}
for (Cookie cookie : cookies){
if ("Piyush".equals(cookie.getName())) {
Piyush = cookie.getValue();
break;
}
}
}
if (username != null) {
%>
<h2>User Profile</h2>
<p>Username: <%= username %></p>
<p>Piyush: <%= Piyush %></p>
<a href="logout.jsp">Logout</a>
<%
} else {
out.println("No session found. Please log in.");
}
%>
</body>
</html>
welcome
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="jakarta.servlet.http.Cookie" %>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username != null && password != null) {
Cookie userCookie = new Cookie("username", password);
userCookie.setMaxAge(24 * 60 * 60);// time in seconds
response.addCookie(userCookie);
Cookie nonCookie = new Cookie("Piyush", "Piyush");
response.addCookie(nonCookie);
out.println("<h2>Welcome, " + username + "!</h2>");
%>
<a href="profile.jsp">Go to Profile</a>
<%
} else {
out.println("Invalid credentials.");
}
%>
</body>
</html>
login
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login Form</h2>
<form action="welcome.jsp" method="post">
Name: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Login">
</form>
</body>
</html>
logout<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="jakarta.servlet.http.Cookie" %>
<!DOCTYPE html>
<html>
<head>
<title>Logout</title>
</head>
<body>
<%
// Get all cookies from the request
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("Piyush".equals(cookie.getName())) {
// Set the cookie max age to 0 to delete it
cookie.setMaxAge(0);
response.addCookie(cookie);
break;
}
}
}
%>
<h2>You have successfully logged out.</h2>
<a href="login.jsp">Go to Login</a>
</body>
</html>
index
<!DOCTYPE html>
<html>
<head>
<title>Start Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="login.jsp" method="post">
<input type="submit" value="Login">
</form>
</body>
</html>
Editor is loading...
Leave a Comment