Eduard Bakeš
Eduard Bakeš

Reputation: 194

Call Java method from JSP file directly

I have JSP file, in which i have created instance of java class SeznamPolozek. SeznamPolozek is encapsulated HashSet. After that I am printing each entry from SeznamPolozek. This runs OK. But: I want on every line button Remove which calls method of SeznamPolozek and removes element on the line. But i cant get it working. Please help.

<%@page import="java.util.Iterator"%>
<%@page import="model.Polozka"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="model.SeznamPolozek" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>


    <table border="1">
        <thead>
            <tr>
                <th>ID</th>
                <th>Cena</th>
                <th>Rozměr x</th>
                <th>Rozměr y</th>
                <th>Rozměr z</th>
                <th>Název</th>
                <th>Popis</th>
                <th>Adresát</th>
                <th>Stav objednávky</th>
                <th>Vymazat</th>
            </tr>
        </thead>
        <tbody>
             <tr>
                 <%
        SeznamPolozek seznam = new SeznamPolozek();
        Iterator<Polozka> it = seznam.iterator();
        session.setAttribute("it", it);
       for(Polozka polozka:seznam){
       out.print("<td>"+polozka.getId()+"</td>");
       out.print("<td>"+polozka.getCena()+"</td>");
       out.print("<td>"+polozka.getX()+"</td>");
       out.print("<td>"+polozka.getY()+"</td>");
       out.print("<td>"+polozka.getZ()+"</td>");
       out.print("<td>"+polozka.getNazev()+"</td>");
       out.print("<td>"+polozka.getPopis()+"</td>");
       out.print("<td>"+polozka.getAdresat()+"</td>");
       out.print("<td>"+polozka.getStavObjednavky()+"</td>");
       out.print("<td><input type = \"submit\" value = \"a\" name = \"b\"  onclick=\""+seznam.odeberPolozku(polozka.getId())+"\" /></td>");
       }

    %>
     </tr>

            <c:forEach items="${it}" var="item">
                <tr>
                    <td>${item.id}</td>
                    <td>${item.cena}</td>
                    <td>${item.x}</td>
                    <td>${item.y}</td>
                    <td>${item.z}</td>
                    <td>${item.nazev}</td>
                    <td>${item.popis}</td>
                    <td>${item.adresat}</td>
                    <td>${item.stavObjednavky}</td>
                    <td><input type="submit" value="a" name="b" onclick="<%=seznam.odeberPolozku(4)%>"/></td>
                </tr>
            </c:forEach>

        </tbody>
    </table>


</body>

Upvotes: 0

Views: 926

Answers (2)

Ujjwal Mishra
Ujjwal Mishra

Reputation: 95

You may try using javascript instead and implement ajax calls to related methods to remove the 'element'. a library like jquery will also help you modify the dom and show the desired effect.

Upvotes: 1

duffymo
duffymo

Reputation: 309008

This is a fundamental misunderstanding of what goes on in a JSP.

You don't call methods on objects. Events are turned into HTTP requests that are executed on the server, which returns an HTTP response to your browser.

A word of advice: This is not the way to write JSPs. If you must do it, learn JSTL. Scriptlets went out in the late 90s soon after scriptlets were introduced. No one should be embedding scriptlet code in a page in this day and age.

Upvotes: 1

Related Questions