Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Sessions with Java Servlets

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 15-02-2010
Member
 
Join Date: Nov 2009
Posts: 877
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.

Reply With Quote
  #2  
Old 15-02-2010
Member
 
Join Date: May 2008
Posts: 2,382
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
Reply With Quote
  #3  
Old 15-02-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
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()
Reply With Quote
  #4  
Old 15-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,947
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++;
}
}
Reply With Quote
  #5  
Old 15-02-2010
Member
 
Join Date: May 2008
Posts: 2,007
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.
Reply With Quote
  #6  
Old 15-02-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
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);
        }
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 08:15 AM.