1

I have the following javaScript function:

function loadSomething() {
    var xhttp;
    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
        } else {
        // code for IE6, IE5
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhttp.open("GET", "javaservlet.java", true);
    xhttp.send();
}

and I want to get, lets say an int value from a Java Servlet - "javaservlet". What code should I write in javaservlets doGet() method in order to send a value, so I can get and use it in javaScript? Thank you!

Batiaev
  • 1,173
  • 1
  • 14
  • 30
Sargsyan Grigor
  • 533
  • 1
  • 5
  • 22
  • You need to run your java servlet in a servlet container such as tomcat or jetty. The javascript code then triggers a http request to that server. – f1sh May 05 '16 at 12:12

3 Answers3

1

So, you want to return something from your servlet back to the javascript you called that servlet from. Here is the way, make an XMLHttpRequest object using these lines of code

var reqObject = new XMLHttpRequst(); or new ActiveXObject("Microsoft.XMLHTTP");

now make a request to the servlet's get or post method using the XMLHttpRequst's open method, you can simply do it like this

reqObject.open("GET/POST", "ServletName", true);

now if you have made a request to the server and state of the object reqObject is being changed then you will want to see the changes that are being made. Call a function when the state of the object is changed

reqObject.onreadystatechange = processRespose;

if you want to send something as parameter to the servlet use send method otherwise send null.

reqObject.send(null);

now if the servlet is returning something in the method you called from .open the state of the object will be changed and function processResponse will be called.

function processResponse(){
  //check whether the response form the server is intact and correct
   if(reqObject.status==200 && reqObject.readyState==200){
     //simply means we got the response correctly
     //Now you can get the response by
     var res = reqObject.responseText;

   }    
}

you can read about the objects methods and properties here
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

I java servlet you just have to send the expected string with the PrintWriter's object. A rough version of Get method would look somewhat like this

doGet(request, response){
    PrintWriter out = response.getWriter();
    out.println("Javasrvlet");  
 }
viveksinghggits
  • 661
  • 14
  • 35
0

You need to provide url mapping for that servlet in web.xml. I am assuming your servlet class name is JavaServlet.

<servlet>
<description></description>
<display-name>JavaServlet</display-name>
<servlet-name>JavaServlet</servlet-name>
<servlet-class>JavaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JavaServlet</servlet-name>
<url-pattern>/javaServlet</url-pattern>
</servlet-mapping>

Now change the following in javascript code to send GET request to JavaServlet.

 function loadSomething()
{
    var xhttp;
    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
        } else {
        // code for IE6, IE5
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhttp.open("GET", "javaServlet", true);
    xhttp.send();
}
Gaurav Srivastav
  • 2,381
  • 1
  • 15
  • 18
0

You can write like this....

 public void service(HttpServletRequest request, HttpServletResponse){
      response.getWriter().write("<Your Data>");
}