HttpSession Interface | |
public interface HttpSession | |
The HTTP protocol is stateless, meaning that each request is independent and has no knowledge of previous requests. Sometimes, it is desirable to save state information. For instance, the contents of a shopping cart should be known by each request. Http Servlets can maintain this information by way of a session. The HttpSession interface provides methods that define a session between a client and server. The session lasts for a specified time period and can encompass more than one connection or page request from the user. The methods declared by this interface allow the access of information about the session and enable the binding of objects to sessions. The bound object can contain the state information that is intended to be known by every request. Example: Using HttpSession In this example, an HttpSession object is used to monitor the value of a Counter object that is bound to it. The user can increment or re-set the value. import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SessionServlet extends HttpServlet {
// The doGet() method is called when the servlet is invoked.
// It sends a simple form back to the client containing two
// buttons, one to add to the count and one to clear the count.
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<HTML> <HEAD> <TITLE> Cookie Example </TITLE>");
pw.println("</HEAD><BODY>");
pw.println("<FORM METHOD=POST>");
pw.println("<INPUT TYPE=SUBMIT NAME=add VALUE=Add><BR>");
pw.println("<INPUT TYPE=SUBMIT NAME=clear VALUE=Clear><BR>");
pw.println("</FORM></BODY></HTML>");
pw.close();
}
// The doPost() method is called when either of the two buttons
// is pressed.
//
// This example is set up to run under Java Servlet API 2.1.
// Under version 2.2, the putValue() method would be replaced
// by setAttribute() and the getValue() method would be replaced
// by getAttribute()
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
// A HttpSession object is created if it does not already exist.
// If the client has not yet accessed the servlet, a Counter object
// is created and bound to the session. The Counter object is
// returned using the getValue() method every time the servlet is
// accessed.
HttpSession session = request.getSession(true);
if (session.isNew()) {
session.putValue("count", new Counter(0));
}
Counter counter = (Counter) session.getValue("count");
// If the "Add" button was pressed, the count is incremented.
// If the "Clear" button was pressed, the count is cleared.
if (request.getParameter("add") != null) {
counter.addOne();
} else {
counter.clear();
}
response.setContentType("text/html");
// Open a character output stream to the client machine and write
// the current count. There is also a hyperlink to return to the
// button page
char dq = '\"';
PrintWriter pw = response.getWriter();
pw.println("<HTML> <HEAD> <TITLE> Session Example </TITLE>");
pw.println("</HEAD><BODY>");
pw.println("current amount: " + counter.getCount() + "<BR>");
pw.print("<A HREF=" + dq);
pw.print(request.getRequestURI());
pw.println(dq + ">Return to Buttons</A>");
pw.println("</BODY></HTML>");
pw.close();
}
}
// Counter is a simple class that maintains a count. Methods are
// provided to increment the count, clear the count, and return
// the current count value.
class Counter {
int count;
public Counter(int c) {
count = c;
}
public void addOne() {
++count;
}
public void clear() {
count = 0;
}
public int getCount() {
return count;
}
}
Press the Add button a few times and watch the count increment. The Clear button causes the count to be re-set to zero. |
getAttribute() | |
public Object getAttribute(String name) | Method |
getAttribute() returns the Object bound to the specified name in this session. |
getAttributeNames() | |
public Enumeration getAttributeNames() | Method |
getAttributeNames() returns an Enumeration of String objects containing the names of all the objects bound to this session. |
removeAttribute() | |
public void removeAttribute(String name) | Method |
removeAttribute() removes the Object bound to the specified name from this session. |
setAttribute() | |
public void setAttribute(String name, Object value) | Method |
setAttribute() binds an Object to the specified attribute name for this session. If the attribute name already exists, the Object passed to this method will replace the previous Object. |
getId() | |
public String getId() | Method |
getId() returns a String object containing a unique identifier for this session |
invalidate() | |
public void invalidate() | Method |
invalidate() invalidates the session and unbinds any objects bound to it. |
isNew() | |
public boolean isNew() | Method |
isNew() returns true if the server has created a session that has not yet been accessed by a client. |
getCreationTime() | |
public long getCreationTime() throws IllegalStateException | Method |
getCreationTime() returns the time when the session was created in milliseconds since midnight Jan 1, 1970 GMT. |
getLastAccessedTime() | |
public lone getLastAccessedTime() | Method |
getLastAccessedTime() returns the last time a client request associated with the session was sent. The return value is the number of milliseconds since midnight Jan 1, 1970 GMT. |
getMaxInactiveInterval() | |
public int getMaxInactiveInterval() | Method |
getMaxInactiveInterval() returns the number of seconds the server will wait between client requests before the session is invalidated. A negative return value indicates the session will never time out. |
setMaxInactiveInterval() | |
public void MaxInactiveInterval(int time) | Method |
setMaxInactiveInterval() specifies the number of seconds the server will wait between client requests before the session is invalidated. If a negative value is passed to this method, the session will never time out. |
getSessionContext() | |
public HttpSessionContext getSessionContext() | Deprecated |
This method has been deprecated under Java Servlet API 2.2 and should not be used for new code. |
getValue() | |
public Object getValue(String name) | Deprecated |
This method has been deprecated under Java Servlet API 2.2 and should not be used for new code. |
getValueNames() | |
public String[] getValueNames() | Deprecated |
This method has been deprecated under Java Servlet API 2.2 and should not be used for new code. |
putValue() | |
public void putValue(String name, Object value) | Deprecated |
This method has been deprecated under Java Servlet API 2.2 and should not be used for new code. |
removeValue() | |
public void removeValue(String name) | Deprecated |
This method has been deprecated under Java Servlet API 2.2 and should not be used for new code. |