Ilya Beleychev
Ilya Beleychev

Reputation: 11

Message from server to client in Alert window

I have this

    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;      
}

How can i hand off charArray to the main WebPage in AlertWindow? Please, help.

Upvotes: 0

Views: 105

Answers (2)

Keval
Keval

Reputation: 1859

You can do with Ajax call to your servlet like below example.... In your jsp write below 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> 

on your servlet

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

Upvotes: 0

Kartik73
Kartik73

Reputation: 513

If you are using JSP than you can use following code:

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

above code will set attribute in request object then do below in your JSP:

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

Upvotes: 1

Related Questions