6

I am creating a jsp application in Netbeans Ide. I am having problems in calling a java class method in ajax.Is it possible to do so

My java class is something like this:

public class Hello
{
    public String execute(String s)
    {
        return "success";
    }
}

I am not able to figure out how to call the execute method using ajax : My current ajax code is:

var val="test string";
$.ajax({
type: "GET",
url: "http://localhost:8084/Shade/src/java/mail/Main.execute",
data: val,

async: true,
cache: false,
success: function (msg) {

alert("hi");
$(".col-1").html(msg);
});

Thanx in advance :)

4
  • Have a look over : link Commented Apr 11, 2013 at 8:27
  • 1
    Where is the Servlet? Java by itself does not provide HTTP connectivity, you must use a server/framework (Tomcat, Jetty, JBoss). All of them use the Servlet specification, your bussiness class must implement Servlet and be correctly declared in the server. Commented Apr 11, 2013 at 8:29
  • Or, instead of using Servlets, use SOAP (who manages the servlets for you). The first approach should be easier for a newcomer. Commented Apr 11, 2013 at 8:29
  • 1
    A method can be invoked via AJAX (JSON), if you use a RESTFul API like Spring or Struts. Otherwise, you have to send a request to a URL that responds with XML. Commented Apr 11, 2013 at 8:34

2 Answers 2

10

AJAX is an acronym for Asynchronous JavaScript And XML. It provides an ability to communicate with the server asynchronously.

To explain that in simple terms, you can send a request to server and continue user interaction with the user. You need not wait for response from the server. Once the response arrives, a designated area in UI will update itself and reflect the response information. Whole page need not be reloaded.

So, you can not access Java Class directly as url to make your Ajax request. It should any mapped url like JSP, Servlets, PHP etc.

Create a JSP (e.g. hello.jsp)

<%
String strResponse;
mail.Main objMain = new mail.Main();
strResponse = objMain.execute();
%>

<%=strResponse %>

In Ajax request

url: "hello.jsp",

EDIT: Added Example:

<script type="text/javascript" src="js/jquery.min.js"></script> 
<script type="text/javascript">
  $(function(){
      function getData() {
          var dataToBeSent  = {
            uName : $("#userName").val() , //
            passwd: $("#password").val()
            }; // you can change parameter name

          $.ajax({
                url : 'getDataServlet', // Your Servlet mapping or JSP(not suggested)
                data :dataToBeSent, 
                type : 'POST',
                dataType : 'html', // Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.
                success : function(response) {
                    $('#outputDiv').html(response); // create an empty div in your page with some id
                },
                error : function(request, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
      }

});

In Servlet/JSP access your parameters request.getParameter("uName");

Sign up to request clarification or add additional context in comments.

5 Comments

@Baadshah: Thank You.. :). Can you please tell what's that ? So, I can edit.
:) I do not think you don't know may be you forgot.In order to use the java class file,we have to import that in jsp,right?
@Baadshah: As per OP's example I wrote mail.Main where mail is the package name. :)
thankyou. i just needed one thing more that how should i pass my variable data in ajax to my jsp file and hence to java method execute?
How we access uName in servlet, if uName passing in javascript is array object? That is var uName = [{id: 1, value: 'one'}, {id: 2, value: 'two'}];. How we can access object variables too in servlet?
3

You cannot call the method directly. You should map an URL to the method you want to call. This can be done in a servlet. If you're already serving pages through your Java code, you just add a new method to serve a page with the content you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.