0

Hi I have form that takes user inputs, I want to invoke a java method of a Class that resides in the client side on click.

The relevent code of the JSP is as follows

<%@ page
import="clent.televisionshopclient.TelevisionShopClient"%> //This is the Java class for client
<%TelevisionShopClient client = new TelevisionShopClient(serverURL);%>

This is what I want to achieve

<input type="submit" value="Submit Stock" onclick="<%client.setStock();%>">

How can I do this?? Thanks in Advance.

ycr
  • 12,828
  • 2
  • 25
  • 45

1 Answers1

1

You cannot do that, that's not how Web Application works. If you want to perform an action, you should make a request to the server, which should attend it using a Servlet or another kind of component.

This would be the example to handle the data using plain Servlets:

In your JSP:

<form method="POST" action="${request.contextPath}/YourServlet">
    Stock: <input type="text" name="txtStock" />
    <br />
    <input type="submit" value="Set stock" />
</form>

In your Servlet:

@WebServlet("/YourServlet")
public class YourServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        int stock = Integer.parseInt(request.getParameter("stock"));
        TelevisionShopClient client = new TelevisionShopClient("...");
        //complete the logic...
    }
}

Note that you can ease all this work by using a web application MVC framework like JSF, Spring MVC, Play, Vaadin, GWT or other.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Hi thanks for the reply, But my Java class resides in the client side, Java Class is the actual Client, The above example code I mentioned works fine,,But it executes before even the click is made, How can i execute this "OnClick"? – ycr May 16 '14 at 04:35
  • 2
    @user2627018 any Java code in scriptlets **are not** in client side. They are in server side and only serve to generate the HTML output. Once the HTML is rendered, there's no way to interact with Java (server side) unless you fire a request. – Luiggi Mendoza May 16 '14 at 04:38