Hello, I am providing you simple example of the session with the help of servlet. I think you will able to understand it without any explanation. It will help you to understand it by the use of the program:
Code:
import javax.servlet.*;
import javax.servlet.http.*;
class sessionManagement extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res) throws
IOException, ServletException {
res.setContentType("text/html");
PrintWriter PW = res.getWriter();
HttpSession session = req.getSession(true);
Date date = new Date(session.getCreationTime());
Date newDate = new Date(session.getLastAccessedTime());
PW.println("ID " + session.getId());
PW.println("Created: " + date);
PW.println("Last Accessed: " + newDate);
String str = req.getParameter("str");
if (str != null && str.length() > 0) {
String string = req.getParameter("string");
session.setAttribute(str, string);
}
Enumeration enum = session.getAttributeNames();
while (enum.hasMoreElements()) {
String strname = (String)enum.nextElement();
String value = session.getAttribute(strname).toString();
PW.println(strname + " = " + value);
}
}
}
Bookmarks