| Author |
servlet setting some value need to retrive that value in a normal java file
|
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Hi ,
I am having a jsp page for submitting values .
All these values are recievied by a servlet .
This servlet in turn calls a normal java file
(This is the flow of our application)
Now the problem is
In that servlet i am setting some value for this i have to pass this value to that java class
For setting value inside servlet ,i have taken help of javaBean class as shown below
Inside servlet :
But when i am trying to retrive this inside my Java Class
It is returning me null .
Please help
|
Save India From Corruption - Anna Hazare.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
So you are actually passing the FlagBean instance that you created in your servlet to the java class?? I don't see any reason how this can happen
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
I am also not sure how to do this ?
so please tell me how can the communication from my servlet class to the java class can happen ??
|
 |
Andrew Monkhouse
author and jackaroo
Marshal Commander
Joined: Mar 28, 2003
Posts: 10820
|
|
|
Perhaps you could show us the bit of your code in the doGet or doPost that is related to the call to your worker bean?
|
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
|
It's not clear what you are trying to do here, Are you trying to call some methods of a bean from the servlet or ...?
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Ankit Garg wrote:So you are actually passing the FlagBean instance that you created in your servlet to the java class?? I don't see any reason how this can happen
thats why i posted .
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Andrew Monkhouse wrote:Perhaps you could show us the bit of your code in the doGet or doPost that is related to the call to your worker bean?
Its nothing but a simple if condition .
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
well, you are creating the instance(DTO) in servlet and setting a value. where you are getting that? As vijitha said,explain your problem clearly,if you want to solve[get answare] your problem.
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
where you are getting that
Oh i am getting that inside my normal java class
But its returning me null .
Its a 3000 lines of code and relates to Spatial Data and Mapviewer , so could not post all that , so just explained in simple way .
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Ravi Kiran V wrote:
Hmm.dont create a new instance . use the existing one(in which you set the value) .please google for "instance vs class"
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
so please tell me how can the communication from my servlet class to the java class can happen ??
Okay so in your servlet you are doing this
And in your java class you are doing this
And you are expecting the value sld or sdo to be returned?? You just created a new object in your java class, how will the value magically transfer from your servlet to the normal class?? Can you show us the code where you are calling the normal java file in your servlet. You'll need to pass the FlagBean instance that you create in your servlet to the normal java class for the value to be able to reach the normal java class...
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
i dont know that thank you i will try that on monday and let you know that status .
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
You'll need to pass the FlagBean instance that you create in your servlet to the normal java class for the value to be able to reach the normal java class...
so Ankit by the above you mean that , don't create new class of FlagBean inside the java class but just call the getter Method it (Am i right to understand this properly ?)
Inside my Java class
FlagBean fb = new FlagBean()
but just fb.getMethod()
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
Ravi Kiran V wrote:.... don't create new class of FlagBean inside the java class but just call the getter Method it (Am i right to understand this properly ?)
Inside my Java class
FlagBean fb = new FlagBean()
but just fb.getMethod()
You should call the getter of the object you have set the values in, perhaps passing that reference to your java class.
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
You should call the getter of the object you have set the values in, perhaps passing that reference to your java class.
Hi i did as per you said but without creating an instance of FlagBean inside my second java class it is not compiling .
For example :
Its inot understanding what is meant by fb .
How can i do this ??
|
 |
Saifuddin Merchant
Ranch Hand
Joined: Feb 08, 2009
Posts: 576
|
|
Lot of confusion over this one - so let me try taking up one point at a time,
Ravi Kiran V wrote:Hi ,
I am having a jsp page for submitting values .
--Sam - Right this works
All these values are recievied by a servlet .
--Sam - I think we are good up to this point too
This servlet in turn calls a normal java file
(This is the flow of our application)
--Sam - This is where the problem is - so lets clear up a few things.
There is no such thing as calling a Java File - unless you are using Runtime class to execute the file or something.
What you can do though is create an Object of the plain Java class and call a method or call static method of this plain java class and pass your bean as a parameter to the method!
It will be interesting to know what you are doing currently - I would suggest that you post your entire Servlet class if you are looking for more help!!
|
Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
All of you thank you very much .
I have made all the methods and properties to static and its working (without creating an instance of that Bean class)
|
 |
Saifuddin Merchant
Ranch Hand
Joined: Feb 08, 2009
Posts: 576
|
|
Ravi Kiran V wrote:All of you thank you very much .
I have made all the methods and properties to static and its working (without creating an instance of that Bean class)
Might work - but it could be a problem for a web application!! Does it work when you have more that one simultanous request?
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
seems interesting sam . ( I did not test for simultaneous requests actually )
But can you please tell me what will be the problem actually .
|
 |
Saifuddin Merchant
Ranch Hand
Joined: Feb 08, 2009
Posts: 576
|
|
Ravi Kiran V wrote:seems interesting sam . ( I did not test for simultaneous requests actually )
But can you please tell me what will be the problem actually .
When you use static properties - you are just working with one copy of the variables. So if one request sets the value to 1 & another sets it to 2 the first value will get over written. Remember the container is going to span different threads to handle each incoming request - so you might end up with cases where you were expecting some value and ended up with another - and these kind of bugs might not even show up when you test the application under low load!
|
 |
gaurav kant
Greenhorn
Joined: Oct 06, 2008
Posts: 14
|
|
Why don't you do things in this way.
lets take an example :
suppose you have 1 text field named "myText" in jsp. you submitted form to your servlet in which you have called a java class.
Now myText comes in your servlet inside request object. you do 1 thing. pass this complete "request object" inside your java class constructor and assign it to instance variable and then use it in your methods.
some thing like this
Servlet side code:
MyClass side code:
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Thank You sam for your nice explanatation
Gaurav i don't know that we can use HttpServletRequest inside a normal java class (That is our class without extending any Generic Or Http Servlet)
i will certanily this .
Thank you Java Ranch
|
 |
gaurav kant
Greenhorn
Joined: Oct 06, 2008
Posts: 14
|
|
Yes, you can do. Just import javax.servlet.http.HttpServletRequest and use it. i am sure about it. It will ease your life
|
 |
samir singha
Greenhorn
Joined: Mar 17, 2008
Posts: 23
|
|
why don't you pass the bean object (fb in your case)created in your servlet to your java class through constructor or method parameter of your java class and get the value from bean object.
|
 |
gaurav kant
Greenhorn
Joined: Oct 06, 2008
Posts: 14
|
|
samir singha wrote:why don't you pass the bean object (fb in your case)created in your servlet to your java class through constructor or method parameter of your java class and get the value from bean object.
Both are the same things. either direct fetch values from request or do one more step in which first populate bean then use in another class. Do any way you think is good
|
 |
 |
|
|
subject: servlet setting some value need to retrive that value in a normal java file
|
|
|