|
| ||||||||||
| Tags: java, programming, servlets, sessions |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Sessions with Java Servlets
|
|
#2
| ||||
| ||||
| 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:
|
|
#3
| ||||
| ||||
| Re: Sessions with Java Servlets
Hi, HttpSession Interface provides you the methods below for the session management in the Java Servlets:
|
|
#4
| ||||
| ||||
| 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++;
}
} |
|
#5
| |||
| |||
| 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:
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. |
|
#6
| ||||
| ||||
| 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);
}
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Sessions with Java Servlets" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java Beans Vs Servlets | Ram Bharose | Software Development | 5 | 02-02-2010 10:53 AM |
| What are the advantages of java servlets over JSP? | Mithun Seth | Software Development | 5 | 23-01-2010 12:21 PM |
| What are the advantages of the servlets? | Sheenas | Software Development | 3 | 01-12-2009 01:56 PM |
| How to use Java Servlets | Wyvern | Software Development | 3 | 24-10-2009 06:24 PM |
| Which Technology is best, servlets or JSP? | Swetlano | Software Development | 3 | 05-02-2009 09:49 PM |