Results 1 to 6 of 6

Thread: How To Test If A Session Is Opened?

  1. #1
    Join Date
    Nov 2009
    Posts
    117

    How To Test If A Session Is Opened?

    Hello, I am working on the java from few days for learning java programming language. But, while working on it, I have got one question which when I tried to solve I am not able to solve it. If anyone from you is having knowledge about the session in java then please provide me help regarding it. I want to check whether the session is open or not in java?

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How To Test If A Session Is Opened?

    Hello, if you are making use of the java programming language then it will provide you simply method below for getting the session information whether it is open or not?
    public boolean isNew():
    If you use the method above then it will provide you true value if the client does not know the type of the session ie the cookie based session or cookie less session. So, if you make use of the method above then you will able to get the value of true if the server has created a session but not from the client. If you make use of this method on any invalidated session then it will throws the exception from the java.lang.IllegalStateException.

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: How To Test If A Session Is Opened?

    Hey, I have the solution below for your problem. If you make use of the code below in your program then you will able to achieve it:
    Code:
    HttpSession S = request.getSession(false);
    if(Session ==null)
    
    HttpSession S = request.getSession(true);
    if(S.isNew())
    Just add the code above in your program and get your problem solved.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How To Test If A Session Is Opened?

    Hello, if you want to test the session is open or not in java then there is facility of the two methods below. You can make use of them on your computer and get your problem solved.
    • public HttpSession getSession(boolean create)
    • public boolean isNew()

    You can use either of them.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How To Test If A Session Is Opened?

    Hello, if you want to test if a session is opened or not then you must need to use the code below in your program. This code will simply provide you the details about the open session is exist or not:
    Code:
    if(null == session.getAttribute("Name"))
    {
      //add your code here
    }
    else
    {  
     // add your code here
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: How To Test If A Session Is Opened?

    Hey, I have got the code below which will find out whether the session is opened or not. So, just use it and get your problem solved.
    Code:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    import java.text.DateFormat;
    public class Testing HttpServlet 
    {   
     public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, java.io.IOException 
    {
    HttpSession httpsession = req.getSession(false);
    if (httpsession == null)
    {
    res.sendRedirect("/TA/Start.jsp");
    } 
    else 
    {
    res.sendRedirect("/TA/add.jsp");
    }

Similar Threads

  1. Replies: 1
    Last Post: 23-12-2011, 03:55 AM
  2. Replies: 4
    Last Post: 17-01-2011, 11:14 AM
  3. Replies: 4
    Last Post: 24-12-2010, 07:58 AM
  4. Replies: 3
    Last Post: 31-07-2009, 01:09 PM
  5. Replies: 6
    Last Post: 20-06-2006, 07:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,716,436,338.05094 seconds with 17 queries