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
422 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 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 167 total points
Comment Utility
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 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 83 total points
Comment Utility
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
Comment Utility
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 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 167 total points
Comment Utility
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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 166 total points
Comment Utility
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
Comment Utility
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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 166 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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 82

Expert Comment

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

Expert Comment

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

Author Comment

by:Jacobbaby
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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 how to look for a specific file type in a local or remote server directory using PHP.

762 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

14 Experts available now in Live!

Get 1:1 Help Now