Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
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
Medium Priority
?
441 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
18 Comments
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 501 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 61

Assisted Solution

by:Julian Hansen
Julian Hansen earned 249 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 501 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 45

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 498 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 45

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 498 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 111

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
 
LVL 111

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 84

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 111

Accepted Solution

by:
Ray Paseur earned 252 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 84

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 84

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 45

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

578 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