request.getParameter()在servlet中返回null值...不知道什么是错的

时间:2016-01-22 05:35:22

标签: java servlets

这是我通过输入表单发送数据的html表单....

<form role="form" action = "AddServlet" method="GET">
  <div class="form-group">
    <label for="name" class= "bg-primary">Name</label>
    <input type="text" class="form-control" name="name" placeholder="Enter name of the book">
  </div>
  <div class="form-group">
    <label for="author" class= "bg-primary">Author</label>
    <input type="text" class="form-control" name="author" placeholder="Enter the authors name">
  </div>
  <div class="form-group">
    <label for="count" class= "bg-primary">Count</label>
    <input type="number" class="form-control" name="count" placeholder="Enter the number of books">
  </div>
   <div class="form-group">
    <label for="description" class= "bg-primary">Description</label>
     <textarea class="form-control" rows="5" name="description" placeholder="Enter book description"></textarea>
  </div> 

  <input type="submit" value = "submit">
</form>

这是我通过request.getParameter接收数据的servlet ....

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  
import org.hibernate.cfg.Configuration;  

@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


   String name, author, description, count;



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

         Configuration cfg=new Configuration();  
            cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  

            //creating seession factory object  
            SessionFactory factory=cfg.buildSessionFactory();  

            //creating session object  
            Session session=factory.openSession();  

            //creating transaction object  
            Transaction t=session.beginTransaction();  

            Books b = new Books();  

            name = request.getParameter("name");
            author = request.getParameter("author");
            description = request.getParameter("description");

            count = request.getParameter("count");


            System.out.println(count + "dfghjkls");  
            b.setName(name);  
            b.setAuthor(author);  
            b.setDescription(description);
            b.setCount(count);

            session.persist(b);//persisting the object  

            t.commit();//transaction is commited  
            session.close();  

            System.out.println("successfully saved");  


    }

}

但是这会返回空值...不知道这有什么问题......

1 个答案:

答案 0 :(得分:0)

得到答案...标签为=“名称”是问题....删除它.. 也许“for =”会造成歧义,因此浏览器无法收集数据