File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes strange checkbox problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "strange checkbox problem" Watch "strange checkbox problem" New topic
Author

strange checkbox problem

Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
i have a jsp page which shows dynamic rows generated according to the query made by the user.At the end of each row is a checkbox which user can check to select that record.
The problem is- the last row checkbox in the page always passes value no matter it is checked or not and the value passed is always blank (not NULL).It is always the last row checkbox that is giving problem.
I have checked my code and yet not able to understand the problem.

Please help!!!

[Bear edit: modified title]
[ September 23, 2004: Message edited by: Bear Bibeault ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
need to see your code.

how you are creating table in your jsp and how and where you are getting the value from the check boxes.
Balan Raj
Ranch Hand

Joined: Aug 26, 2003
Posts: 74
do u have any hidden fields running along ??
the code needs to be there to help..
Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
this forum is not allowing me to put my code with < > tags.I tried to replace all but even whencode does not have any such tags still it is giving such error.

I do not know whar to do now
Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
//this is the jsp code

String query1="select * from advice_tbl where party_name='"+p_name+"' and status='PENDING'";
rs1=st1.executeQuery(query1);
while(rs1.next())
{
count=count+1;
String p_id=rs1.getString("prod_id");
String query2= "select prod_type,prod_micron,prod_width from product_tbl where prod_id='"+p_id+"'";
Statement st2 = dbcon.connect();
ResultSet rs2=st2.executeQuery(query2);
if(rs2.next())
{
String adv_no=rs1.getString("adv_no");

/* %>
<td width="66"><%=adv_no%> <div align="center"></div></td>
<td width="52"><%=p_id%> <div align="center"></div></td>
<td width="62"><%=rs2.getString("prod_type")%> <div align="center"></div></td>
<td width="80"><%=rs2.getString("prod_micron")%> <div align="center"></div></td>
<td width="77"><%=rs2.getString("prod_width")%> <div align="center"></div></td>
<td width="49"><%=rs1.getString("qty")%> <div align="center"></div></td>
<td width="88"><%=rs1.getString("adv_date")%> <div align="center"></div></td>
<td width="60"> <div align="center">
<input type="checkbox" checked value="<%=adv_no+p_id%>" name="<%=count%>" >

}*/
Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
//THIS IS THE SERVLET WHICH ACCEPTS THE VALUES

String count=request.getQueryString();
int count1=Integer.parseInt(count);
int flag=0;

for(int i=1;i(count1;i++)
{
String s=request.getParameter(""+i);


if(s!=null)
{
flag=0;
System.out.println("SSSS"+s);
String adv_no=s.substring(0,s.indexOf("P"));
String po_num=s.substring(s.indexOf("P"));

String query1="select qty,party_name from advice_tbl where adv_no='"+adv_no+"' and prod_id='"+po_num+"'";
ResultSet rs1=st1.executeQuery(query1);
while(rs1.next())
Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
I have added /* */ tags just to let the editor take it as comments.

Please just check the code i have just copy and pasted it so do not go for syntax errors
thanks in advance
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
hey buddy you messed up all. jsp with jdbc, jdbc with servlet. it is not at all a good practice.

first try to seperate it all.

use jsp just for presentation
use servlet just for request/response
use some regular java class for your jdbc stuff.

and i saw there you are getting connection inside your while loop and then you are not closing it even. try to make it clear. your real problem is, your code is messed up too much.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
give a static name to your checkbox field. if it would be more than 1 it will return you a String[].
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You can not name form elements with a variable.

should be


Eric
Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
thanks aadeel and eric!!

I will make it very clear next time.
U saw my code. My connection class returns a statement so everytime i need a statement i have to make a connection.i know this is not right but could you tell me that how to use one statement to execute nested(not exactly nested) queries for example like for each resultset of query1 i have to execute query2.

query1 executed
while(rs of query1)
{
query2 excuted..

}
Could you tell me how to do it correctly???
Balan Raj
Ranch Hand

Joined: Aug 26, 2003
Posts: 74
Eric,


Eric said :
You can not name form elements with a variable.


I dont think there are any limmitations as such from naming form fields with variables ..Is it from a practice stand point ??
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
you can use the same connection for your nested query. and if you get a new connection in your while loop make sure to return/close it inside the loop as well.

again jdbc in servlet/jsp is a very bad practice.
Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
hi adeel
i need ur help.
I have made an application which u can say is a small erp package for a client.
I make connection using a class which also has a close function for closing the connection.
Now the problem is whenever i try to close connection after the transction it always gives this error

Error in Closingjava.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]
Invalid transaction state

which is not the case as the jsp just displays the result of a query and does nothing else.

this is my connecting and close function

public Connection con = null;
public Statement connect() {
Statement stmt = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dataSourceName = "al_db";
String dbURL = "jdbc dbc:" + dataSourceName;
con = DriverManager.getConnection(dbURL, "","");
con.setAutoCommit(false);//To ser Transaction mannually...
stmt = con.createStatement();
}
catch (Exception e)
{
System.out.println("Error Occured While Connecting to database..." + e);
}
return (stmt);
}

//close function
public void close(){
try
{
con.close();
}
catch(SQLException e)
{
System.out.println("Error in Closing"+e);
}
}
}

I know this code cud have errors but i am still new to all jdbc,jsp and servlet fundamentals but trying to catch fast
Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
Hey adeel and eric
Know what I just solved my checkbox problem.
As eric told i named my checkbox with a 'P' before count and it worked!!!

U guys are just gr888888888888888t.

Thanks again.
Adeel pls give some suggestions on the problem i just posted.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
it is not threadsafe. imagine if two threads invoking two different methods and both methods are doing this in your case,

Statement stmt= connect();

as inside your connect you are gettin connection from DriverManager and setting it to an instance variable "con". so here both the threads are going to access same con. after second thread execute the Driver.getConnection(), con for the first thread would get lost and second thread sets "con" with a new value. now consider that first one has finished transaction and closed the con object by invoking your close(). then when second thread comes to close the con, you will get the exception. because that con has already been closed.

just get the connection when you need it and close it in the same block. like,



it is the simplest, not the best, way we do that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: strange checkbox problem