0

I have a client project (dynamic web project having just a jsp page) which is running on PC1, and a server project (dynamic web project having a java class having HTTP methods) running on PC2.

I want to be able to call server's GET or DELETE method by invoking my client project which is on PC1.

I know I have to make use of PC2's IP address in client project, and I have tried that as well but it's not properly working.

Let me first attach both the codes below:

CLIENT PROJECT (client.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 http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>GET user</title>
<script>
function loadResponse()
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
    };
    xmlhttp.open('DELETE','http://PC2_IP_ADDRESS:8080/ServerProject/app/user/service/deleteUser',true);
    //I've written actual PC2 IP address instead of PC2_IP_ADDRESS in my code
    xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv">The response text would come here</div>
<button type ="button" onclick="loadResponse()">submit</button>
</body>
</html>

SERVER PROJECT (UserServices.java)

package com.service.user;
import javax.ws.rs.*;

@Path("/user/service")
public class UserServices {

    @GET
    @Path("/getUser")
    public void getUser()
    {
        System.out.println("Inside GET user method");
    }

    @Path("/deleteUser")
    @DELETE
    public void deleteUser()
    {
        System.out.println("Inside DELETE user method");
    }
}

When I run client.jsp, I can see some activity on console(in PC2), but it just doesn't print

Inside DELETE user method

What am I doing wrong?

The console activity is attached below

Sep 15, 2016 4:53:04 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages: 
  com.service.user
Sep 15, 2016 4:53:05 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.service.user.UserServices
Sep 15, 2016 4:53:05 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Sep 15, 2016 4:53:05 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.17.1 02/28/2013 12:47 PM'
Sep 15, 2016 4:53:05 PM com.sun.jersey.spi.inject.Errors processErrorMessages
WARNING: The following warnings have been detected with resource and/or provider classes:
WARNING: A HTTP GET method, public void com.service.user.UserServices.getUser(), MUST return a non-void type.
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
DD25
  • 93
  • 1
  • 8
  • Both PC's have to be on the same network. How did you get the IP you are referring to? – Rishi Goel Sep 15 '16 at 10:02
  • Saying **I can see some activity on console(in PC2)** is not enough. What exact activity you see? Do you see any errors, on JSP or REST side? – Sabir Khan Sep 15 '16 at 10:07
  • Have you try to access to http://PC2_IP_ADDRESS:8080/ServerProject/app/user/service/getUser from a navigator on PC1 to see if it works? – Nicolas Filotto Sep 15 '16 at 10:09
  • lemme add what i'm getting in console as well – DD25 Sep 15 '16 at 10:20
  • I checked both PCs IP through ipconfig command on cmd And both are using same wifi network – DD25 Sep 15 '16 at 10:34
  • Able to ping PC2 from PC1 ? – Vinay Veluri Sep 15 '16 at 10:45
  • @SabirKhan see now the console activity has been added – DD25 Sep 15 '16 at 11:57
  • @VinayVeluri yes successfully pinged – DD25 Sep 15 '16 at 11:58
  • do you have any logger ( like log4j etc ) deployment on service side? Also, which server you use? – Sabir Khan Sep 15 '16 at 12:14
  • I'm using tomcat, and no idk about that @SabirKhan – DD25 Sep 15 '16 at 16:05
  • Possible duplicate of [jQuery AJAX cross domain](http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) – Xavier J Sep 15 '16 at 16:47
  • Few pointers that I can give - flushing of output required, sysout is redirected to somewhere else.Instead of using your jsp client, try using any other popular rest client and make your methods return some response etc. [See Question](http://stackoverflow.com/questions/3302060/system-out-println-not-functioning) – Sabir Khan Sep 16 '16 at 03:45
  • Install a REST client [like this](http://restclient.net/) on PC1 and change your code to return some meaningful response and see if client is getting any response. If you are getting a response then there is an issue with `System.out.println` and not your service. If no response then connectivity issue. – Sabir Khan Sep 16 '16 at 03:49

1 Answers1

0

You can't do an ajax call from site A to site B without more configuration, because of a security concern called cross-site scripting. Essentially, the browser won't allow it by default. Here's a Stackoverflow remedy on how to fix it

Community
  • 1
  • 1
Xavier J
  • 4,326
  • 1
  • 14
  • 25