0

I am currently working on a simple web App project with Oracle JDeveloper.

I have and input field where the user can provide its user name. As soon as focus is moved out of it, jquery AJAX method will execute and call the servlet and process the response.

index.jsp

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery, Ajax and Servlet/JSP integration example</title>

<script src="https://code.jquery.com/jquery-1.10.2.js"
    type="text/javascript"></script>
<script src="js/app-ajax.js" type="text/javascript"></script>
</head>
<body>

    <form>
        Enter Your Name: <input type="text" id="userName" />
    </form>
    <br>
    <br>

    <strong>Ajax Response</strong>:
    <div id="ajaxGetUserServletResponse"></div>
</body>
</html>

app-ajax.js

$(document).ready(function() {
    $('#userName').blur(function() {
        $.ajax({
            url : 'GetUserServlet',
            data : {
                userName : $('#userName').val()
            },
            success : function(responseText) {
                $('#ajaxGetUserServletResponse').text(responseText);
            }
        });
    });
});

Then the servlet: GetUserServlet.java


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/GetUserServlet")
public class GetUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String userName = request.getParameter("userName").trim();
        if(userName == null || "".equals(userName)){
            userName = "Guest";
        }

        String greetings = "Hello " + userName;

        response.setContentType("text/plain");
        response.getWriter().write(greetings);
    }

}

After providing the user name and when the focus is moved out I get the error: 404 Not Found

proera
  • 123
  • 1
  • 3
  • 14
  • Hi, I am not that much familiar with AJAX, but maybe this will be a hint of some meaning. You can always System.out.println something in the servlet code to see if it gets called. If not, then the issue may be caused by the URL reference in the app-ajax (try preceding it with "/") or the fact that Servlet calls are by default synchronous and AJAX is async. – ptr92zet Mar 09 '20 at 10:28
  • I have tried a System.out.println in the servlet code and it doesn't get called. – proera Mar 09 '20 at 10:30
  • I edited the first comment. – ptr92zet Mar 09 '20 at 10:33
  • (1) What exactly is the URL of the index.jsp (i.e. the browser location bar) (2) Open the browser's console in the network tab; where exactly is it sending the request? (3) Are you sure the servlet is deployed? Override the servlet's `init()` method and print something in the log to check it is there. (4) Is the servlet in the default package, i.e. no `package` declaration in the .java file? Sometimes this causes problems, try putting it in a proper package. – Nikos Paraskevopoulos Mar 09 '20 at 10:36
  • (1) http://localhost:7101/ViewController/venda/index.jsp (2) is it sending the request to http://localhost:7101/GetUserServlet?userName=Thomas – proera Mar 09 '20 at 10:41
  • 2
    HTTP error code 404 basically means "this URL does not exist". Please figure out the correct URL of the servlet by attempting to test it individually and then correct the URL accordingly in your JS code. So far it looks like that the URL should be `/ViewController/GetUserServlet`, provided that it didn't fail to deploy. See the duplicate. – BalusC Mar 09 '20 at 12:52
  • Yeah @BalusC you are right. The Url should be /ViewController/GetUserServlet. Thanks a lot. – proera Mar 09 '20 at 12:58

0 Answers0