Implicit Objects in JSP, session object in JSP, Session Management in JSP
session is an object of HttpSession interface of Java Servlet, It is used to manage the state of an user and creating and managing the account login sessions for every user. The session object used to create a new session for an user and maintain the session till the user will not close the session explicitly, with session object we can differentiate the multiple session ID's and can be identify the session creation time etc.
The list of methods linked with session object
1. setAttribute(String sessionName, Object sessionValue):- This method is used to create a session variable and assigned the value to it.
2. getAttribute():- This method is used to get the already created session value.
3. getAttributeNames():- This method is used to get the all existing session variables names. It returns the Enumration type list of already created session variables.
4. getCreationTime():- This method is used to get the creation time of a session in Long data type.
5. getId():- This method is used to get the unique Identity for each session object.
6. getLastAccessedTime():- This method is used to get the session time in miliseconds, when a request sent associated with the current session ID.
7. getMaxInactiveInterval():- This method is used to get the max time for a session will alive to the container.
8. invalidate():- This method is used to destroy the session object.
9. removeAttribute(String attrName):- This method is used to remove any existing attribute associated with the session.
10. setMaxInactiveInterval(int intervalTime):- This method is used to set the time of the session will keep alive. After expiration of this given time limit the session will automatically invalidate.
Simple Example for creating a session and using it into another page
login.html
<html>
<body>
<form method="post" action="loginValidate.jsp">
Enter Username<input type="text" name="user"><br/>
Enter Password<input type="password" name="pass"><br/>
<input type="submit" value="login">
</form>
</body>
</html>
loginValidate.jsp
<html>
<body>
<%
String user=request.getParameter("user");
String pass=request.getParameter("pass");
if(user.equals("talib")&&pass.equals("hassan"))
{
session.setAttribute("username",user)
response.sendRedirect("welcome.jsp");
}
else
{%>
<h4>invalid username or password</h4>
<%@include file="login.html"%>
<%
}
%>
</body>
</html>
welcome.jsp
<html>
<body>
<h1>Welcome <%=session.getAttribute("user")%></h1>
</body>
</html>
we will discuss about the other methods of session in upcoming chapters.
session is an object of HttpSession interface of Java Servlet, It is used to manage the state of an user and creating and managing the account login sessions for every user. The session object used to create a new session for an user and maintain the session till the user will not close the session explicitly, with session object we can differentiate the multiple session ID's and can be identify the session creation time etc.
The list of methods linked with session object
1. setAttribute(String sessionName, Object sessionValue):- This method is used to create a session variable and assigned the value to it.
2. getAttribute():- This method is used to get the already created session value.
3. getAttributeNames():- This method is used to get the all existing session variables names. It returns the Enumration type list of already created session variables.
4. getCreationTime():- This method is used to get the creation time of a session in Long data type.
5. getId():- This method is used to get the unique Identity for each session object.
6. getLastAccessedTime():- This method is used to get the session time in miliseconds, when a request sent associated with the current session ID.
7. getMaxInactiveInterval():- This method is used to get the max time for a session will alive to the container.
8. invalidate():- This method is used to destroy the session object.
9. removeAttribute(String attrName):- This method is used to remove any existing attribute associated with the session.
10. setMaxInactiveInterval(int intervalTime):- This method is used to set the time of the session will keep alive. After expiration of this given time limit the session will automatically invalidate.
Simple Example for creating a session and using it into another page
login.html
<html>
<body>
<form method="post" action="loginValidate.jsp">
Enter Username<input type="text" name="user"><br/>
Enter Password<input type="password" name="pass"><br/>
<input type="submit" value="login">
</form>
</body>
</html>
loginValidate.jsp
<html>
<body>
<%
String user=request.getParameter("user");
String pass=request.getParameter("pass");
if(user.equals("talib")&&pass.equals("hassan"))
{
session.setAttribute("username",user)
response.sendRedirect("welcome.jsp");
}
else
{%>
<h4>invalid username or password</h4>
<%@include file="login.html"%>
<%
}
%>
</body>
</html>
welcome.jsp
<html>
<body>
<h1>Welcome <%=session.getAttribute("user")%></h1>
</body>
</html>
we will discuss about the other methods of session in upcoming chapters.
{ 0 comments... read them below or add one }
Post a Comment