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]

[16533 byte] By [Kranthi.Remalaa] at [2007-11-26 19:19:27]
# 1
Is your back button making a new request or simulating the pressing of the browser back button?
ChristopherAngela at 2007-7-9 21:35:48 > top of Java-index,Java Essentials,Java Programming...
# 2
You should really consider using the tomcat datasource for you db connections. it's very easy to configure and use.You might also want to think about creating a jsp page as code within html is easier on the brain than html within code.
ChristopherAngela at 2007-7-9 21:35:48 > top of Java-index,Java Essentials,Java Programming...
# 3

[url=http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html]Understanding JavaServer Pages Model 2 architecture[/url]

[url=http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html]Web-Tier Application Framework Design[/url]

[url=http://www.fawcette.com/javapro/2002_06/online/servlets_06_11_02/]Almost All Java Web Apps Need Model 2[/url]

Then...

[url=http://www.theserverside.com/articles/article.tss?l=RedirectAfterPost]Redirect After Post[/url]

~

yawmarka at 2007-7-9 21:35:48 > top of Java-index,Java Essentials,Java Programming...
# 4

[nobr]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>");

That implies that i'm making a new request to the older page.[/nobr]

Kranthi.Remalaa at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 5

Sir, i thank u for guiding towards JSP but we are almost finished 70% pf our application..So i would like to know how can i achieve the thing i wanted.. Anyways, in the mean time i shal look into jsp..

Yeah as you said.........when we are stoping the tomcat and restarting it.it is showing the new results..

can you help me with configuring the tomcat better.

Infact, I've started learning adv java just few months back and i'm learning it myself . So in the middle we have started creating this new application as a Mini project for a social organisation.

So what do yo thing is the problem there.?

Kranthi.Remalaa at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 6
At a glance you appear to be storing all books in the str variable.This variable is only populated once at startup. It doesn't matter how many records you delete from the DB they will still remain in the str variable.
ChristopherAngela at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks for the link..I had overlooked it..I shall respond to you with my queries after going thru the article
Kranthi.Remalaa at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 8
You could add the meta tag<META HTTP-EQUIV="EXPIRES" CONTENT="May 1, 2002 00:00:00 EST">This stops your browser from caching the page.Ted.
ted_trippina at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 9

Well, I'm storing the resultset into Str[][]....

But, when i say "select * from books" after going back to the previous page. which means that i'm executing the servlet once again. It should get the data that is existing in the books database, which is now obviously containing the list of books after getting deleted.

Kranthi.Remalaa at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 10
I'm a little confused about your mapping from one servlet to the other so forgive me if I'm wrong but you change str[][] in book delete but ebooks is still using the str which was populated at start up in the init method.
ChristopherAngela at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 11
Yeahhhh Right, I've just retested my program and what you have told is right. Its justtaking data at startup..Can you suggest a probable solution for this problem?
Kranthi.Remalaa at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 12
Move the population of str from init to the get method?
ChristopherAngela at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 13

But, It would get the same resultset back right..........

well since i'm new to this part i would like t

o to explain me wat would be the consequences of retrieving

the data from init and get method?

I must say here that i apologise my naiveity

inspite of implementing an app,

but i felt this is the best way to learn

Kranthi.Remalaa at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 14
The init method gets called once in the lifetime of the servlet.The get method will be called each time a request is made.
ChristopherAngela at 2007-7-9 21:35:49 > top of Java-index,Java Essentials,Java Programming...
# 15
well thnks i got wat u meant.............i was quite confused about wat ur weer telling i got watu meant.. init() is executed only once rght
Kranthi.Remalaa at 2007-7-9 21:35:51 > top of Java-index,Java Essentials,Java Programming...
# 16
yes that is correct.init is called once when the servlet is first createdget is called each time a request is made destroy is called once when the servlet is destroyed
ChristopherAngela at 2007-7-9 21:35:51 > top of Java-index,Java Essentials,Java Programming...
# 17

> i was quite confused about wat ur weer telling i got watu meant..

Please make the extra effort to properly write out words such as "what", "you", and "were". The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership. Also, it will give the appearance that you take your question seriously, which will in turn make your question look more interesting to answer.

~

yawmarka at 2007-7-9 21:35:51 > top of Java-index,Java Essentials,Java Programming...
# 18
Yeah, I apologize over that mistake as I was eager to reply. So, in that pace they had turned out like that.I shall earnestly write proper words.Thanks for reciprocating me.
Kranthi.Remalaa at 2007-7-9 21:35:51 > top of Java-index,Java Essentials,Java Programming...
# 19
Yawmark, don't reciprocate him, he might get up the duff!!!!!Ted.
ted_trippina at 2007-7-9 21:35:51 > top of Java-index,Java Essentials,Java Programming...
# 20
Ted,Can you please e-mail me at TravisTierno@hotmail.com
TTierno2a at 2007-7-9 21:35:51 > top of Java-index,Java Essentials,Java Programming...
# 21
> Ted,> > Can you please e-mail me at TravisTierno@hotmail.comI could....Any particular reason why?If you have a java question then post it. I don't check the forums regularly but always happy to help.Ted.
ted_trippina at 2007-7-9 21:35:51 > top of Java-index,Java Essentials,Java Programming...