0

I want to call servlet when radio button is clicked, how can I make it?

EDIT

I tried to add URL to servlet in javascript function like this

$.post( "ParentManagementServlet", "isActivated"); 

and like this

 $.post(<%=getServletContext().getContextPath()+"/ParentManagementServlet"%>, "isActivated"); 

and like this

  $.post("/ParentManagementServlet?isActivated=true");

but both does not call servlet!

here's the url mapping of the servlet in web.xml

<servlet>
    <servlet-name>ParentManagementServlet</servlet-name>
    <servlet-class>ps.iugaza.onlineinfosys.servlets.ParentManagementServlet</servlet-class>
</servlet>
 <servlet-mapping>
    <servlet-name>ParentManagementServlet</servlet-name>
    <url-pattern>/ParentManagementServlet</url-pattern>
</servlet-mapping>

I usually add the servlet through its name, but I read that it's better to get the servlet absolute path form servlet context.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
palAlaa
  • 9,500
  • 33
  • 107
  • 166
  • @smas, @Bozho, @Jigar Joshi, I tried to add URL to servlet like this $.post( "ParentManagementServlet", "isActivated"); and like this $.post(<%=getServletContext().getContextPath()+"/ParentManagementServlet"%>, "isActivated"); , but both does not call servlet! – palAlaa Apr 29 '11 at 13:58
  • @Alaa: show part with servlet mapping from your web.xml and put your code in question not in comment – lukastymo Apr 29 '11 at 14:00
  • @smas, ok, just a minute – palAlaa Apr 29 '11 at 14:00
  • $.post(uri, data) - data - must be an object (e.g. $.post("/path/to/your/servlet", { name: "John", time: "2pm" } );) – ninja Apr 29 '11 at 14:04
  • @Alaa: you haven't servlet-mapping part in your web.xml. After you add this part test your servlet. Enter URL in browser and check if then it works – lukastymo Apr 29 '11 at 14:06
  • @smas, I add the url mapping part. – palAlaa Apr 29 '11 at 14:09
  • @ninja, I make like this ,$.post(<%=getServletContext().getContextPath()+"/ParentManagementServlet"%>, {isActivated:"true"}); , but not working too. – palAlaa Apr 29 '11 at 14:10
  • 1
    What does Firebug say? What does `success:{}` or `error:{}` handlers of `$.post` say? Is it the URL which returns 404 or is it just the `$.post` which doesn't get called? Please learn how to debug code. What line get executed and what not? What HTTP request get sent and what not? – BalusC Apr 29 '11 at 14:22
  • @BlausC,I make alert before this line, it works , and another alert after it, but doesn't work, so ${post} doesn't call the servlet, about fireBug, here's what returns $ is not defined [Break On This Error] $.post("/ParentManagementServlet?isActivated=true"); – palAlaa Apr 29 '11 at 14:33
  • @BlausC, Does that happen because I am calling it through js function? – palAlaa Apr 29 '11 at 14:36
  • It woooooooooooooooooooooorks I make like this document.location.href = "<%=getServletContext().getContextPath()+"/ParentManagementServlet?isActivated=true"%>"; document.getElementById("stdActivationForm").submit(); – palAlaa Apr 29 '11 at 14:59
  • Oh boy.. You were just stabbing and running headlessly around in the dark. Please take time to buy/read a good book/tutorial. By the way, my nickname is BalusC, not BlausC. – BalusC Apr 29 '11 at 15:33

3 Answers3

1

Use ajax. For example, with jQuery:

...onclick="invokeServlet()"

function invokeServlet() {
    $.post("/path/to/servlet", params);
}
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

To call servlet in time when user select radio (without submit button) use:

  • Add appropriate mapping to your Servlet - Bind some URL to servlet.
  • use onselect attribute to call javascript function, which will redirect to the URL

example:

<input type ="radio" Value="blah blah" onSelect="yourFunction()"/>

In other situation the idea is the same: bind Servlet, choose event which will be trigger the servlet

lukastymo
  • 26,145
  • 14
  • 53
  • 66
1

As per the comments:

about fireBug, here's what returns $ is not defined

The $ is definied by jQuery. You've apparently not declared it in your page. jQuery is a JS library and not something already builtin the browser or something.

Put the following line before any other <script> in your <head>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

(you can of course also download and serve a copy from your own domain)

<script src="jquery-1.5.2.min.js"></script>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That's the start of jQuery library :) many thanx for the "See also" link, sorry for writing ur nickname incorrectly. – palAlaa Apr 29 '11 at 15:58