Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to to call a Java class when a button gets clicked from JSP. inside my JSP file I have the following:

<%
  Object name = session.getAttribute("name");
  Object ext = session.getAttribute("ext");
  DBOps ops = new DBOps();
  ReturnGetDisplayInfo GDI = ops.getDisplayInfo(ext); 
%>

I have a method in DBOps that will delete a certain field so I added a button to teh table that displays the information and now I am trying to call the delete method when the button is clicked. so I tried doing the following but it did not work.

<td><button onclick=<% ops.delete(ext); %>>Delete</button></td>

I was looking at some examples that utilize javascript but it uses defiend functions in teh script rather than calling the Java class.

Thanks in advance

share|improve this question
Search information about javascript and ajax (xhr) – TroyAndAbed 17 hours ago

2 Answers

One exemple of my code in javascript and ajax if it can help you:

On my jsp i have a onClick"changeTimeZone(org)"

Javascript:

function changeTimeZone(org) {

            //Prepare a new ajaxRequest.
            if (window.XMLHttpRequest) 
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange = function() 
            {
                //state 4 is response ready.
                //Status 200 is page found.
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                    //fill the timezone field with the reponse of my servlet.
                    document.getElementById('timeZoneText').value = xmlhttp.responseText;

                }
            };

            //send an ajax request.
            //Go to my servlet
            xmlhttp.open('GET','mainServlet?command=ajax.ChangeTimeZone&Org=' + org.value, true);
            xmlhttp.send();

    }
share|improve this answer
You are calling XMLHttpRequest in your javascript which is ok, but this does not work for calling a different class the script does not even see my classes – user2247823 16 hours ago

You can't do that directly. You need a roundtrip to the server.

The best option for that is AJAX:

  • make a servlet that handles performs the delete request when a certain url is invoked
  • use jQuery (or native XmlHttpRequest) to invoke that url

(DWR - direct web remoting is also an option, which is implemented using AJAX)

share|improve this answer
I am looking at DWR to help do this, can you post a simple example please. I do not have any servlets and I am not sure how to add one in my case. all I have is JSP files and I am calling Java classes and using them just fine using the <% %> tag. its just the button click that I am struggling with – user2247823 16 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.