Sessions with Java Servlets
Hello, I have recently started learning java servlet programming. I have understand the most of the part, but when I come across the Sessions I am not able to understand it. I have read different articles on internet, but it is not possible to understand the java servlet sessions. If you know it in detail then please provide me some point wise information about it. So, I can able to understand it. Thank you in advance.
Re: Sessions With Java Servlets
Hello, I think first you need to understand the use and need of the session.
As there are number of users available which will create connection with the visit to the website. Then you must need to use the session management for the reasons below:
- Customizations
- Security
- User Behavior
Re: Sessions with Java Servlets
Hi, HttpSession Interface provides you the methods below for the session management in the Java Servlets:
- getAttribute()
- getId()
- getCreationTime()
- getLastAccessedTime()
- getMaxInactiveInterval()
- isNew()
- invalidate()
Re: Sessions with Java Servlets
The code below will provide you details about the total number of pages viewed by the user.
Code:
import java.io.Serializable;
public class Total implements Serializable
{
private int total = 0;
public int getCount()
{
return this.total;
}
public void increment()
{
this.total++;
}
}
Re: Sessions with Java Servlets
Hello, if you want to perform the session tracking with the use of any type of technology then it is necessary to make use of the following things:
- Cookies
- URL Rewriting
- Hidden form fields
As we all know that we most of the time make use of the HTTP for using the web. So, HTTP is the stateless protocol. But, it is necessary to remember the previous transactions if we consider the online shopping. So, the solution on such problem is the use of the Session in java technology.
Re: Sessions with Java Servlets
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);
}
}
}