用request.getParameter()从另一个servlet调用它返回的Java Servl

2019-10-31 00:44发布

我做了两个servlet Authenticate.java和Accept.java

下面是Authenticate.java的功能的doGet

      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>");
          }

和继承人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");

在Accept.java类时我指的是变量“cmpid”使用的request.getParameter(“cmpid”);

它返回空......谁能告诉我什么是真正的问题就在这里?

Answer 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>");

在形式添加name属性为“名称= cmpid”



文章来源: request.getParameter() returns NULL in java servlet on calling it from another servlet