moodyahmad
asked on
Get a row information of a table by click "BUY" button which exist at end of each row using servlet and javascript
hi experts
i have a Servlet , this servlet contain the table of books(isbn , title , userid , quantity) , and at the end of each row in this table i but a buton tell the user to BUY this book , the problem is i want when the user press the button "BUY" for specific book , i want to save the values of each filed in this row in ahidden html text boxs.(isbn , title , userid , quantity) , beacuse i want to save this row in the orders table . this my job >>> this is my incomplete servlet , i want when the user press "BUY" button for specific book , get the information of this book in a hidden html text boxs.
/*
* ShowBooks.java
*
* Created on October 30, 2006, 12:09 PM
*/
package p2;
import java.io.*;
import java.net.*;
import java.sql.ResultSetMetaData ;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement ;
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* @author amohammad
* @version
*/
public class ShowBooks extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServlet Request request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("t ext/html;c harset=UTF -8");
PrintWriter out = response.getWriter();
out.write("<script language=\"JavaScript\">\n ");
out.write(" function getIsbn() { \n" +
// " document.getElementById('i sbn').valu e = Pisbn \n" +
// " document.forms(0).submit() ;\n" +
" \n" +
" }");
out.write("</script>\n");
out.println("<html>");
out.println("<head>");
out.println("<title>Servle t ShowBooks</title>");
out.println("</head>");
out.println("<body>");
out.print(" <input type='hidden' name='isbn'/> ");
out.print(" <input type='hidden' name='title'/> ");
out.print(" <input type='hidden' name='userid'/> ");
out.print(" <input type='hidden' name='quantity'/> ");
out.println("<form name=\"fm\" action=\"ShowBooks\" method=\"Post\">\n" +
" <table cellspacing=\"3\" cellpadding=\"2\" border=\"1\" width=\"100%\">\n" +
" <tr>\n" );
out.println(" <input type=\"submit\" name=\"srch\" size=\"60\" value=\"Search Book\"/>");
out.println("<TABLE BORDER=1>");
try {
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
Connection connection = DriverManager.getConnectio n("jdbc:od bc:books", "","");
Statement statement = connection.createStatement ();
ResultSet results = statement.executeQuery("se lect * from books ;");
//out.println("<TABLE BORDER=1>");
ResultSetMetaData resultsMetaData =
results.getMetaData();
int columnCount = resultsMetaData.getColumnC ount();
out.println("<TR>");
for(int i=1; i<columnCount+1; i++) {
out.println("<TH>" +
resultsMetaData.getColumnN ame(i) ) ;
}
while( results.next() ) {
out.println("<TR>");
for(int i=1; i<columnCount+1; i++) {
out.print("<TD>" + results.getString(i));
}
int o = 3 ;
out.print("<TD> <input type=\"button\" name=\"buy \" onclick=\"getIsbn()\" size=\"60\" value=\" BUY\" /> </TD>");
}
statement.close();
results.close();
connection.close();
}
catch (Exception e) {
System.out.println(e);
}
out.println("</TABLE>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("t ext/html;c harset=UTF -8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servle t ShowBooks</title>");
out.println("</head>");
out.println("<body>");
out.println("<form name=\"fm\" action=\"ShowBooks\" method=\"Post\">\n" +
" <table cellspacing=\"3\" cellpadding=\"2\" border=\"1\" width=\"100%\">\n" +
" <tr>\n" );
out.println(" <input type=\"submit\" name=\"srch\" size=\"60\" value=\"Search Book\"/>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
//out.println("</table");
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
this servlet contain a button when you press this button a list of the books appear with "BUY" button next each row
please please help me
thank you
i have a Servlet , this servlet contain the table of books(isbn , title , userid , quantity) , and at the end of each row in this table i but a buton tell the user to BUY this book , the problem is i want when the user press the button "BUY" for specific book , i want to save the values of each filed in this row in ahidden html text boxs.(isbn , title , userid , quantity) , beacuse i want to save this row in the orders table . this my job >>> this is my incomplete servlet , i want when the user press "BUY" button for specific book , get the information of this book in a hidden html text boxs.
/*
* ShowBooks.java
*
* Created on October 30, 2006, 12:09 PM
*/
package p2;
import java.io.*;
import java.net.*;
import java.sql.ResultSetMetaData
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* @author amohammad
* @version
*/
public class ShowBooks extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServlet
throws ServletException, IOException {
response.setContentType("t
PrintWriter out = response.getWriter();
out.write("<script language=\"JavaScript\">\n
out.write(" function getIsbn() { \n" +
// " document.getElementById('i
// " document.forms(0).submit()
" \n" +
" }");
out.write("</script>\n");
out.println("<html>");
out.println("<head>");
out.println("<title>Servle
out.println("</head>");
out.println("<body>");
out.print(" <input type='hidden' name='isbn'/> ");
out.print(" <input type='hidden' name='title'/> ");
out.print(" <input type='hidden' name='userid'/> ");
out.print(" <input type='hidden' name='quantity'/> ");
out.println("<form name=\"fm\" action=\"ShowBooks\" method=\"Post\">\n" +
" <table cellspacing=\"3\" cellpadding=\"2\" border=\"1\" width=\"100%\">\n" +
" <tr>\n" );
out.println(" <input type=\"submit\" name=\"srch\" size=\"60\" value=\"Search Book\"/>");
out.println("<TABLE BORDER=1>");
try {
Class.forName("sun.jdbc.od
Connection connection = DriverManager.getConnectio
Statement statement = connection.createStatement
ResultSet results = statement.executeQuery("se
//out.println("<TABLE BORDER=1>");
ResultSetMetaData resultsMetaData =
results.getMetaData();
int columnCount = resultsMetaData.getColumnC
out.println("<TR>");
for(int i=1; i<columnCount+1; i++) {
out.println("<TH>" +
resultsMetaData.getColumnN
}
while( results.next() ) {
out.println("<TR>");
for(int i=1; i<columnCount+1; i++) {
out.print("<TD>" + results.getString(i));
}
int o = 3 ;
out.print("<TD> <input type=\"button\" name=\"buy \" onclick=\"getIsbn()\" size=\"60\" value=\" BUY\" /> </TD>");
}
statement.close();
results.close();
connection.close();
}
catch (Exception e) {
System.out.println(e);
}
out.println("</TABLE>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("t
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servle
out.println("</head>");
out.println("<body>");
out.println("<form name=\"fm\" action=\"ShowBooks\" method=\"Post\">\n" +
" <table cellspacing=\"3\" cellpadding=\"2\" border=\"1\" width=\"100%\">\n" +
" <tr>\n" );
out.println(" <input type=\"submit\" name=\"srch\" size=\"60\" value=\"Search Book\"/>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
//out.println("</table");
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
this servlet contain a button when you press this button a list of the books appear with "BUY" button next each row
please please help me
thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.