aspose file tools*
The moose likes Servlets and the fly likes How to get session in a common file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get session in a common file" Watch "How to get session in a common file" New topic
Author

How to get session in a common file

vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170
Hi all,
I have a common file which can be accessed from many other files.
I want to store the session here how can i do this?
After I get the session ,I will get the id by using session.getSessionID()
No request is passed to this file from any file


Thanking you in advance !!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not sure what you're asking.

What do you mean a common file?
What do you mean by "Store Session"?

You don't store a session.
Sessions are created on your behalf by the container.
You can then access it, bind attributes, and retrieve attributes from it.

If you can explain in a little more detail, what you're trying to do, someone should be able to give you a suggestion.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170
What do you mean a common file?


I will have a common java file which can be accessed at any point from the different files.I will do some operations here.
Lets say this file name is Common.java


Sessions are created on your behalf by the container.
You can then access it, bind attributes, and retrieve attributes from it.


This is exactly what is needed.
I do not know how to get the session in my utility file ? i.e Common.java
In my other files , we can get the session from
HttpSession session = request.getSession();
where request is HttpServletRequest.
Since i am not passing any request to my file Common.java so thats why i cannot get the session in this file .
Is there ways to get the session here ??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60975
    
  65

It's a poor idea to pass resources like the request and the session around. I'd suggest extracting the data you need, and pass the data as parameters to the "common" classes.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170
Hi,
I need the session ID to create a URL connection in the common file which will do some operations ...
Is there any way to get the sessionId in this util file ?
suri jagadish
Greenhorn

Joined: Sep 14, 2005
Posts: 19
Hi,

The first thing you need to do is the make the method that will prepare the URL static so that your servlet can call that method and pass the sessionID as parameter to that method and that will return the URL.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I think we have great confusion over the use of the word "file."

I will have a common java file which can be accessed at any point from the different files.I will do some operations here.
Lets say this file name is Common.java


You appear to be talking about a Java class. Calling it a file is just confusing. As suri suggests, you might use static methods and variables in this class which could be accessed by other servlets in the same context.

Bill
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to get session in a common file