• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass bean class instance from one servlet to another servlet

 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here i have my bean class


and here is my one servlet code in which i am reading the values from xml file and its printing also and i can see the number of count is 4 so all are correct here



now i want to retrieve all these values in another servlet where i want to do some calculation but not able to retrieve it is showing null and indicating
for this value in my eclispe IDE " Iterator<salarybean> itr=list.iterator(); "


please anyone help me here......
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're adding the object as a session attribute, but you're trying to retrieve it as a context attribute. You need to be consistent.
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:You're adding the object as a session attribute, but you're trying to retrieve it as a context attribute. You need to be consistent.


ok sir i want to retrieve as a session attribute and normal retrieving i know like retrieving a session single attribute
like this


but how to retrieve session as a array list object which is associate with bean i mean i want syntax please sir help me.....
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not following. You're asking how to retrieve a session attribute, which is exactly what this snippet of code does. What difference does it make what type an attribute has?
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok sir i am doing like this now...


and when its printing like this com.emp.salarybean@18ae503 , 4 time and with different in last so tell me how can i access those value which is storing into in that object
there name , days and ID
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to something meaningful to be printed when you convert the object to a string, then the object needs to implement a toString() method.
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir can you tell me more about that how to convert object into String and here one object have three variable to after converting into string how to refer that particular variable like
we are storing ID, Name, Days so how to do.... sir just give me a little syntax... so i can go further ...
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done like this



its again retrieving same in that way not values its retturing like [com.emp.salarybean@a14c18, com.emp.salarybean@18ae503, com.emp.salarybean@1c12ee7, com.emp.salarybean@1ee4ce6]
as i asked there are three variables which is holding by object so how to retrieve one by one and there values...
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please stop using QUOTE tags around what you write; those are for quoting what other people have written before you.

The code now calls toString() on the ArrayList - that, too, produces nothing useful. You need to implement a toString() method for the salarybean class instead. Then "System.out.println(list.get(i))" will produce something useful (namely, it will print whatever you implemented the toString method to print).
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir i have attached this code to my salary bean class as you said..



now what should i do in Time Servlet to call this method i meant how should i call so it will give the values for first object..
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir thank you so much... for your ideas i am getting values on console like this...

Saurabh-20.0-1.0
Raj-30.0-2.0
Karan-40.0-3.0
vikram-50.0-4.0


what i want now , i want to save this value separately like saurabh in one variable, days in one variable , and id in one variable so i can do some calculation using those values with jdbc and
want to save like key value-pair, i will calculate saurabh salary using some values from database then i will store his salary with his id that i can do... but i just want to know how can save them
separately using any method or any idea may be i have to change something in code so please sir give me any idea...
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what all the getXYZ methods in the salarybean class are for, no?
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir..i explain you what i am doing... i will have one xml file in which some n number of employee will be there... i am reading there name id and no.of days then attaching those value to salarybean method
and passing this attribute to another servlet in that servlet i want to retireve first object all fields then second object all fields so here i can do some calculation so i am asking now how to save there name
id ,days in separate variables so i can do some calculation using those variables like in jsp class i did with your help using for each method there i can save there values in different different variables if i needed so same way i am asking how to do here in servlet .please give me any idea...
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
id ,days in separate variables so i can do some calculation using those variables

I'm at a loss to understand what the problem is. You have a list of all salarybeans - so you can get at every single one. And once you have a reference to those objects, you can call the getXYZ and setXYZ methods for their fields. How is that different from working with individual variables?
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir i dont know what you are saying... what list... its returning all the variable in same line like this-> Saurabh-20.0-1.0
and i am saying i want to save this saurabh separately then 20.0 separatly, then 1.0 separately so every time it will have like this

Id=1.0
Name=saurabh
Days=20.0

then

Id=2.0
Name=karan
Days=22.0

and so on like wise and i dont know what you are saying set and getting

through setter method only i am storing those value into bean

where to use get method so i can retrieve them as i want please review my code
then try to understand may be am talking something wrong because i am not technical expert person..
please sir..
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what list...

Your variable with the salarybean objects is called "list", is it not? And it is a list, the only one in the code that you posted.

its returning all the variable in same line like this-> Saurabh-20.0-1.0

Yes, if its toString method is called. Since you want something else, you need to call some other method.

i dont know what you are saying set and getting

Actually, you do know what a setter method is, since in the next sentence you state:
through setter method only i am storing those value into bean

Now, if setter methods are those called setXYZ, which methods might I be referring to as getters? It would seem that you can use those setters and getters to do anything you want with the data.

i am not technical expert person.

Since you want to do software development, you had better become one quickly, no? :-)
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir i think i should stop this conversation i am feeling like i am getting sick ..... i know everything about bean class at least basic things...
i dont know why you did not talk about the main thing which i asked i gave you sample data in which i want output you are not talking
about that..., anyways sir thanks leave it god will show me way... and i will not give up i will keep on doing
i am trying to build salary generator and i will... thanks for your help and time
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry you think I did not help you. I, on the other hand, think that I pointed you directly at the answer to your question (without writing code for you, that's true).

You asked how you could access the data in the bean objects - the answer is through its getter and setter methods. I am truly at a loss about what is missing for you to use those methods to manipulate the data that you said you wanted to manipulate.
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No sir thanks lot you helped me lot thank you so much its me that i am not able to get the things
sir first tell me sb is here bean object rite?
then what is items...? what i should call list object right?
and i understand your point using setter and getter method i can manipulate the things but there some flow
problem its printing value more than single time so i thought to take these value in another servlet class
so in that class i told your when i am using list.get(i) its returning all the value together
but i want separate so for that i am asking what syntax i should use..
like listgetId()....? which is nor correct like that i am asking so i can get get the values...
sir can i get your mail id ...so i can elaborate more otherwise its ok here..
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using "list.get(i)" is a good start. That provides you successively with all the salarybean objects, on which you can invoke the getter and setter methods - that lets you use its data in whatever way you wish.

You seem hung up on the output of the toString method - as I said, before, if that's not what you want, then don't use it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!