从servlet doPost()开始一个进程

时间:2016-04-24 10:51:43

标签: java servlets runtime.exec

我从servlet的doPost方法内部调用外部进程(git命令)。当用户单击按钮时,post请求通过xhttpRequest()调用进入servlet。 在我的代码中,我使用响应的writer对象打印出响应HTML。奇怪的是,当代码到达流程部分时,它在后台执行正常,但我没有看到正确的响应。有人对此问题有一些见解吗?我正在使用此链接中描述的streamgobbler类:http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2 doPost()方法代码的流程部分的片段如下所示。

try {
    String line;
    List<String> lines = new ArrayList<String>();
    Process cloneProcess = Runtime.getRuntime().exec("git clone " +
                              hvafModifiedXMLRepository + " " +
                              xmlRepoPath.toString());
    StreamGobbler errorGlobber = new StreamGobbler(cloneProcess.getErrorStream(), "CMDTOOL-E",resp.getWriter());
    StreamGobbler outputGlobber = new StreamGobbler(cloneProcess.getInputStream(), "CMDTOOL-O",resp.getWriter());
    errorGlobber.start();
    outputGlobber.start();
    cloneProcess.waitFor();
    if (cloneProcess.exitValue() != 0)
        throw new IOException(org.apache.commons.lang.StringUtils.join(lines, "\n"));
    System.out.println("Done.");
} catch (IOException ex) {
    sendLogHead(resp);
    sendLog(resp, "Unable to clone Modified XML repository.", ex);
    sendLogTail(resp);
    return;
}

这是我的POST请求。

function sendSelectedTestCasesToServlet(paramData, projName, uniqueInstanceName, configCapabilities) {
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200)
            document.getElementById('content').parentElement.removeChild(document.getElementById('content'));
        document.getElementById('footer').parentElement.removeChild(document.getElementById('footer'));
        var el = document.createElement('html');
        el.innerHTML = xhttp.responseText;
        var newBody = document.createElement('div');
        newBody.innerHTML = AJS.$(el.getElementsByTagName('body')[0]).html();
        document.body.appendChild(newBody);
        window.stop();
    }
    else if (xhttp.readyState == 4 && xhttp.status == 404) {
        return;
    } else if (xhttp.readyState == 4 && xhttp.status == 500) {
        document.getElementById('content').parentElement.removeChild(document.getElementById('content'));
        document.getElementById('footer').parentElement.removeChild(document.getElementById('footer'));
        var el = document.createElement('html');
        el.innerHTML = xhttp.responseText;
        var newBody = document.createElement('div');
        newBody.innerHTML = AJS.$(el.getElementsByTagName('body')[0]).html();
        document.body.appendChild(newBody);
        window.stop();
    }
};var baseurl = window.location.protocol + '//' + window.location.host + new URI(window.location.href).path()
xhttp.open('POST', baseurl + '?test-cases-for-configs=' + paramData + '&project-name=' + projName + '&job-instance-uniqueId=' + uniqueInstanceName + '&havf-config-capabilities=' + configCapabilities, true);
xhttp.send()
}

0 个答案:

没有答案