Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Objects to Session  RSS feed

 
Will Sillmon Jr.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have four fields that I am trying to add to a session. Two of the fields are provided by the user and passed to a servlet. I know how to post them to a JSP:

***COMES FROM FORM****
String sessionKey = (String) session.getAttribute("Key");

***COMES FROM SERVLET****
<p align=left><FONT face=Arial, Helvetica, sans-serif><B>Company</B><strong>:</strong><strong> " + sessionKey + "</strong></font></FONT></p>

My servlet takes this information from the form and returns values for two new variables... the results come from information returned from a query to a table.

I have these variable occupied with the correct information but I don't know how to add them to a session so I can pass them to the results screen using the same type of code above. I know how to do this using a bean but I am asked to do this using sessions.... HELP!!!
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you correctly, you can just set this variables on the sessioin using theSession.setAttribute(key, value)
 
Will Sillmon Jr.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The results that I get back from the query come in a string. I have used substring to take out the data that I need from this string and given them names. How do I take that new field name from the substring and place it in a session. Hope this clears up any misunderstanding.

I tried using what you suggested but I could only get it to work when I was dealing with info from the original form... not from data that is NOT entered from a user...

Maybe a better question would be.. " how do you take predefined variables and add them to the current session?"

I tried to make this as clear as I could... THANKS IN ADVANCE..
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The setAttribute method is what you will use regardless of where the data comes from. The second parameter to the method can be any object type.
 
Will Sillmon Jr.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what goes where?? Ok.... The two field that were populated from the query are as follows...

String data = myResponse.substring(164, 183);
String desc = myResponse.substring(184, 233);

*myResponse is the string that is returned from the table query*

I tried using:

String sessionData = "";
session.setAttribute(sessionData,data);

String sessionDesc = "";
session.setAttribute(sessionDesc,desc);

I then used:

<p align=left><FONT size=3><B><font face=Arial, Helvetica, sans-serif>Data: " + sessionData +" </font></B></FONT></TD>"

to display the data on the output of the servlet....

It did not work... what am I doing wrong.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String sessionData = "";
session.setAttribute(sessionData,data);


That makes no sense. Why are you setting the scoped variable name to the empty string?

For that matter, why not back up and describe why you are putting scoped variables into the session in the first place? It's clear that you are having some issues with the concept.
 
Will Sillmon Jr.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am basically in "trial and error" mode right now. All of the research that I have done has turned up nothing but "how to display session information" or another "very specific" situation that does not pertain to mine.

If you could guide me in the direction of how to take those fields coming from a query and add them to a session, it would be greatly appreciated. I

Better yet... here's the deal...

1.) form one: company and key input field that is sent to servlet.

2.) servlet: takes company and key and runs query on them and retuns a 180 char string called myResponse.

3.)myResponse: subString is used to divide string into different fields and each field is then given a name.

4.)Desc and Data: are two field that comes from myResponse that needs to be sent to new HTML/JSP for display to the user. Currently being done inside of the servlet but should eventually be done in JSP. (just taking one step at a time)..

5.)ALL DATA needs to be kept in session because it may be used in future screens
Any suggestions....
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5.)ALL DATA needs to be kept in session because it may be used in future


This is a key part since otherwise I'd say that request scope would be a more approriate place than session for such data. But that said, also be aware that caching data in the session in this way is usually considered a bad idea since it allows the data to become stale very quickly.

Storing scoped variables, be they in whatever scope, is done in the same manner: setAttribute is used to create the variable, and getAttribute is used to retrieve the vairable (at least in Servlet code -- JSP has short-hand mechanisms for this).

As I said, the code:

String sessionData = "";
session.setAttribute(sessionData,data);


makes no sense since you are creating a scoped variable with the empty string as a name. Not good.

Rather, you'll usually see something like:


[ January 03, 2006: Message edited by: Bear Bibeault ]
 
Will Sillmon Jr.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to seperate what fields should be kept as request scope and which should be applied to the session. The only reason why I am using session scope is because there will be a login screen that must be completed before you are able to access this screen. Should I just keep the items on that screen session scope and make these fields request?
 
Will Sillmon Jr.
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
--------------------------------------------------------------------------------

session.setAttribute( "thisIsTheNameOfTheVariable", variableData );

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



So is there a need for me to declare "thisIsTheNameOfTheVariable" or does this statement do it for me. I guess that's what I was trying to do in the previous code that I provided.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the looks of this thread, you will have a much easier time with servlets if you step back from this project for a bit and go through a few books and/or web tutorials. There are tons of them out there.
If you search this and the JSP forum with keywords "book" and "books" you will find lots of discussion about which ones people like and dislike.

Personally, I found "Core Servlets and Java Server Pages" along with it's followup "More Servlets..." to more than cover all the fundementals.
Core servlets is now out in it's 2nd edition.
Another one that is wildly popular on this site is "Head First Servlets and JSP". I haven't read this one but I've heard it mentioned hundreds of times here.

If you haven't already, you may want to follow a few Java tutorials as well.
Once you've got these concepts under your belt, I'm sure you will return to your project and make up the time spent very quickly.
You'll also enjoy Servlets more.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Will Sillmon Jr.:



So is there a need for me to declare "thisIsTheNameOfTheVariable" or does this statement do it for me. I guess that's what I was trying to do in the previous code that I provided.


To answer this one..
setAttribute takes two arguments; a String which is the key, and an Object which is the value.

For the first argument, you can use a literal constant:
session.setAttribute("myBean", myBean");
or you can use a variable:
String beanName = "myBean";
session.setAttribute(beanName, myBean);

java.lang.String,%20java.lang.Object&#41;" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSession.html#setAttribute(java.lang.String,%20java.lang.Object)
[ January 03, 2006: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Will Sillmon Jr.:
Is there a way to seperate what fields should be kept as request scope and which should be applied to the session. The only reason why I am using session scope is because there will be a login screen that must be completed before you are able to access this screen. Should I just keep the items on that screen session scope and make these fields request?


A good rule of thumb is to keep your scoped object as local as possible.
If you're only going to use an object within the span of one request, it make no sense to bind it to session or context scope.

Any good Servlet or JSP tutorial will cover all of the scope objects and their, well, 'scope' very early on.
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Will Sillmon Jr.:
2.) servlet: takes company and key and runs query on them and retuns a 180 char string called myResponse.

3.)myResponse: subString is used to divide string into different fields and each field is then given a name.


Why are you returning the query data as a String which then has to be parsed into the component fields? Instead, create a POJO which represents the data returned by the query & return an instance of this POJO populated with the data returned by the query. Then you can put this one object in the session; get it whenever you want and access the various values via the accessor (getter) methods.
Example:
If the query returns data about an employee,
  • You create an Employee.java class with get/setFirstName(), get/setLastName(), get/setDepartment() etc. methods
  • The method that executes the query instantiates an object employee of the Employee class & sets the various fields (employee.setFirstName(), employee.setLastName(), employee.setDepartment(), etc)
  • The servlet will return the employee object
  • You put it in the session - session.setAttribute("Employee", employee);
  • You retrieve it from the session - Employee emp = (Employee) session.getAttribute("Employee");
  • Access the fields of this - emp.getFirstName(), emp.getDepartment(), etc.


  • [ January 04, 2006: Message edited by: Sadanand Murthy ]
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!