0

Here I am sending data from servlet to jsp using ajax request.But i didn't get any value from servlet on my client side.This is my code

Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String s="msg";
    System.out.print("Servlet");
    response.setContentType("text/plain");
    PrintWriter out=response.getWriter();
    out.print(s);
    out.flush();
    out.close();
}

jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">

<title>Insert title here</title>
<script src="js/jquery-1.11.1.js" type="text/javascript"></script>
<script type="text/javascript">
function poll() {
    setTimeout(function () {
        $.ajax({
            type: 'POST',
            url: 'http://localhost:8080/ajaxtest/testajax',
            success: function (data) {
               alert(data); //DatO ANY PROCESS HERE
               //document.getElementById("testid").value=data;
               //document.write(data)
            },
            complete: poll
        });
    }, 5000);
}
</script>
</head>
<body onload="poll()">
<form><input type="text" name="test" id="testid" value=""></form>
</body>
</html>

output:

enter image description here Am getting empty alert box and "No element found" on the browser debugging tool.

Ganesh kumar S R
  • 541
  • 1
  • 6
  • 10

2 Answers2

0

Your code is working fine for me. Check if you have your jquery.js accessed properly from jsp and also check your web.xml for correct servlet mapping. best to include below line for jquery.js , one less thing to bother about:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

enter image description here

user3487063
  • 3,672
  • 1
  • 17
  • 24
  • I think it's a problem in web.xml.Because i created this project in eclipse without generating web.xml,After that i checked this code with web.xml,it worked.But in my previous case My servlet worked fine with the help of annotation but i didn't get the element on client side.Still I don't understand the root cause of the problem.. – Ganesh kumar S R Aug 03 '14 at 06:04
0

You should set the data, before putting alert box. currently alert box is not getting any data