0

When I click update button, pop-up in jsp(for update) is opening up. Also I am sending good id(Urunler id) from my main.jsp to update.jsp. İt's working, but all time pop-up jsp comes with id from behind. For example I clicked 1. id in my main.jsp and it returns as ?id=1 its ok, but pop-up jsp is with null(sending formatException). After one more click let suppose we click 2. id then my pop-up jsp open up as ?id=1. So id always come from back.

My click code in main.jsp:

<td> 
    <a onclick='popup()' href="managergosterim.jsp?id=<%=user.getUrunId()%>" >Güncelle</a> 
</td>

pop-up jsp stuff in main.jsp :

function popup(){ 
    window.open('update.jsp?<%=request.getQueryString()%>','name','width=500,height=500');
}

This is my main.jsp

<%@page import="entities.Urunler"%>
<%@page import="org.hibernate.Query"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page import="entities.Uyeler"%>
<%@page import="org.hibernate.cfg.AnnotationConfiguration"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MANAGER Ürünler</title>

</head>
<body>
    <divdisplay:block;>
                 <div id="index">
                    <img style="float: right; display:inline-block;  "   src="Manager.png"  alt="Manager" />
                </div>
                <div id="sidebar">
                    <form class="login"   action="logout.jsp" method="Post">           
                        <input type="submit"  style="float:right;  display:inline-block;        margin: 5px; margin-bottom: 0px;  "  class="submit" value=" Çıkış Yap "   />       
                    </form> <br><br>
                    <form class="login"   action="Raporlama" method="Post">           
                        <input type="submit"  style="float:right; margin: 5px; margin-bottom: 0px; display:inline-block;   " class="submit" value=" PDF Oluştur "   />
                    </form> <br><br>
                </div>
            </div> 
            <table border=1 style="float:top" ><tr><th>Marka</th><th>Model</th><th>Fiyat</th><th>Renk</th><th colspan="3">Seçenekler</th> </tr>
            <%
                //**HERE WE RETRIEVE MANAGER DATABASE TABLES.**
                    out.println("<title>MANAGER     Ürünler</title>");
                    HttpSession oturum = request.getSession();
                    AnnotationConfiguration configiki = new AnnotationConfiguration();
                    configiki.addAnnotatedClass(Uyeler.class);
                    SessionFactory factory = configiki.configure().buildSessionFactory();
                    Session sessioniki = factory.getCurrentSession();
                    sessioniki.beginTransaction();
                    Query queryResult = sessioniki.createQuery("from Urunler");
                    java.util.List allUsers;
                    allUsers = queryResult.list();
                    for (int i = 0; i < allUsers.size(); i++) {
                        Urunler user = (Urunler) allUsers.get(i);
            %>
                <tr>
                    <td><%=user.getMarka()%></td>
                    <td> <%=user.getModel()%></td>
                    <td><%=user.getPrice()%></td>
                    <td><%=user.getRenk()%></td>
                    <td> <a onclick='popup()' href="managergosterim.jsp?id=<%=user.getUrunId()%>" >Güncelle</a> </td>
                    <td> <a href="del.jsp?id=<%=user.getUrunId()%>">Sil</a> </td>
                </tr>
            <%
                }
                    //**EACH TABLE ROWS HAVE DELETE(href to DEL.Jsp) AND UPDATE(HREF TO GUNCELLE.JSP) .**
            %>
            </table>
            <script>
                function popup(){       
                    window.open('update.jsp?<%=request.getQueryString()%>','name','width=500,height=500');
                }
            </script>

      </body>
</html>

This is pop-up jsp.

<%@page import="org.hibernate.Query"%>
<%@page import="java.util.List"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page import="entities.Urunler"%>
<%@page import="org.hibernate.cfg.Configuration"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style>
.mdl {
    font-size: 20px;
    font-family: serif;
    margin: 5px;
}
</style>
</head>
<body>
    <h3>Ürünün yeni değerlerini giriniz</h3>
    <form>
        <%!void degistir() {
        HttpServletRequest request = null;
        String marka = request.getParameter("marka");
        String model = request.getParameter("model");
        String fiyat = request.getParameter("fiyat");
        String renk = request.getParameter("renk");
        Configuration confi = new Configuration();
        confi.addAnnotatedClass(Urunler.class);
        SessionFactory factor = confi.configure().buildSessionFactory();
        Session oturumiki = factor.getCurrentSession();
        oturumiki.beginTransaction();
        List allItem;
        Query queryResultz = oturumiki.createQuery("from Urunler");
        allItem = queryResultz.list();
        for (int i = 0; i < allItem.size(); i++) {
            Urunler urun = (Urunler) allItem.get(i);
            if (request.getParameter("id").equals(urun.getUrunId())) {
                urun.setMarka(marka);
                urun.setModel(model);
                urun.setPrice(Integer.parseInt(fiyat));
                urun.setRenk(renk);
            }
        }

    }%> <%
            int gelenid = Integer.valueOf(request.getParameter("id"));
            out.println("gelen id su : " + gelenid);
            Configuration configiki = new Configuration();
            configiki.addAnnotatedClass(Urunler.class);
            SessionFactory factory = configiki.configure().buildSessionFactory();
            Session sessioniki = factory.getCurrentSession();
            sessioniki.beginTransaction();
            List allItems;
            Query queryResult = sessioniki.createQuery("from Urunler");
            allItems = queryResult.list();
            for (int i = 0; i < allItems.size(); i++) {
                Urunler urun = (Urunler) allItems.get(i);
                if (gelenid == urun.getUrunId()) {
        %>

        Marka : <input type="label" class="mdl" name="marka"
            value="<%=urun.getMarka()%>" /><br> Model : <input type="label"
            class="mdl" name="model" value="<%=urun.getModel()%>" /><br>
        Fiyat : &nbsp;&nbsp;<input class="mdl" type="label" name="fiyat"
            value="<%=urun.getPrice()%>" /><br> Renk : &nbsp;&nbsp;<input
            type="label" class="mdl" name="renk" value="<%=urun.getRenk()%>" /> <br>
        <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input
            type="submit" class=mdl value="Değiştir" onClick="degistir()" />


        <%
            }

            }
        %>
    </form>
</body>
</html>

1 Answer 1

1

You have to understand what is the difference between client side and server side. Here is an explanation what really happens in your example scenario:

  1. (Client) Go to /main.jsp (no query params)
  2. (Server) Render main.jsp, but request.getQueryString() is empty, so rendered function popup will be:

    function popup(){       
        window.open('update.jsp?','name','width=500,height=500');
    }
    

    and send response (rendered html) to Client.

  3. (Client) Click on record with id=1 (but it doesn't matter what id it is, because every call of popup function is the same).

  4. (Client) Go to /update.jsp?

  5. (Server) Try to render update.jsp, but there was no query string so, id param is null (it cause exception).

Possible solution:

Change popup definition:

function popup(id){       
     window.open('update.jsp?id='+id,'name','width=500,height=500');
}

and change popup call

<td> 
     <a onclick='popup(<%=user.getUrunId()%>)' href="managergosterim.jsp?id=<%=user.getUrunId()%>" >Güncelle</a> 
</td>
Sign up to request clarification or add additional context in comments.

1 Comment

Ty you very much.Very clear to understand.Appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.