2016-11-21 65 views
-1

每當我嘗試從jsp表單添加數據時,URL就會改變,然後什麼都不會發生。我檢查了web.xml文件,一切正常...在我點擊提交按鈕後,它使用url =/servlet名稱進入新的空白頁面。我嘗試通過使用相同的連接手動插入數據,它工作正常。無法通過servlet將jsp表單數據插入到數據庫

JSP形式:

<form action="add_patient" method="post"> 
    <input type="text" id="fname" required> <br /> 
    <input type="text" id="mname"> <br /> 
    <input type="text" id="lname" required> <br /> 
    <input type="text" id="address" required> <br /> 
    <input type="number" id="contact" required> <br /> 
    <select id="gender" required> 
     <option value="">Choose here</option> 
     <option value="male">Male</option> 
     <option value="female">Female</option> 
     <option value="other">Other</option> 
    </select> 
    <input type="date" id="dob" required> <br /> 
    <input type="number" id="alloted"> <br /> 
    <input type="text" id="problem" required> <br /> 
    <input type="submit" value="Add Patient"> 
    <input type="reset" value="Reset Input"> 
</form> 

Servlet的文件:

public class add_patient extends HttpServlet 
{ 
    private Connection con=null; 

    private PreparedStatement stmt=null; 

    private final ResultSet rs=null; 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, 
     IOException, 
     ParseException, 
     SQLException 
    { 
     response.setContentType("text/html;charset=UTF-8"); 
     try (PrintWriter out=response.getWriter()) 
     { 

      // add_patients ap = new add_patients(); 

      String fname=request.getParameter("fname"); 
      String mname=request.getParameter("mname"); 
      String lname=request.getParameter("lname"); 
      String address=request.getParameter("address"); 
      int contact=Integer.parseInt(request.getParameter("contact")); 
      String gender=request.getParameter("gender"); 
      String dob=request.getParameter("dob"); 
      int alloted=Integer.parseInt(request.getParameter("alloted")); 
      String problem=request.getParameter("problem"); 

      out.println(address); 

      try 
      { 
       dbconnection db=new dbconnection(); 
       db.getConnection(); 

       con=dbconnection.getConnection(); 
       stmt=(PreparedStatement)con.createStatement(); 

       String sql="INSERT INTO patient (fname,mname, lname, address, contact, gender, dob, room_no, problem)" + "   VALUES (' " + fname + " ', ' " + mname + " ', '" + lname + "', '" + address + "', '" + contact + "', '" + gender + "', '" + dob + "', '" + alloted + "', '" + problem + "')"; 

       stmt.executeUpdate(sql); 
       if (stmt.executeUpdate(sql) > 0) 
       { 

        request.getSession().setAttribute("error", "sorry the operation failed!please try again."); 
        request.getRequestDispatcher("add_patient.jsp").forward(request, response); 
       } 
       else 
       { 
        request.getRequestDispatcher("add_patient.jsp").forward(request, response); 
       } 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      con.close(); 
     } 
    } 
} 

如果需要的話..數據庫連接:

public class dbconnection 
{ 
    private static String url="jdbc:mysql://localhost/HMS"; 

    private static String driverName="com.mysql.jdbc.Driver"; 

    private static String username="root"; 

    private static String password=""; 

    private static Connection con; 

    public static Connection getConnection() 
    { 
     try 
     { 
      Class.forName(driverName); 
      try 
      { 
       con=DriverManager.getConnection(url, username, password); 
      } 
      catch (SQLException ex) 
      { 
       // log an exception. fro example: 
       ex.printStackTrace(); 
       System.out.println("Failed to create the database connection."); 
      } 
     } 
     catch (ClassNotFoundException ex) 
     { 
      ex.printStackTrace(); 
      // log an exception. for example: 
      System.out.println("Driver not found."); 
     } 
     return con; 
    } 
} 
+0

任何有趣的網絡服務器錯誤日誌?另外,我建議你刪除你的catch(Exception),並讓異常通過'throws'子句傳播。 –

回答

0

<input>值需要有一個name屬性,爲了通過POST或GET HTTP請求發送到服務器。這應該會導致您所有的request.getParameter("...")行將返回null,並在第一次嘗試使用這些值時失敗,並返回NullPointerException

正如@LittleSanti所說的,如果您打算自己管理這些異常,您應該檢查SystemOut/SystemErr日誌以找出拋出的異常,並且充分利用catch塊。

+0

感謝隊友,但我已經解決了它...問題是,我錯過了名稱標籤>謝謝反正隊友!乾杯。 –

相關問題