Re: Printing Problem
In article <gu4kd0$i8a$1@news.albasani.net>, Lew <noone@lewscanon.com>
wrote:
[...]
Perhaps three seconds isn't long enough for the execution of the
batch file to complete?
You'd be better off actually waiting for the execution than guessing
about the timing.
<http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor()>
stdInput = new BufferedReader(
new InputStreamReader(p.getInputStream ()));
Even then the PDFCreator is just seen running without giving output
when called through servlet.
Interestingly, you kill the subprocess before you try to read its
output. I've not used 'Process', but that seems suspect to me.
Same here. I've used Process, although never from a servlet. Out of
curiosity, I tried it. No problem, as long as one resists the temptation
to make "cmd" a request parameter:
<code>
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** @author John B. Matthews */
public class Test extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Exec";
out.println("<html>");
out.println("<head>");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<h2>" + title + ": " + new Date() + "</h2>");
doExec("ls", out);
out.println("</body>");
out.println("</html>");
}
private void doExec(String cmd, PrintWriter out) {
String s;
try {
Process p = Runtime.getRuntime().exec(cmd);
// read from the process's stdout
BufferedReader stdout = new BufferedReader (
new InputStreamReader(p.getInputStream()));
while ((s = stdout.readLine()) != null) {
out.println(s + "<br>");
}
// read from the process's stderr
BufferedReader stderr = new BufferedReader (
new InputStreamReader(p.getErrorStream()));
while ((s = stderr.readLine()) != null) {
out.println(s + "<br>");
}
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
out.println("Exit value: " + p.waitFor() + "<br>");
}
catch (Exception e) {
e.printStackTrace(out);
}
}
}
</code>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>