Question

i have a 2 JSP pages i.adminhome.jsp ii.deletecontroller.jsp

adminhome.jsp shows the client table

i want the admin to delete a specific row from the client table if he so desires. so i need to use a radiobutton to select the clientid from the client table, and press the "Delete" button to delete that selected row

But i am not getting the desired output as i am failing to pass the selected clientid value to the deletecontroller page as a session variable. Thus i am getting null value.

These are the codes of the 2 pages

1.adminhome.jsp

<%
try
{
Connection conn=null;
Statement stmt=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:xyz","sa","pass");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select UserID,Username,Location,Gender,EmailID from Client");

while(rs.next()){%>


<table border="1" align="center" >
    <tr>
        <td><input name="district" type="radio"  value="<%=rs.getString("UserID") %>" required="true"/></td>
        <td><%=rs.getString("Username") %></td>
        <td><%=rs.getString("Location") %></td>
        <td><%=rs.getString("Gender") %></td>
        <td><%=rs.getString("EmailID") %></td>


        <%  
            String x = request.getParameter("district");
            session.setAttribute("userid", x);
        %>




        <td><input type="submit" name="btn1" value="Delete"></td>

    </tr>
</table>

        </div>

        <br/><br/>



<%}
}
catch(Exception e){e.printStackTrace();}

%></form>

2.deletecontroller.jsp

<%@ page import="java.util.*" %>
<%@ page language="java" import="java.sql.*" errorPage="" %>
<%
    String x=session.getAttribute( "userid" ).toString();
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:xyz;","sa","pass");

    Statement stmt=null;
    stmt = con.createStatement();

    String del="Delete from Client where UserID='"+x+"' ";
    stmt.executeUpdate(del);

    response.sendRedirect("index.jsp");



%>

No correct solution

OTHER TIPS

Let's just go through what your code does

 <td><input name="district" type="radio"  value="<%=rs.getString("UserID") %>" required="true"/></td>
...
<%  
 String x = request.getParameter("district");
 session.setAttribute("userid", x);
%>

You are outputting an HTML form with a field called district. And at the same time you are reading district from the request and saving it to the session. Since district isn't in the request, you are reading a NULL, and saving NULL in the session. How would it be in the request when you are just now outputting the form? The user can't submit the form before you've output the form to the user, can they?

Then when you submit the form, in your servlet you are reading from the session:

String x=session.getAttribute( "userid" ).toString();

You are reading the NULL you saved into the session when you were building the form. You are not reading the form that the user actually submitted.

What should you be doing? Reading from the request in the servlet:

String x = request.getParameter("district");

The request with a parameter district is generated when the user submits the form, so the request is being sent to the servlet and you should read from the request there.

Or, another way would be to save to the session from the rs.getString("UserID"):

<% 
  session.setAttribute("userid", rs.getString("UserID")); 
%>

That might be what you want, since UserID probably is something that should be session scope rather than request, and since you don't want the user changing their userid in the form and manipulating things. Then you can still read from the session in your servlet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top