Problem in Form (This is a small mini project)

Posted on 2006-04-20
Last Modified: 2010-04-01
package minopharm;
// This is an order form
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 {
      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+"'");
                  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("<table border=1>");
                  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>");*/
while(  //  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
out.println("<td width=150><input type=checkbox name=cb value='"+productName+"'>");
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>");
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) {
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
Question by:divvela2001

    Author Comment

    PLZ give reply to me
    LVL 16

    Accepted Solution


    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.

    LVL 11

    Assisted Solution

    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)
    LVL 8

    Assisted Solution

    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.

    LVL 19

    Assisted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    constructor overloading 2 70
    servlet cookie finding by name 1 53
    best way to search/remove a file from an EAR file 3 89
    maven set up 2 84
    After several days of searching and hunting for limited documentation, I wanted to share this guide to hopefully save someone the hassle of trying to figure this out on their own. I have tested this on Xendesktop 7.1 and PS 4.5 running simultaneous…
    The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now