Problem in Form (This is a small mini project)

package minopharm;
// This is an order form
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.PreparedStatement;

 public class CreateOrder1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   
      private static final long serialVersionUID = 1L;
      String partyName;
      String productName;
      ResultSet resultset,resultset1;
      PreparedStatement pstmt;
      int j;
      
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
      }        
            
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            PrintWriter out=response.getWriter();
            partyName=request.getParameter("partyName");// here we are getting partyname from partyinformation page
            Date d =new Date();
            Connection connection=DBAccess.createODBCConnection();
            out.println("<head><title> Order Form </title>");
            out.println("<font face=arial >");
            out.println("<center><h1> ORDER FORM</h1></center>");
            try {
                  Statement statement=connection.createStatement();
                  resultset=statement.executeQuery("select partyCodeNo,drugLicenseNo20B,drugLicenseNo21B,vatNo,cstNo from party_Information where partyName='"+partyName+"'");
                  resultset.next();
                  out.println("<form name=order method=post action=PartyOrder>");
                  out.println("<b>Customer Code : <input type=text name=customerCode value="+resultset.getString(1)+">");
                  out.println("Order Number : <input type=text name=orderNo value=ord"+j+">");
                  out.println("Date : <input type=text name=date  size=20 value="+d.getDate()+"/"+(d.getMonth()+1)+"/"+(d.getYear()+1900)+"><br>");
                  out.println("DrugLicence No 20B <input type=text name=drugLicenseNo20B value="+resultset.getString(2)+">");
out.println("21B<input type=text name=drugLicenseNo21B value="+resultset.getString(3)+">");
out.println("VAT/CST No : <input name=orderNo type=text  value="+resultset.getString(4)+"><br><br><br>");
                  resultset=statement.executeQuery("select ProductName,UnitOfPacking from product_Add order by productname");
                  ResultSetMetaData metadata=resultset.getMetaData();
                  out.println("<center>");
                  out.println("<table border=1>");
                  out.println("<tr>");
                  out.println("<td width=50><center><b> S.NO </b></td>");
                  for(int i=1;i<=metadata.getColumnCount();i++)
out.println("<td width=130><center><b>"+metadata.getColumnName(i)+"</b></td>");
out.println("<td width=80><center><b> Q.ty </b></center></td>");
                  for(int i=1;i<=metadata.getColumnCount();i++)
out.println("<td width=130><center><b>"+metadata.getColumnName(i)+"</b></td>");
out.println("<td width=50><center><b> Q.ty </b></center></td>");*/
out.println("</tr>");
                  j=1;
while(resultset.next())  //  We are getting all the product names from the product table and we //are giving checkbox option to the customer to select the product
// WE are displaying all the products in table format.Customer will select the products
{
      productName=resultset.getString(1);
      out.println("<tr>");
      out.println("<td>"+j+"</td>");
out.println("<td width=150><input type=checkbox name=cb value='"+productName+"'>");
out.println(productName+"</td>");
out.println("<td width=100><center>"+resultset.getString(2)+"</center></td>");
out.println("<td width=80><center><input type=text name='"+productName+"' size=3></center>");
      j++;
                  }
                  out.println("</table><br>");
out.println("<center><input type=button value=Generate >");// Generate button
out.println("<input type=text name=totalcost><br><br>");// Total Cost will be displayed in this text box upon clicking Generate button
out.println("<input type=submit value=CreateOrder>&nbsp;&nbsp;&nbsp;");
out.println("<input type=reset value=Cancel>");
            } catch (SQLException e) {
                  e.printStackTrace();
            }
            
      }                    
}
Upon clicking GENERATE Button(which is a Button ,not a SUBMIT Button), we need to get the products and Quantity Ordered by the Customer (which is in that Page only) and NetPrice of selected products  that has to be Obtained from PriceList table(database) and we have to display the total amount in this page only .
  How to solve this problem
divvela2001Asked:
Who is Participating?
 
suprapto45Commented:
Hi,

Why can't you make it a submit button then? If you intend to still use the normal button, you may need to either pass it from Session or as GET variables.

David
0
 
divvela2001Author Commented:
PLZ give reply to me
0
 
fargoCommented:
it's not a good idea to use servlet for such handlings.
Instead use jsp and make a server round trip on clicking the generate button to get the net price. (u need to avoid the submission or insert/update on db with some parameter handling)
0
 
colr__Commented:
This can be done with Javascipt. On loading the page, youll need to make sure that you have 'Obtained from PriceList table(database) ' and make this avaiable to the javascipt code (use the JSP to dynamically write some javascript with the values you need in it).

When the user clicks the button, this will fire a javascript function that will collect the values entered by the user, perfrom the calculation and update the page with the result. This can all be done without leaving the page.

colr__
0
 
actonwangCommented:
whichever you want :servlet or jsp, in your code, here:

out.println("<center><input type=button value=Generate >");// Generate button

you'd have to add a "onclick" event handler which will do some thing on this page as:

out.println("<center><input type=button value=Generate onclick='<your js function>'>");// Generate button

then you'd put the js function there.

Yup, I'd suggest you switch to jsp instead of servlet ESPECIALLY in your case.
depending on your case, your js function either:
1. submit the form and return back another page
2. use AJAX (if extra information needed) or javascript function to generate result

Acton
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.