Loopback in a servlet
[nobr]I have been facing this problem in my servlet application. I'm using Apache Tomcat as the webservice container and mysql as the backend database and servlets for the frontend. So the problem is:
I have page where i am displaying the list of books available and providing an option of DELETE and INSERT . So ther is a checkbox provided for each item so that whatever is checked will be DELETED. The deletion is being commited at the database end as we are showing the results ikn the next page and it is being properly reflected. Now in this servlet, we are providing a button to go back to the previous page. So now, it should display the data with the books that are existing after the deletion. So, THE THING arises here.........IT IS SHOWING THE OLDER RESULTS. How should i track this problem....
I'm attaching the underlying servlet code. We are not finding any problem with the database. It is accepting the query properly n giving the results accordingly.
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.text.*;
publicclass ebooksextends HttpServlet
{
Connection c;
Statement s;
PreparedStatement p;
ResultSet r;
String str[][];
ResultSetMetaData m;
int n,j=1,i=1,l=0;
publicvoid init()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("jdbc:odbc:csit","root","project");
System.out.println("connected");
s=c.createStatement();
r=s.executeQuery("select * from books");
m=r.getMetaData();
n=m.getColumnCount();
str=new String[100][5];
while(i<=n)
{
str[0][i-1]=m.getColumnName(i);
i++;
}
while(r.next())
{
for( i=1;i<=n;i++)
{
str[j][i-1]= r.getString(i);
}
j++;
}
c.commit();
c.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
publicvoid doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html;image/jpeg;");
PrintWriter pw=res.getWriter();
pw.println("<html>\n<head>\n<title>Samskrita Bharati</title></head>");
pw.println("<body background=\"C:\\Program Files\\Apache Group\\Tomcat 4.1\\common\\classes\\bg.jpg\">");
pw.println("<form name=f1 action=http://localhost:8080/examples/servlet/bookdelete >");
pw.println("<h2> Available Books</h2><br>");
pw.println("<table border=2 bordercolor=black>");
for(int k=0;k<j;k++)
{
pw.println("><tr>");
if(k>0)
{
pw.println("<td><input type=checkbox name=cb"+k+"></td>");
}
else
pw.println("<td><h3>Check</h3></td>");
for(l=0;l<(i-1);l++)
{
pw.println("<td>");
if(k==0)
pw.println("<h3>"+str[k][l]+"</h3>");
else
pw.println("<i><b>"+str[k][l]+"</i></b>");
pw.println("</td>");
}
pw.println("</tr>");
}
pw.println("</table>");
pw.println("<br><h3>Select the rows you want to delete and press </h3> <input type=submit name=bt1 value=Delete ></form>");
pw.println("<form name=f2 action=http://localhost:8080/examples/servlet/bookinsert><br><br><h3>Fill the details in the fields to insert</h3><br>");
pw.println("<table>");
pw.println("<tr><td><b>Book Id</b> </td> <td><input type=textbox name=b1 maxlength=5 ></td></tr>");
pw.println("<tr><td><b>Book Name</b></td> <td> <input type=textbox name=b2 maxlength=50 ></td></tr>");
pw.println("<tr><td><b>Author</b> </td> <td><input type=textbox name=b3 maxlength=25 ></td></tr>");
pw.println("<tr><td><b>Publisher</b></td> <td> <input type=textbox name=b4 maxlength=50 ></td></tr>");
pw.println("<tr><td><b>Price</b></td> <td> <input type=textbox name=b5 maxlength=10></td></tr></table>");
pw.println("<input type=submit name=bt1 value=Insert ></form>");
pw.println("<form name=f3 method=\"get\" action=http://localhost:8080/examples/servlet/empoptions>");
pw.println("<br><center><input type=submit value=\"Go back to the options\"></center></br></form>");
pw.println("</body>");
pw.println("</html>");
pw.close();
}
}
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.text.*;
publicclass bookdeleteextends HttpServlet
{
Connection c;
Statement s;
PreparedStatement p;
ResultSet r;
String str[][];
ResultSetMetaData m;
int n,j=1,i=1,l=0;
publicvoid init()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("jdbc:odbc:csit","root","project");
System.out.println("connected");
s=c.createStatement();
r=s.executeQuery("select * from books");
m=r.getMetaData();
n=m.getColumnCount();
str=new String[100][5];
while(i<=n)
{
str[0][i-1]=m.getColumnName(i);
i++;
}
while(r.next())
{
for( i=1;i<=n;i++)
{
str[j][i-1]= r.getString(i);
}
j++;
}
c.commit();
c.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
publicvoid doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("jdbc:odbc:csit","root","project");
System.out.println("connected");
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("doget method");
int u=1;
try{
while(u<j)
{
try{
String re=req.getParameter("cb"+u);
if(re.compareTo("on")==0)
{p=c.prepareStatement("DELETE from books where BookId=?");
p.setString(1,str[u][0]);
p.execute();
c.commit();
u++;
}
}
catch(Exception e)
{u++;
}
}
c.close();
}
catch(Exception e)
{
e.printStackTrace();
}
i=1;j=1;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("jdbc:odbc:csit","root","project");
System.out.println("connected");
s=c.createStatement();
r=s.executeQuery("select * from books");
m=r.getMetaData();
n=m.getColumnCount();
str=new String[100][5];
while(i><=n)
{
str[0][i-1]=m.getColumnName(i);
i++;
}
while(r.next())
{
for( i=1;i<=n;i++)
{
str[j][i-1]= r.getString(i);
}
j++;
}
c.commit();
c.close();
}
catch(Exception e)
{
e.printStackTrace();
}
res.setContentType("text/html;image/jpeg;");
PrintWriter pw=res.getWriter();
pw.println("<html>\n<head>\n<title>Welcome to Samskrita Bharati</title></head>");
pw.println("<body background=\"C:\\Program Files\\Apache Group\\Tomcat 4.1\\common\\classes\\bg.jpg\">");
if(j==1)
{
pw.println("<form name=f3 method=\"get\" action=http://localhost:8080/examples/servlet/empoptions>");
pw.println("<br><br><center><table border=2 bordercolor=black height=50% width=50%>");
pw.println("<tr><td><h2><center><i>The Books Database does not have any records </i></center></h2></td></tr></table></center>");
pw.println("<br><center><input type=submit value=\"Go back to the options page\"></center></br></form>");
}
else
{
pw.println("<form name=f1>");
pw.println("<h2><center> Available Books</center></h2><br>");
pw.println("<center><table border=2 bordercolor=black>");
for(int k=0;k<j;k++)
{
pw.println("><tr>");
for(l=0;l<(i-1);l++)
{
pw.println("<td>");
if(k==0)
pw.println("<h3>"+str[k][l]+"</h3>");
else
pw.println("<i><b>"+str[k][l]+"</i></b>");
pw.println("</td>");
}
pw.println("</tr>");
}
pw.println("</table></center></form>");
pw.println("<form name=f2 method=\"get\" action=http://localhost:8080/examples/servlet/ebooks>");
pw.println("<br><center><input type=submit value=\"Go back to books database\"></center></br></form>");
}//End of else
pw.println("<form name=f3 method=\"get\" action=http://localhost:8080/examples/servlet/empoptions>");
pw.println("<br><center><input type=submit value=\"Go back to the options\"></center></br></form>");
pw.println("<br><br><center><a href=http://localhost:8080/examples/servlet/homepage>Back To Home</a>");
pw.println("</body>");
pw.println("</html>");
pw.close();
}
}
[/nobr]

