1

I am fairly new to java and am trying to get the solution posted here working: How to use Servlets and Ajax?, but the jQuery call is not executing the servlet.

Servlet code is as follows:

package com.morley.app;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AjaxTest extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String text = "some text";
    System.out.println("Im working");
    response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
    response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
    response.getWriter().write(text);       // Write response body.
}
}

My web.xml mappings are as follows:

<servlet>    
    <servlet-name>AjaxTest</servlet-name>
    <servlet-class>com.test.app.AjaxTest</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>AjaxTest</servlet-name>
    <url-pattern>/AjaxTest</url-pattern>
</servlet-mapping>

And my jsp is as follows:

<head>
    <title>SO question 4112686</title>

    <script type="text/javascript" src="<%= request.getContextPath() %>/js/jquery-latest.min.js"></script>

    <script>
        $(document).ready(function() {                        // When the HTML DOM is ready loading, then execute the following function...

            $('#somebutton').click(function() {               // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event...

                $.get('AjaxTest', function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
                    //alert('ready');
                    $('#somediv').text(responseText);         // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
                });

            });    
        });
    </script>

</head>
<body>
    <button id="somebutton">press here</button>
    <div id="somediv"></div>
</body>

If anyone can help me figure out why the doGet isn't firing it would greatly appreciated

Community
  • 1
  • 1
Steve Salowitz
  • 1,283
  • 1
  • 14
  • 28
  • 1
    Have you tried to see if the ajax command is being sent, and if its going to the correct address? In chrome do ctrl+shift+c to open console window and go into network to help you debug. (Firefox has firebug for this) – t.pimentel Aug 08 '14 at 14:15
  • Did you try $.get('/AjaxTest' ....) - slash in front of AjaxTest – jny Aug 08 '14 at 14:20
  • It should work without the slash. Did you restart the server after creating the ServLet? .jsp files "start" on their own, but for ServLets you need to restart the server to get them up and running. – t.pimentel Aug 08 '14 at 14:22
  • 1
    Thx for the tip @t.pimentel . I opened the console and my button was trying to access the servlet at the wrong location. If you add something as an answer I will flag it accordingly. – Steve Salowitz Aug 08 '14 at 14:32

1 Answers1

2

Check if the ajax command is being executed and is sending the request to the correct address.

You can check it in chrome by doing ctrl+shift+c to open console window and going into network to help you debug. (Firefox has firebug for this).

Ps: Restart the server after creating the ServLet. JSP files start on their own, but for servlets you need to restart the server to get them up and running.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
t.pimentel
  • 1,465
  • 3
  • 17
  • 24