I am very new to this and trying to call a method which will update the record depending on the check box user clicks.

now my jsp looks like this

<input TYPE="checkbox" name="<%=as.getAppId()%>" id="<%=as.getAppId()%>"
				 <%=((as.isStatus()==true)?"CHECKED":"")%> onClick="update('<%=as.getAppId()%>')"	/>

and the scripts

function update(appId)
{
	var stat = eval("document.getElementById(appId)");
	var i;
	if (stat.checked == false){
		i =GtwySysMgmtDao .updateAppStatus(appId,"N");	
	}else{
		i = GtwySysMgmtDao .updateAppStatus(appId,"Y");
	}
	
	form.submit();
}

Now i used firebug to debug and saw that its getting to the right place but it never calls the method.

I used imports to get the class
<%@ page import="org.nipr.gateway_mgmt.GtwySysMgmtDao"%>

and method in GtwySysMgmtDao class is a static method.

In internet explorer i get an error when i click on the check box.

it says
GtwySysMgmtDao is undefined.

Please help. what am I doing wrong here?

Recommended Answers

All 2 Replies

Akabir77,

It's not possible to call java/JSP function directly from javascript (same with php, asp, perl, ssi etc). Reason being javascript runs client-side (ie. in the browser); JSP runs server-side. No executable jsp is propogated client-side. Once your page has been served, the recent import of org.nipr.gateway_mgmt.GtwySysMgmtDao into the server-side script that generated it, is completely irrelevant (other than its contribution to composing the current page). From what you say, GtwySysMgmtDao appears to be method of a java/JSP class, and is therefore not directly available to javascript.

However ......

The main means (in this context) of communicating from client to server is the HTTP request, ie. composing and submitting an HTTP URL to call a server-side script (eg. mypage.jsp ). This can be done in one of the following ways:

  • From an HTML tag of the form <a href="mypage.jsp">...</a> .
  • By submitting an HTML form; <form action="mypage.jsp">...</form> .
  • From javascript with eg. location='mypage.jsp'; , location.href='mypage.jsp'; , or location.replace('mypage.jsp'); .
  • From javascript with an "AJAX" request. This differs from the other approaches in that it does not (necessarily) cause the page to be renewed/reloaded, but may cause some part of it to be updated.

Anything else is a variation of one or more of these.

Armed with this knowledge, I'm sure you will appreciate that by including <%@ page import="org.nipr.gateway_mgmt.GtwySysMgmtDao"%> in mypage.jsp , GtwySysMgmtDao() will become available to be called within mypage.jsp .

The easiest way to pass parameters (eg. the state(s) of your checkbox(s)) is to submit the form (see item 2 in the list above). This will make the value/states of all form elements available to the mypage.jsp script.

Thus, you can this reduce your javascript to :

function update(form){
	form.submit();
}

and your HTML to :

<input TYPE="checkbox" name="<%=as.getAppId()%>" id="<%=as.getAppId()%>"
	<%=((as.isStatus()==true)?"CHECKED":"")%> onClick="update(this.form)" />

Airshow

Thanks Airshow

for your detail answer. I knew this duh... When I was studing they taught us this... but couldn't apply it to the real life... But i think I won't be forgetting this ever... lol

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.