0

I am new at JSP development. I have a code to pass value from one.jsp to second.jsp file. But it's not working for me. Please help me.

one.jsp

try
   {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    con =  DriverManager.getConnection("jdbc:odbc:myDSN");
st= con.createStatement();
rs= st.executeQuery("select id,dr_name,categoryname,cityname,availability from doc_registration");



while(rs.next())
{
doc_id = rs.getInt("id");
doc_name=rs.getString("dr_name");
cat_name=rs.getString("categoryname");
city_name=rs.getString("cityname");
status=rs.getString("availability");
//request.setAttribute("Send",doc_id);
 session.setAttribute("Send", doc_id);
out.println("<tr>");
out.println("<td>"   +  doc_id + "</td>");
out.println("<td>"   +  doc_name + "</td>");
out.println("<td>"   +  cat_name + "</td>");
out.println("<td>"   +  city_name + "</td>");
out.println("<td>"   +  status + "</td>");
out.println("<td> <a href ='update.jsp'><input type='button' value='Update' id='act' onclick=''/></a><a href ='delete.jsp'><input type='button' value='delete' id='deact' onclick=''/></a>");
out.println("</td>");

}

st.close();


con.close();
    }
    catch(Exception e)
   {
        out.println(e);
    }

second.jsp

try
{


    //int did = Integer.parseInt(request.getParameter("doc_id"));

int doc_id = (Integer)session.getAttribute("Send");

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    con2 =DriverManager.getConnection("jdbc:odbc:myDSN");

    st2 =con2.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

    count= st2.executeUpdate("Delete from doc_registration Where id =" + doc_id);

    if(count > 0)
    {

    out.println("<body bgcolor='#ECE5B6'>");
    out.println(count + "Record(s) delete successfully");
    out.println("<a href ='doc_availability.jsp'><input type='button' value='back' id='back' onclick=''/>");


    }
    else
    {
        out.println("id does not exit");
    }
    st2.close();
    con2.close();
}
    catch(Exception e)
    {
        out.println("Exception = " + e);
    }
  • In one jsp page i get the data from the doc_availability table and print in table format. and also add update & delete button. if delete button click then second.jsp page call.
  • In second .jsp page is call but selected row is not deleted.i m trying to fetch data from one.jsp but its not working.
  • I m trying get parameter,session attribute or location attribute but its all are not working.

Thank You For Help.

Anjuman
  • 3
  • 1

1 Answers1

0
session.setAttribute("Send", doc_id);

There are many ways you can pass a parameter from one jsp to another jsp. Setting the value in session and retrieving in another jsp is a valid method.

Reason for Why you are not getting the right value in the next jsp

You are iterating a result set and setting the session attribute "Send" inside the loop, which means the value of doc_id set in the attribute would be of the last record in the result set. To confirm this try deleting the last record displayed in your one.jsp

Solution:

Set the session attribute when the button is clicked. Write a onclick javascript function and set the appropriate doc_id

Pass it as a parameter <a href="update.jsp?doc_id="<%=doc_id%> />

Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34