簡體   English   中英

如何在不離開頁面的情況下從servlet返回字符串?

[英]How do I return a string from a servlet without leaving the page?

這是圖片:

我有一個帶有表單的html / jsp頁面。

    <div id = "divAttributes">
        <form id = 'fid' method = "post" action = "RunQuery">
            Id Number: <input type= "text" name = "foo" id = "txtFoo"/><br/>        
            <input type = "checkbox" id = "chboxZap" value = "zap"/>Foo<br/>
            <input type = "checkbox" id = "chboxBar" value = "bar"/>Bar<br/>
            <input type= "submit" id = "btnSubmit" value = "submit" onclick = "setDisabled('divAttributes', true)"/><br/>
        </form>
    </div>

當用戶按下“提交”按鈕時,我想將表單中包含的信息發送到Servlet,然后該Servlet將進行一些處理並返回一個字符串。

public void doPost(HttpServletRequest request,
                   HttpServletResponse response) throws IOException {

    response.setContentType("text/plain");

    ReturnCode rc = world.hello.MyMainClass.wait(request.getParameter("foo"));

    /*I want to return the RC, which is a bool, a string, and another object, which in this case is a string*/
}

然后,應將該字符串發送到另一個servlet,然后再保存一個文件。

我已經有了servlet來保存文件:

public void doPost(HttpServletRequest request,
                   HttpServletResponse response) throws IOException {
    response.setContentType("text/plain");
    response.setHeader("Content-Disposition",
            "attachment;filename=downloadname.txt");

    ServletContext ctx = getServletContext();

    String s = new String(request.getParameter("data"));

    InputStream is = new ByteArrayInputStream(s.getBytes());

    int read = 0;
    byte[] bytes = new byte[BYTES_DOWNLOAD];

    OutputStream os = response.getOutputStream();

    while ((read = is.read(bytes)) != -1) {
        os.write(bytes, 0, read);
    }
    os.flush();
    os.close();
}

我有兩個問題:

  1. 第二個Servlet在被調用時不會重定向到新頁面。 它只是立即提供下載對話框。 但是,當我調用第一個servlet時,它提供了一個空白的html頁面。 為什么?

  2. 如何將值從servlet返回到調用它的HTML頁面,然后從那里訪問它們?

JavaScript,更具體地說, Ajax將為您提供幫助。 如果在HTML中包含一個具有Ajax良好支持的庫(例如jQuery ),則可以在停留在同一頁面上的同時調用servlet。

因此,無需從按鈕提交表單,而是可以調用一個JavaScript函數,該函數使用嵌套的Ajax帖子到兩個servlet:

function submitForm() {
  $.post( 'url/to/firstServlet', { text: $('#txtFoo').val() }, function(dataFromFirst) {
    $.post( 'url/to/secondServlet', { data: dataFromFirst }, function(dataFromSecond) {
      // handle response from second servlet
    });
  });
}

使用AJAX!

AJAX允許您創建一個Ajax Javascript對象,並將HttpResponse返回到該對象。

例如。

function myAjaxRequest()
{
var xmlhttp; /*our ajax object*/

        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        setVisible('loading', true) ;

        /*this function called when ajax state changes*/
        xmlhttp.onreadystatechange=function()
          {
          /*when the query has completed successfully*/
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                /*now you can do what you want with your response text!*/
                var mystring= xmlhttp.responseText;
                alert(mystring);
            }

          }

        /*the URL query we want to run*/
        var query = "RunQuery?foo="+$('#txtFoo').val();
        alert(query);

        /*AJAX object runs the query*/
        xmlhttp.open("GET", query, true); 
        xmlhttp.send();     
}       

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM