Real-time ping result using ajax - java

I'm trying to create a web application which can display the results of a ping command real-time. I'm using JSP in the backend. I'm actually getting the result correctly. But the problem is, the result is not displayed in real-time. The application processes the ping command and dumps the result all at once. What I need is that, the application has to display the result line after line as and when a line of result is obtained.
Here is my code
String ip = request.getParameter("ipaddress");
String pingCmd = "ping -c 3 " + ip;
Runtime runtime = Runtime.getRuntime();
Process p = runtime.exec(pingCmd);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
out.println(inputLine + "<br />");
}
in.close();
What is the change I need to make in this code.
Regards
Sunil Kumar B M

You can do this with a comet servlet. For tomcat 6: http://tomcat.apache.org/tomcat-6.0-doc/aio.html

Related

How to I exit a while loop after starting a process in Java?

Just found this post (and found the code which I'm also pasting below):
java runtime.getruntime() getting output from executing a command line program
My question is, how do I kill the process? It seems that the code blocks in the while loop. I've tried several options like using a boolean, running all the code in a separate thread and stuff like this, but without any success.
I just want to start an Android emulator and kill it whenever I want.
Runtime rt = Runtime.getRuntime();
String[] commands = {"emulator", "-avd", "jenkins",
"-scale", "96dpi", "-dpi-device", "100"};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
Okay.
Use below code to get The Process ID of that current running thread or Process.
String processName =java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
String ProcessID = processName.split("#")[0];//Process Id
Use that Process ID to kill that Process in your CPU.
I think for that purpose you may wish to write any other trigger or any condition in While loop.

Display Sh script output in browser

I have a webappliaction(developed in Jsp & Severlet) that execute the sh script and displays the output in browser only after executing the script but i want to print the output like how its executing in teriminal (one by one for eg ping command).So that user will have the experience like working in unix terminal. My script will run almost one minute(Script to start stop my WAS servers) so the user should not wait till one minute to see the final output . They should see the script started output once they start the process .please find my sample code below.
pb = new ProcessBuilder("/bin/sh",script);
pb.directory(new File(filePath));
p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null){
System.out.println(line);
out.println(line);
}
p.waitFor();
System.out.println ("exit: " + p.exitValue());
out.println("exit: " + p.exitValue());
p.destroy();
out.println("Script Executed");
Please anyone guide me.
Finally i got a solution for my problem, I just added out.flush() after out.println(line);So its flushing output to browser each time in while loop and its looks like unix terminal . Below code did the magic.
pb = new ProcessBuilder("/bin/sh",script);
pb.directory(new File(filePath));
p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null){
System.out.println(line);
out.println(line);
out.flush();
}
p.waitFor();
System.out.println ("exit: " + p.exitValue());
out.println("exit: " + p.exitValue());
p.destroy();
out.println("Script Executed");
Your problem is invisible here in the code example because it works just fine locally (e.g. for System.out). So logic implies that the problem must be somewhere in networking between client and server. Neither do you show how out is created (I guess it is some kind of socket-connected PrintStream) nor how the JSP reads it. What you want is some AJAX approach (XmlHttpRequest or similar) and probably you do not use it, but some "naïve", old-fashioned way. I am not a web developer, but your favourite search engine or some other people here might be able to help you with that part.

how to get the details of USB devices connected to computer using java

I am trying to get the details of USB hardware devices connected to computer but I don't know the native code of windows so is it possible to get the details of hardware connected to computer using JAVA Thanks in advance
vbscript code:
Set HDs = GetObject("winmgmts:(impersonationLevel=impersonate)")
Set colItem=HDs.ExecQuery("Select * from Win32_DiskDrive")
For Each hd In colItem
Wscript.Echo hd.PnPDeviceID & "vigi"
Next
java code:
try {
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch(Exception e){
e.printStackTrace();
}
now i'm trying to get the details using this vbscipt code but when i'm executing this code the error comes no script found
Try using JUsb. The link here provides simple example.

How to execute root process (su) and write/read commands from it in android

i'm trying to execute a command with root privileges, i try this(only for example):
Process process = Runtime.getRuntime().exec(new String[]{"su","-c","whoami"});
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
result += line;
Log.d(TAG,"RESULT:"+result);
}
But i need to call it in a cicle with hundred times, so this way is very slow and always is showing a dialog with the message of the root privileges are granted, so how i can create a sigle su process for write and read consecutively using input and output streams? i know to use both but i dont know how to write and the read the result of the command. Thanks for your help.

Using java to get the current process owner

I want to know the owner of current process in Unix using Java. I want to find the current server's owner name. I tried with running "who am i" command in Runtime.getRuntime().exec(), but its not returning me any results.
String line = "";
Process p = Runtime.getRuntime().exec("who am i");
InputStream iStream = p.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(iStream);
BufferedReader bufReader = new BufferedReader(inputStreamReader);
while ((line = bufReader.readLine()) != null) {
System.out.println("Input "+line);
}
Is there anything wrong with this code or any idea how can I find the owner of current process using Java?
First thing, I think System.getProperty("user.name") should work for that
Second thing, the reason your code is not returning anything is because the command is whoami with NO SPACES so your exec line should be (assuming you are running on windows through cygwin or on a **nix based system)
Process p = Runtime.getRuntime().exec("whoami");

Categories