title: retrieve the data to the same html page i am using
body: <p>using servlet code i inserted the data to database.
      and i want to retrieve that data to the same html page.....i want the servlet code 
      in the same class for inserting and editing. i am using eclipse.....so plz help me     frnds.....i am new to java and 
      my knowledge is close to nothing </p>
  
  <p><strong>html page</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  
  &lt;title&gt;PATIENT&lt;/title&gt;
  &lt;style type="text/css"&gt;
  input, textarea,select {
    background-color : lightgray;
  }
  table
  {
  border: 2px solid gray;
  }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body bgcolor="#DDDDDD"&gt;
  
  &lt;script&gt;
  function myFunction()
  {
  
      var reg_no=prompt("Please enter your Register No", "")
      }
  &lt;/script&gt;
  
  &lt;p align ="center"&gt;&lt;b&gt;PATIENT&lt;/b&gt;&lt;/p&gt;&lt;br&gt;
  &lt;form method="post" name ="patientForm" action="patientDemo"&gt; 
  &lt;table align="center" cellpadding="2" cellspacing="2"&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;font color="maroon"&gt;Register Number&lt;/font&gt;
  &lt;/td&gt;
  &lt;td colspan="1"&gt;
  &lt;input type="text" maxlength="15" name="regs_numb" required&gt;
  &lt;/td&gt;
  &lt;td&gt;
  
     &lt;font color="maroon"&gt;PatientId&lt;/font&gt;
  &lt;/td&gt;
  &lt;td colspan="2"&gt;
      &lt;input type="text" maxlength="10" size="18" name="pat_Id" required&gt;
  &lt;/td&gt;
  &lt;td&gt;
     &lt;font color="maroon"&gt;Patient Type&lt;/font&gt;
  &lt;/td&gt;
  &lt;td colspan="2"&gt;
     &lt;input type="text" size="3" maxlength="3" name="pat_Type"&gt;
  &lt;/td&gt;
  &lt;tr&gt;
  &lt;td&gt;
     &lt;font color="maroon"&gt;Patient Name&lt;/font&gt;
  &lt;/td&gt;
  &lt;td colspan="4"&gt;
      &lt;input type="text" maxlength="50" size="53" name="pat_Name"&gt;
  &lt;/td&gt;
  
  &lt;td&gt;
  &lt;font color="maroon"&gt;Age&lt;/font&gt;
  &lt;/td&gt;
  
  &lt;td&gt;
  &lt;input type="text" maxlength="3" size="3" name="age"&gt;
  &lt;select name="age_units"&gt;
  &lt;option value="years"&gt;Years&lt;/option&gt;
  &lt;option value="months"&gt;Months&lt;/option&gt;
  &lt;option value="days"&gt;Days&lt;/option&gt;
  &lt;/select&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;
    &lt;font color="maroon"&gt;Sex&lt;/font&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;select name="sex"&gt;
  &lt;option value="male"&gt;Male&lt;/option&gt;
  &lt;option value="female"&gt;Female&lt;/option&gt;
  &lt;/select&gt;
  &lt;/td&gt;   
  
  
  
  &lt;td&gt;
    &lt;font color="maroon"&gt;Package&lt;/font&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;input type="text" maxlength="10" SIZE="18" name="package"&gt;
  &lt;/td&gt;
  &lt;/table&gt;
  
  
  &lt;br&gt;
  &lt;table align="center" cellpadding="2"&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;input type="submit" value="save" id="save"/&gt;
  &lt;td&gt;
  &lt;input type="button" onclick="myFunction()" value="edit" id="edit"/&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;input type="button" value="delete" id="delete"&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;input type="reset" value="cancel" id="cancel"/&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;input type="button" value="exit" id="exit"/&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>PatientDemo.java</strong></p>
  
  <pre><code>package patientDemo;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  
  
  
  public class PatientDemo extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  
  {
  
  response.setContentType("text/html");
  
  PrintWriter out= response.getWriter();
  
  
      String a=request.getParameter("regs_numb");
      String b=request.getParameter("pat_Id");
      String c=request.getParameter("pat_Type");
      String d=request.getParameter("pat_Name");
      String e=request.getParameter("age");
      String f=request.getParameter("age_units");
      String g=request.getParameter("sex");
      String h=request.getParameter("package");
  
     try
  
  {
  Class.forName("com.mysql.jdbc.Driver");
  
  System.out.println("driver loaded");
  System.out.println("Driver is loaded");
  Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/charms","root","root");
  System.out.println("Connection created");
  PreparedStatement ps= ((java.sql.Connection) con).prepareStatement("insert into patient(p_reg_no_v,p_patient_id_v,p_patient_type_v,p_patientname_v,p_age_s,p_ageunit_v,p_sex_v,p_package_v) values (?,?,?,?,?,?,?,?)");
  
  
          ps.setString(1,a);
          ps.setString(2,b);
          ps.setString(3,c);
          ps.setString(4,d);
          ps.setString(5,e);
          ps.setString(6,f);
          ps.setString(7,g);
          ps.setString(8,h);
  ps.execute();
  
  out.close();
  System.out.println("Inserted");
  
  }
  catch(Exception e1)
  
  {
  
  System.out.println(e1);
  
  }
  
  }
  }
  </code></pre>