Results 1 to 6 of 6

Thread: Sessions with Java Servlets

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,393

    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

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    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()

  4. #4
    Join Date
    Apr 2008
    Posts
    1,949

    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. #5
    Join Date
    May 2008
    Posts
    2,015

    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.

  6. #6
    Join Date
    Apr 2008
    Posts
    2,010

    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);
            }
        }
    }

Similar Threads

  1. Java Beans Vs Servlets
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 10:53 AM
  2. What are the advantages of java servlets over JSP?
    By Mithun Seth in forum Software Development
    Replies: 5
    Last Post: 23-01-2010, 12:21 PM
  3. What are the advantages of the servlets?
    By Sheenas in forum Software Development
    Replies: 3
    Last Post: 01-12-2009, 01:56 PM
  4. How to use Java Servlets
    By Wyvern in forum Software Development
    Replies: 3
    Last Post: 24-10-2009, 06:24 PM
  5. Which Technology is best, servlets or JSP?
    By Swetlano in forum Software Development
    Replies: 3
    Last Post: 05-02-2009, 09:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •