简体   繁体   中英

I want to use java variable in single jsp page

<%!
    public void runJavaMethod(int id)
    {
    %>
    <%
        try{
            String icd = request.getParameter("icd");
            String inm = request.getParameter("inm");
            String istk = request.getParameter("istock");
            String sstk = request.getParameter("sstock");
            String upr = request.getParameter("uprice");
            String spr = request.getParameter("sprice");

            r = s.executeQuery("select * from itemsyncdata");
            while(r.next())
            {
                s.executeUpdate("update itemsyncdata set itemcode='"+icd+"',itemname='"+inm+"',instock='"+istk+"',storestock='"+sstk+"',unitprice='"+upr+"',storeprice='"+spr+"' where id='"+a+"'");
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    %>
    <%!} %>

And I am Calling Function from html like

<input type="submit" id="btnSync" value="Sync" class="button" name="Sync" onclick="<%runJavaMethod(r.getInt(1));%>"/>

So We want runJavaMethod Parameter.

onclick="<%runJavaMethod(r.getInt(1));%>"/>

HTML/Javascript Plays on client side and JSP/Java plays on server side.

Simply you can't. You might misunderstand that JSP and HTML/JavaScript existed on same document. Yes but JSP part compiles on server side itself comes to client. What you can do is you have to make a server request. Most probably look at Ajax requests.

You are trying to mix up two languages ie, Java and Javascript/html together ie, onclick is a Javascript event and you can't call runJavaMethod from Javascript.

In simple words, you can't directly call a Java method (present inside the scriptlet) using Javascript because all of your JSP code produces (becomes) html when it is loaded from the server .

So, if you have to fix the issue, upon onclick , you need to call an URL which hits a servlet/controller method on the server to do the job (ie, executes the business logic).

One more important point is that Scriptlets are legacy code and I strongly suggest not use them to generate or manage the html content, rather use JSTL so that there will be a clear separation between the concerns (ie, business logic and user interface requirements) .

Also, I strongly suggest you read the JSP best practices from here and follow them.

You are mixing to two different things. JSP is server side code and it's rendered response is the HTML that is send back to the browser.Javascript is pure client side in your case.

If you really want invoke a server side processing than create a simple Java script function with Ajax call and get response back which u can use it.

I suggest send all the logic of JSP in backend class not a good practice to put in the jsp. JSP is ideally for UI design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM