0

I want to make a ajax call to servlet and print the response. I have written index.html, Assignemnt servlet and web.xml as follows, but I am not getting the alert. What is going wrong?

index.html

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
    <script>
    $(document).ready(function() {
        $("button").click(function() {
            $.get("Assignment", function(data, status) {
                alert("Data: " + data + "\nStatus: " + status);
            });
        });
    });
</script>

<title>Insert title here</title>
</head>
<body>
    <form method="GET" action="Assignment" name="showall">
        <table>
            <tr>
                <td><input type="checkbox" name="id1" /></td>
                <td>Jim</td>
                <td>Knopf</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="id2" /></td>
                <td>Jim</td>
                <td>Bean</td>
            </tr>
        </table>



        <p>
            <button>Send an HTTP GET request to a page and get the
                result back</button>
        </p>
    </form>
</body>
</html>

Servlet:

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        System.err.println("-------++++");
        String data = "Hello World!";
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(data);
        //RequestDispatcher rd = request.getRequestDispatcher("/Assignment");
        RequestDispatcher rd = request.getRequestDispatcher("/index.html");
        rd.forward(request, response);

    }

Servlet mapping:

 <servlet>
    <description>FirstAssignment</description>
    <display-name>Assignment</display-name>
    <servlet-name>Assignment</servlet-name>
    <servlet-class>com.Assignment</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Assignment</servlet-name>
    <url-pattern>/Assignment</url-pattern>
  </servlet-mapping>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1269042
  • 121
  • 1
  • 2
  • 8
  • Have you opened the dev tools to see if the network request is being sent? Have you put a breakpoint in your servlet? – Mark Meyer Dec 28 '12 at 15:43
  • yes System.err.println("-------++++"); is executed – user1269042 Dec 28 '12 at 15:44
  • Good sign. What about the network tab of your dev tools on the web browser? Also what's the purpose of the RequestDispatcher? I thought this was used for redirects – Mark Meyer Dec 28 '12 at 15:45
  • 2
    try removing these two lines `RequestDispatcher rd = request.getRequestDispatcher("/index.html"); rd.forward(request, response);` – wirey00 Dec 28 '12 at 15:47
  • i just want to send response back to same place .. i am not sure this is causing problem ... what should i write there ? – user1269042 Dec 28 '12 at 15:48
  • @user1269042 nothing.. because `response.getWriter().write(data);` is already writing back the response to your page – wirey00 Dec 28 '12 at 16:05

1 Answers1

4

You don't need to forward the response at all. It would override the text/plain response. Remove those lines:

//RequestDispatcher rd = request.getRequestDispatcher("/Assignment");
RequestDispatcher rd = request.getRequestDispatcher("/index.html");
rd.forward(request, response);

See also:


Unrelated to the concrete problem, w3schools tutorials must be taken with a huge bag of salt. For instance, the $("button") selector is a very bad advice because this affects every single <button> element throughout the HTML document while you'd rather like to bind the ajax function to only one specific button identified by some ID.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555