Enigma
Enigma

Reputation: 102

Client not receiving the response from Servlet

I am a beginner in Client Server application and developing very basic one with Java Servlet on Server side and JavaScript on client side. I am using POST request of AJAX on the client side (index.html) like this:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.post("/Serve",
      {
        name: "Donald Duck",
        city: "Duckburg"
      },
      function(data, status) {
        alert("Data: " + data + "\nStatus: " + status);
      }
    );
  });
});
</script>
</head>
<body>

<button>Send an HTTP POST request to a page and get the result back</button>

</body>
</html>

I am using GlassFish ver 4.1 and the Servlet code (myServe.java) looks like this:

public class MyServe extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException{
        PrintWriter out = response.getWriter();
        out.print("HI");
    }

}

Ideally the response should go to data variable in AJAX function and it should display that on button click, but the alert screen just displays the JS code again: enter image description here

Can anybody tell me where I am going wrong? Also web.xml file is this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
    <servlet-name>MyServe</servlet-name>
    <servlet-class>org.MyServe</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyServe</servlet-name>
    <url-pattern>/Serve</url-pattern>
</servlet-mapping>
</web-app>

Upvotes: 2

Views: 391

Answers (2)

Sunil
Sunil

Reputation: 447

ajax request is going to wrong controller please make some correction into ajax request by remove / from Serve

      $("button").click(function(){
        $.post("Serve",
          {
            name: "Donald Duck",
            city: "Duckburg"
          },
          function(data, status) {
            alert("Data: " + data + "\nStatus: " + status);
          }
        );

I think it will help you.

Upvotes: 1

user4872433
user4872433

Reputation: 113

I haven't worked with plain text responses, and xml is recommended, byt you may try this out

public class MyServe extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException{
        response.setContentType("text/plain;charset=UTF-8");
        try(PrintWriter out = response.getWriter();){
        out.print("HI");}
    }
}

I don't know if the client(jquery) expect plain response(with or without header), but for xml types I mentioned, setting the content type is essential.


Also note that the client is calling /Serve/ which is not /Serve where registered in web.xml
Try to call the servlet as full qualified name(like http://[::1]/my_app/Serve) rather than relative(/Serve). If the web-app has not registered as root context, then client will call http:://[::1]/Serve instead of actual/expected http://[::1]/my_app/Serve

Upvotes: 0

Related Questions