aspose file tools*
The moose likes Servlets and the fly likes Creating a Simple Session Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Creating a Simple Session Variable" Watch "Creating a Simple Session Variable" New topic
Author

Creating a Simple Session Variable

Michele Smith
Ranch Hand

Joined: Oct 27, 2010
Posts: 421
Hello I have a servlet which serves an xsl page. I wanted to enable that particular servlet to request a session variable.

What I have done to the servlet that serve the xsl page is to put an import: import bcc.pages.parent;

and the code on the parent servlet is as follows:



what is happening now is that the page wont' load.

What I am trying to do is:

1) Create a session variable in your application where you would store the "parentId"
e.g.
request.getSession().setAttribute("parentId",114);

Could you please point me in the right direction?

Thanks,
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

-->servlet often used in multi threading environment. so dont define HttpSession as an instance variable.
-->secondly can you elaborate what problem are you facing?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

But that servlet doesn't output any data, so probably that's why you aren't seeing anything in the response.

And I don't understand what you mean by a "parent" servlet.
Michele Smith
Ranch Hand

Joined: Oct 27, 2010
Posts: 421
Hello I am trying to do the following:

(At a high level)

1) Create a session variable in your application where you would store the "parentId"
e.g.
request.getSession().setAttribute("parentId",114);

2) In the report where you are using the parentId to filter the query you will have to read the session variable and add it to the query instead of the report parameter value. However, inorder to make it run from your desktop you may need the parameter otherwise it will always be null.

e.g. in the beforeOpen method of the query

var sessionParentId = null
sessionParentId= reportContext.getHttpServletRequest().getSession().getAttribute("parentId");

if
(sessionParentId== null)

sessionParentId = params[
"parentId"].value;



this.queryText = 'select distinct home_short_name from home where parent_id = ' +sessionParentId;



While calling the report do not pass the parentId in the URL.

-------------------------------------------------------------------------------------------------------------------------------------------------

I understand the beforeopen, I am having trouble with the session servlet described above.

I already have a servlet running the page, the page is displaying some dynamically hyperlinked items in xml 1.0 and because it is dynamically hyperlinked, it displays the parameter parentid on the URL when you click on the hyperlink and it also displays the parameter when you hover above it.

It is already using a bunch of servlets which utilize the following:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

I don't have a very simple example of creating a session variable, and also I don't know where to put the code for the session variable, i.e., should I put it in with the other servlets:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

I have been told this session is only necessary on two dynamically hyperlinked landing pages. It would break the whole application if I introduced it globally to the application.

My question to Seetharaman is this:

servlet often used in multi threading environment. so dont define HttpSession as an instance variable.


What should I do if I don't define a httpsession? Thanks,

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Michele Smith wrote:
My question to Seetharaman is this:
servlet often used in multi threading environment. so dont define HttpSession as an instance variable.


What should I do if I don't define a httpsession?

define inside a service(doGet or doPost) . threads share instance/class variables among them. so person A will get charged, because of B person activities.
 
 
subject: Creating a Simple Session Variable