GeeCON Prague 2014*
The moose likes Servlets and the fly likes How to get dynamic string values generated in a java class in a serlvet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get dynamic string values generated in a java class in a serlvet?" Watch "How to get dynamic string values generated in a java class in a serlvet?" New topic
Author

How to get dynamic string values generated in a java class in a serlvet?

Anil basha
Greenhorn

Joined: Apr 22, 2013
Posts: 8
I'm working on a simple web application. This application has a functionality 'ping' which we usually do from command prompt.

So in my jsp page i'll give an address like 'www.google.com' and click submit to send the address to my servlet called 'PingServlet'. My servlet receives the address and sends to a java class which will processes the pinging of that address.


while processing the address, in while loop it'll produce the some string values, in which i want to send each string value to servlet when it is generated. I've searched google a lot. But i didn't found any idea....

Please Help me out...!
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Hi Anil,

Looks like you are invoking some method of a class from PingServlet and want the processed data/String from that class to be madwe available to another servlet or may be the same servlet.

Now, this String data is generated in loop and sending it to Servlet in a loop is impossible!
Serlvets are not meant for that. Servlets will be processing the client requests and generate responses.

The processing part might be done by some helper classes though.

Now, is there any specific reason you want that String data in loop to be processed by Servlet only and not by the same class or some other helper class?
What is it that you are looking to do in Servlet?

Thanks,
Prajakta


Regards,
Prajakta
Anil basha
Greenhorn

Joined: Apr 22, 2013
Posts: 8
Hi Prajakta.. Thanks for reply,

What i exactly want is, first time when the string is generated in while loop, it should be sent to the servlet and the control should come back and it should continue the while loop
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

My question is...why specifically a servlet?
A helper class wil not have response object to redirect your request to servlet. Also, redirecting response to any other resource ends the cycle of request-response altogether.

No looping can be involved here!!

If you let me know the exact requirenment while your string is getting generated in loop, I might think on those lines.
Anil basha
Greenhorn

Joined: Apr 22, 2013
Posts: 8
I'll explain what exactly i want..

I have a jsp page from where user will enter the address(ex: www.google.com) which should be pinged. After entering address, user will submits the request and that address i'll get in PingServlet.

From servlet, i'll call a java class method which will process the pinging of that particular given address. To process this data, that method is taking a time of 20 seconds(aprox). So user need to wait that much without knowing any status. So i want to show the data which is generating in the while loop dynamically in jsp page.
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Ok...I got it now. I don't think it will be so straightforward to implement this manually.

You will need to include the code in the servlet itself and store the processed string in request scope and dispatch user request to the JSP.
Using Javascript, you will need to autosubmit the page at a specific interval and get the original data back in the servlet and progress from there.
After the processing is complete, servlet will need to set some flag for JSP to identify and stop auto submitting.

I don't know what issues can come up in this flow.

This exact functionality can be achieved by using an 'Execute and Wait' interceptor provided by Struts 2.

Details can be found here:
http://struts.apache.org/release/2.1.x/docs/execute-and-wait-interceptor.html



Let me know how it goes.

Thanks,
Prajakta
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Posted twice by mistake...check my response in previous post.
Anil basha
Greenhorn

Joined: Apr 22, 2013
Posts: 8
I don't know struts at all. I'm using just plain servlets and jsp..!! As you said i'll just keep the java code in servlet itself. In while loop as soon as i get the string i'll forward it to the jsp page using request dispatcher. Since request dispatcher stays in servlet it'll continue to process remaining data also ... right?
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

The question here is, how are you planning to keep track of the processed data?

I'm sure you won't be executing the ping command everytime your JSP refreshes.

in.readLine() will be reading the data from BufferedReader sequentially.
Anil basha
Greenhorn

Joined: Apr 22, 2013
Posts: 8
yes.. it's not working. Once response has been sent to the client, the connection between the server and client is going to be disconnected. Then how can we maintain the processed data in servlet?
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

You can try something like this:



where in.readLine().length() will server as a pointer to the inputstream already read.
This counter will increment each time JSP refreshes and your Servlet code is executed. This counter should be maintained in session.

in.reset() will mark the stream to that pointer and further reading will continue from that point.

Ofcourse once you execute a request dispatcher and JSP is rendered, connection between server and client will be lost and you will need to re-establish the connection by refreshing the JSP. Javascript should have something for auto submitting a page with a specific interval.
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

How are you going about this?

I gave another thought to this and realized that using this approach will need to execute the command on every refresh cycle, so not recommended.
Further thought to this -
Can servlet initiate a thread to execute the command?
I have implemented the solution partly. Here we go:

1) JSP:


2) PingServlet


3) Thread: ProcessCommand


Note: I have just displayed the reponse on standard console.You can set the response as requst attribute and display on your JSP.

Thanks,
Prajakta
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Improved code below:
1) JSP


2) PingServlet


3) ProcessCommand thread
Anil basha
Greenhorn

Joined: Apr 22, 2013
Posts: 8
Hi Acharya,

I have tried the code that you have given. But it's not working. Sorry, I didn't understand what actually happening. You declared progress variable and not using anywhere else in the code. The thread always returning null.
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Yes, the progress variable is no longer required. Missed to remove it. But it won't harm your end result!
Have you tried implementing the latest code? It does work. I have seen the results wherein I see the commands being displayed on JSP one after the other.
Make sure to clear your session before you execute it.

Troubleshoot it at your end. Following this approach has resulted in the desired output on my end.
Anil basha
Greenhorn

Joined: Apr 22, 2013
Posts: 8
I used your updated code prajakta acharya. But i'm not getting output. You are getting output on your side from ProcessCommand.? I'm always getting null value from that class. So nothing is printing on screen.
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

By the way, thread wont always return null.

I think you should at least analyze the code that is given to you.

It is setting some variable in loop which is accessed by the Servlet at specific intervals.
Once processing is over, that variable is nullified for the Servlet to know that command execution is over.
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

I must say Anil Basha, you will not be spoon-fed here.For that matter, anywhere.

If you've got some code developed by someone, you should put in your efforts, analyze it and get to your desired goal rathar than just declaring 'It doesn't work'!

Hope you understand.
As I had mentioned previously, the code works for me after which it has been posted here.

You can try refreshing the page at a faster interval. Quite possible that the code reading commandstatus is out of sync with the pace at which the command is executed.
Are you sure command execution takes few seconds? That was what was mentioned by you earlier.
Anil basha
Greenhorn

Joined: Apr 22, 2013
Posts: 8
Sorry Acharya Prajakta for unnecessary posts. I'm using coderanch since 6 months, but this is my first post in coderanch. So little enthusiastic about posts. That's it. Sorry for that. I analyzed the code that you have given. Everything is fine with your code, but the problem is that in while loop, there will be "<empty strings>" also.

So the pc.getResp method is reading that empty string because it is not null. And the "req.setAttribute("commandRes", inputLine);" is setting empty string, so it is unable to show it in the jsp page. So i modified code little bit.

Now it's working properly.
Thanks,
Anilbasha
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Good to know that is has worked
 
Don't get me started about those stupid light bulbs.
 
subject: How to get dynamic string values generated in a java class in a serlvet?