Solved

Is there any way to make a ajax request to a java file(Servlet) from a application running in PHP

Posted on 2013-10-27
19
426 Views
Last Modified: 2014-03-31
Dear Everybody,

I want to know, where there is any way to make a ajax request to a servlet from a php application. I have PHP file in which i plan to write the ajax code  and its processing pahe will be a java sevlet file. Is this possible? if so how can i implement it.
0
Comment
Question by:Jacobbaby
  • 5
  • 5
  • 3
  • +3
19 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 167 total points
ID: 39604825
AJAX requests are done from the client's web browser after PHP runs on the server.  Any file you can do an AJAX GET or POST to can be the target of an AJAX request.  Except for the target file, implementation is the same as any other AJAX request.
0
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 83 total points
ID: 39604878
As davebaldwin says the source is immatterial (PHP, ASP etc) - the only consideration is the javascript code to implement the AJAX request.

So if you can write the javascript to AJAX the servlet - all that is required is to include this in the ouptut generated by the PHP script.
0
 

Author Comment

by:Jacobbaby
ID: 39604894
Thank you

I want to know the implementation part, my application i want to develop will be in php and i want to make ajax request to servlet. Can anyone help me to accomplish the task.
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 167 total points
ID: 39604902
AJAX stands for Asynchronous Javascript And XML.  It has nothing to do with PHP.  Here's a tutorial about AJAX: http://www.w3schools.com/ajax/default.asp
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 166 total points
ID: 39605151
AJAX allows you to make a client-side request for information one your page has loaded. This is done using Javascript.

If you want to make a request for information in PHP then you use different methods (not AJAX) such as cURL, or the file_get_contents() function.

Maybe explain, in simple terms, what you're tying to do and awe can help some more.
0
 

Author Comment

by:Jacobbaby
ID: 39605235
Thank you,

Currently I have an application in PHP and MySQL. In-order to increase the performance I am planning to implement java as its back-end along with php to do database operation. I am using jquery ajax for fetching the data.

The thing I want to know, how can use java servlet as processing page for jquery ajax request.
I have tried it with this,

function myfunction() {



$.ajax({                                                      
      type: "POST",
      url: "http://192.168.1.200:8080/HelloWorld",
      dataType: "html",
      success: function (data) {
            alert("haiii1121212");
      }
});

}      
            

but i receive an error like this "NetworkError: 405 Method Not Allowed - http://192.168.1.200:8080/HelloWorld"  in Mozilla fire-bug.
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 166 total points
ID: 39605246
If you're using AJAX to request data from the server, why not make your request directly to a PHP script that reads the database info and passes that back.

Is there a particular reason why you are requesting data from Java, which in turn requests data from PHP? Seems counter-intuitive to gaining performance...
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39605293
It sounds like what you want is an API, not an AJAX request.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12239-Introduction-to-Application-Programming-Interfaces.html

Sidebar note:  You don't need to use Java to improve performance of systems at scale.  Facebook is written in PHP.
0
 
LVL 9

Expert Comment

by:rinfo
ID: 39605295
NetworkError: 405 Method Not Allowed means that server is not allowing request method -post get or put implemented by you.
Besides is http://192.168.1.200:8080 (Seems more like a local network address) your server , i am not sure. And unless this is your server how to you expect it to execute request.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39605315
If you really want an AJAX request and not an API, then this article may provide the guidance.  It uses a PHP script for the server-side response, but you could put your Java application at the request URI instead.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39606155
What happens if you put "http://192.168.1.200:8080/HelloWorld" in your browser?  Do you get a response or an error?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 84 total points
ID: 39607215
It may be as simple as omitting $(document).ready(function(){.  If we can see the entire HTML/jQuery document we might be able to help debug.

Also, alert("haiii1121212"); is kind of useless with AJAX.  You would want to see the data that came back from the server.
0
 

Author Comment

by:Jacobbaby
ID: 39613411
Thank you All,

@ DaveBaldwin

When i put "http://192.168.1.200:8080/HelloWorld" in browser everything works fine. When i am using it with ajax it's not working.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39613414
Putting that in your address bar performs a GET, not a POST.  Try using GET instead of POST in your jQuery.
0
 

Author Comment

by:Jacobbaby
ID: 39613422
Hi,

@ DaveBaldwin

Thank you for your response. I have already tried it. It's not working that's why changed GET to POST and tried and also i am not accepting any parameters just i want to display some hard coded message and it is not working. I have included my servlet code below,

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class HelloWorld extends HttpServlet {

  /**
   * A simple HelloWorld Servlet
   */
  public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws java.io.IOException {
   //res.setContentType("text/html");
   //res.getWriter().write("Hello World!");

    //System.out.println("Hello World!");
    //System.out.close();

      res.setContentType("text/plain");
      PrintWriter ajaxWriter = res.getWriter();
      ajaxWriter.write("ok dear thank you ! 121332323");
      //ajaxWriter.flush();
      //ajaxWriter.close();  
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws java.io.IOException {
    doPost(req, res);
  }
}

Thank you
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39613448
Does using GET eliminate the network error response?  And what happens when you use the original "Hello World" code?
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39613836
Is the java script on the same server as your page?
0
 

Author Comment

by:Jacobbaby
ID: 39613904
Hi,

@ChrisStanyon

As i have mentioned in beginning i have two servers running in my local machine. Apache in its web root my php page is running and Apache-tomcat server in its document root my servlet code is running. In the PHP file i have the ajax code which requests for the servlet page.

Waiting for your reply
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now