JSP implicit object "session"

The session object is an instance of a servlet API javax.servlet.http.HttpSession interface. This is typical reequirement for web application, where a 'user activity' happens across multiple requests and responses, and the server is required to associate some form of uniqueness to each of the users.
Example of implict object "session" :
index.html
<html> <head> <title>Welcome implict object session example </title> </head> <body> <form action="login.jsp" method="post"> <fieldset> <legend> <font face="Courier New" size="+1" color="red"> Login </font> </legend> <table> <tr> <td>User Name </td> <td> <input type="text" name="userName"> </td> </tr> <tr> <td>Password </td> <td> <input type="text" name="password"> </td> </tr> <tr> <td colspan=2> <input type="submit" value="Login"> </td> </tr> </table> </fieldset> </form> </body> </html>
login.jsp
<%@ page language="java" %> <html> <body> <% String userName = request.getParameter("userName"); String password = request.getParameter("password"); if (userName != null && password != null && userName.equals("Sunil") && password.equals("Kumar")) { session.setAttribute("user",userName); response.sendRedirect("welcome.jsp"); } else { response.sendRedirect("index.html"); } %> </body> </html>
welcome.jsp
<%@ page language="java" import="java.lang.*" %> <html> <head> <title>Welcome page</title> </head> <body> <h1>Login Successful.</h1> <% out.println("Welcome "+session.getAttribute("user")); // or out.println("<BR>"); String name=(String)session.getAttribute("user"); out.println("Welcome "+name); %> </body> </html>
Output
session
session
Download this example
 



 

Latest Tutorial


Most Visited Tutorial