ycr
ycr

Reputation: 14604

How to parse form data to a client side Java class Using a JSP

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.

Upvotes: 0

Views: 1156

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85789

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:

Upvotes: 1

Related Questions