Session in JSP
JSP has HttpServlet interface that helps to identify user and
store their session credentials . There are many methods for using session
object that can be used in JSP.
S.N.
|
Methods & Description
|
1
|
public Object getAttribute(String name)
This method returns the object bound with the specified name in this session, or null if no object is bound under the name. |
2
|
public Enumeration getAttributeNames()
This method returns an Enumeration of String objects containing the names of all the objects bound to this session. |
3
|
public long getCreationTime()
This method returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT. |
4
|
public String getId()
This method returns a string containing the unique identifier assigned to this session. |
5
|
public long getLastAccessedTime()
This method returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT. |
6
|
publicintgetMaxInactiveInterval()
This method returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses. |
7
|
public void invalidate()
This method invalidates this session and unbinds any objects bound to it. |
8
|
publicbooleanisNew()
This method returns true if the client does not yet know about the session or if the client chooses not to join the session. |
9
|
public void removeAttribute(String name)
This method removes the object bound with the specified name from this session. |
10
|
public void setAttribute(String name, Object value)
This method binds an object to this session, using the name specified. |
11
|
public void setMaxInactiveInterval(int interval)
This method specifies the time, in seconds, between client requests before the servlet container will invalidate this session. |
JSP File (index.jsp)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>nirajanghimireyworkshop.blogspot.com</title>
</head>
<body>
<form action="process.jsp">
<input type="text" name="uname" placeholder="Enter Your Name">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
JSP File (process.jsp)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>nirajanghimireyworkshop.blogspot.com</title>
</head>
<body>
<%
String name=request.getParameter("uname");
out.println("<h3>You have Enter on nirajanghimireyworkshop.blogspot.com</h3>");
out.print("Welcome "+name);
session.setAttribute("user",name);
%>
<br/>
<a href="data.jsp">Next Page</a>
</body>
</html>
JSP File (data.jsp)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>nirajanghimireyworkshop.blogspot.com</title>
</head>
<body>
<%
String name=(String)session.getAttribute("user");
out.print("Hello "+ "<b>"+name + "</b>" +"<br/>");
out.println ("You can learn HTML, CSS, JSP, etc.");
%>
</body>
</html>
No comments:
Post a Comment