Hi,
I would like to find out how to call javabean from jsp page with passing values upon onChange event. Pls provide example if possible

Dani AI

Generated

Short answer: you cannot run server-side Java directly inside the browser. As explained, the browser-side onChange must send an HTTP request to the server; that server endpoint (a servlet or a JSP controller) instantiates or calls the JavaBean and returns a result. and others were on the right track — use client-side code to call the server — but the usual, robust pattern is AJAX (fetch/XMLHttpRequest) or a normal form submit, not trying to "execute" Java in the client.

A minimal, practical workflow:

  • onChange -> JavaScript reads the value and sends it to a server endpoint (POST for changes, GET for reads).
  • The servlet/controller invokes the JavaBean, does processing, and returns JSON or HTML.
  • JavaScript receives the response and updates the page DOM.

Example client-side (fetch) to call the server:

document.getElementById('mySelect').addEventListener('change', function(e) {
  fetch('/app/BeanServlet', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: 'value=' + encodeURIComponent(e.target.value)
  })
  .then(res => res.json())
  .then(data => { document.getElementById('out').textContent = data.result; })
  .catch(err => console.error('request failed', err));
});

Server-side (servlet) sketch: accept the parameter, call the bean, return JSON. Use a JSON library (Gson/Jackson) to serialize safely rather than hand-rolling JSON.

Troubleshooting notes: use the browser Network/Console tabs to see requests and payloads; set correct Content-Type and charset; watch for CORS if the endpoint is cross-origin; prefer POST for operations that change state; validate and escape input on the server; ensure any bean shared between threads is thread-safe. For a full-page flow you can submit a form to a JSP that uses the bean and forwards to a view, but AJAX gives a smoother UX. This addresses original question and gives a concrete path for the examples requested by and .

Recommended Answers

All 5 Replies

cal ur jsp using a javascript ,cal that script upon onChange u can write ur bean cals in that JSP

cal ur jsp using a javascript ,cal that script upon onChange u can write ur bean cals in that JSP

Can you give a simple example?

onChange="javascript: return calBeanFucntion('yourJsp.jsp');

onChange="javascript: return calBeanFucntion('yourJsp.jsp');

He there!

Shyam I can't see what are you trying to do. Which code do you plan to place in the function "calBeanFunction()"?

In my case, what I need to do is execute a JavaBean each time I catch a Javascript event and I'm really lost to do so. Could you please give a bit of light, please?

Cheers!

You can't execute Java code from a Javascript function directly.
All you can do is have the Javascript generate an Http request to a servlet which can execute the Java code for you and optionally return something which your Javascript could then use to generate content for your page.

It is tricky, but it can be done. Can't tell you how we solved it as the code is not mine to give away.

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.