In this tutorial how to create a JSP page and how to create a Session in JSP is shown.
Code:
index.html
<html>
<head>
<title>Session Eg</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>
<form action=”SessionEg.jsp” method=”POST”>
Username: <input type=”text” name=”uname”><br>
<input type=”submit” value=”Submit”>
</form>
</body>
</html>
SessionEg.jsp
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Session Eg</title>
</head>
<body>
<%
String uid=request.getParameter(“uname”);
session.setAttribute(“session-uid”,uid);
response.sendRedirect(“Showuser.jsp”);
%>
</body>
</html>
Showuser.jsp
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<%
String uname=(String)session.getAttribute(“session-uid”);
out.println(“Welcome “+uname);
%>
</body>
</html>