Forums Register Login

servlet setting some value need to retrive that value in a normal java file

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ??
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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 ...?
+Pie Number of slices to send: Send
 

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 .
+Pie Number of slices to send: Send
 

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 .
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 .
+Pie Number of slices to send: Send
 

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"
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
i dont know that thank you i will try that on monday and let you know that status .

+Pie Number of slices to send: Send
 

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()
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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 ??
+Pie Number of slices to send: Send
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!!
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
seems interesting sam . ( I did not test for simultaneous requests actually )

But can you please tell me what will be the problem actually .
+Pie Number of slices to send: Send
 

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!


+Pie Number of slices to send: Send
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:

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
Yes, you can do. Just import javax.servlet.http.HttpServletRequest and use it. i am sure about it. It will ease your life
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4478 times.
Similar Threads
getting farmula cell values in excel using poi or jexcelapi
Get Object from multipart request
refreshing the page automatically if the configuration file changes
UPS Online Tools
Problem in setting value from one java file to another file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:10:41.