request.getParameter() returns NULL in java servle

2019-09-05 14:24发布

问题:

I have made two servlets Authenticate.java and Accept.java

Here's the doGet function of Authenticate.java

      PreparedStatement stmt = null;
      PrintWriter out=response.getWriter();

      response.setContentType("text/html");  
      response.setCharacterEncoding("UTF-8");  
      try{
          int cnt=0;
          Class.forName("com.ibm.db2.jcc.DB2Driver");
          Connection con= DriverManager.getConnection("jdbc:db2://localhost:50000/SSMS", "kiit", "kiit");
          con.setAutoCommit(false);
          String s=request.getParameter("userName");
          if(s==null){
              out.println("You Are Not Authorized");
              return;
          }
          stmt=con.prepareStatement("SELECT COMP_ID,NAME,CONTACT FROM COMPANYREG");

          ResultSet rs=stmt.executeQuery();
          while(rs.next()){
              cnt++;
              out.println("<form action='Accept' method='post'> <input type='text'  size='20' id='cmpid' value='"+rs.getString(1)+"'><br> Name :"+rs.getString(2)+"<br>Contact: "+rs.getString(3)+"<br><input type='submit' class='confirm' id='"+cnt+"' value='Approve'><br></form>");
          }

And Heres Accept.java

               String user=request.getParameter("cmpid");

      PrintWriter out=response.getWriter();
      response.setContentType("text/html");  
      response.setCharacterEncoding("UTF-8"); 
      out.println("user = "+user);
      try{
          String cid = null,pss = null,nam = null,add = null,sec = null,cnt = null,ema = null;
          Class.forName("com.ibm.db2.jcc.DB2Driver");
          Connection con= DriverManager.getConnection("jdbc:db2://localhost:50000/SSMS", "kiit", "kiit");

          stmt=con.prepareStatement("SELECT * FROM COMPANYREG WHERE COMP_ID = '"+user+"'");
          ResultSet rs=stmt.executeQuery();
          out.println("query1");
          while(rs.next()){
              out.println("query2");
              cid = rs.getString(1);
              pss=rs.getString(2);
              nam=rs.getString(3);
              add=rs.getString(4);
              sec=rs.getString(5);
              cnt=rs.getString(6);
              ema=rs.getString(7);

          }
          out.println("query3");
          stmt=con.prepareStatement("INSERT INTO COMPANY VALUES ('"+cid+"','"+pss+"','"+nam+"','"+add+"','"+sec+"',"+cnt+",'"+ema+"')");

          int numRowsChanged= stmt.executeUpdate();
          if(numRowsChanged==0) out.println("Some Error Encountered Please Re-enter Information values Correctly");
          else out.println("You Were Registered Successfully.You will be allowed to login once your details are verified. Thank You");

In the Accept.java class when i refer to variable "cmpid" using request.getParameter("cmpid");

it is returning null ...... Can anybody tell me what actually is the problem here??

回答1:

out.println("<form action='Accept' method='post'> <input type='text'  size='20' id='cmpid' value='"+rs.getString(1)+"'><br> Name :"+rs.getString(2)+"<br>Contact: "+rs.getString(3)+"<br><input type='submit' class='confirm' id='"+cnt+"' value='Approve'><br></form>");

add name attribute in form as "name=cmpid"