警报窗口中从服务器到客户端的消息

时间:2015-02-04 11:37:36

标签: java eclipse web

我有这个

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    char[] charArray = reverse(request.getParameter("field").toCharArray());
    PrintWriter out = response.getWriter();

}

public char[] reverse(char[] array){
    int id = 0;
    char[] newArr = new char[array.length];
    for(int i=array.length-1; i >= 0; i--){
        newArr[id] = array[i];
        id++;
    }
    return newArr;      
}

如何将charArray交给AlertWindow中的主WebPage? 请帮忙。

2 个答案:

答案 0 :(得分:1)

如果您使用的是JSP,则可以使用以下代码:

request.setAttribute(xyz,reverse(abc).toString);

上面的代码将在请求对象中设置属性,然后在JSP中执行以下操作:

document.ready(){
    alert(<%out.write(request.getAttribute(xyz))%>);
}

答案 1 :(得分:0)

您可以使用Ajax调用您的servlet,如下例所示.... 在你的jsp写下javascript

<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
    <script>
        function callServelet(){  
          $.ajax({  
            type: "GET",  
            url: "Controler(your servelet name)?field=("your field to process")",
            success : function(responseText) {
            alert(responseText);
            }
          });  
        }   
    </script> 
上的

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    char[] charArray = reverse(request.getParameter("field").toCharArray());
    PrintWriter out = response.getWriter();
    out.write(charArray);
 }