Dantalian
Dantalian

Reputation: 581

Mysql query with JSP and JQuery without refreshing

I want to generate a SQL query when I press the submit button and show the number of rows it returns without refreshing. I've manage to do this below, but it refreshes and clears everything and the value is always 0. I'm using Tomcat and NetBeans.

JSP:

<div data-role="page" id="realizadas">
    <%@include file="header.jsp"%>
    <div data-role="content">
        <form name="tareasr" id="tareasr" data-mini="true" method="post">
            <fieldset data-mini="true" data-role="controlgroup" data-type="horizontal"> 
                <select data-inline="true" name="vina" id="vina">
                    <option value="0">VIÑAS</option>
                    <%=Funciones.lista("VINA","option")%>
                </select>
            </fieldset>
            <input data-icon="search" type="submit"  value="Buscar" />
        </form>
        <b><span id="msg"></span></b>

        <script>

            $("#tareasr").submit(function() {
            <jsp:useBean id="formHandler" class="ges.FormBean">
                <jsp:setProperty name="formHandler" property="*"/>
            </jsp:useBean>
                var men="Num: ";
                <%Funciones.setSql(formHandler.getVina()); 
                Funciones.count();%>
                men+=<%=Funciones.numF%>;
                alert(men);
              $("#msg").text(men).show().fadeOut(3000);
            });
        </script>
    </div>
</div>

Upvotes: 1

Views: 1314

Answers (1)

JB Nizet
JB Nizet

Reputation: 691943

You're mixing Java code and JavaScript code as if they were the same thing. Java code inside a JSP should be avoided, and if it's there, it must be inside scriptlet tags (<% ... %>). Thsi Java code is executed at server-side, when the JSP is executed and the HTML and JavaScript code is generated.

Then, in the browser, JavaScript code can be executed. This JavaScript code can't invoke methods of Java objects which were used to generate HTML at server-side. Java doesn't exist in the browser. If you need to invoke server-side Java code from the browser, you must send a new HTTP request to the server (by clicking a link, submitting a form, or sending an AJAX request).

Upvotes: 1

Related Questions