2016-01-22 99 views
-1

這是從那裏我「米發送通過輸入表單中的數據的HTML表單....的request.getParameter()返回空值的servlet ......不知道什麼是錯

<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 recieving的數據的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"); 


    } 

} 

但這返回空值...不知道怎麼回事錯這個......

+0

請嘗試不創建一個新的會話和事務第一 - 只是爲了檢查新的會話是否覆蓋原來的。 – koem

+0

什麼是返回null值?該請求包含您在html表單中輸入的數據。 – WrongRhyme

+0

您是否在uClass Book中創建了setName(name),setAuthor(author)? –

回答

0

得到的答案...標籤=「名稱」是p roblem ....刪除它.. 也許「for =」是造成歧義,所以瀏覽器無法收集數據

相關問題