0

I have the following simple form element:

 <form action="Test" method="POST" enctype="multipart/form-data">
    <input type="text" name="vorname" title="Vorname"></input> 
    <input type="text" name="nachname" title="Nachname"></input> 
 <input
        type="submit"></input>
</form>

my POST method in the Servlet looks the following way:

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

     System.out.println(request.getParameter("vorname"));


    String var_Name =  request.getParameter("nachname");
    String var_Vorname =  request.getParameter("vorname");

     try {
        con = Datenbankverbindung();

         if (con != null) {
            System.out.println("Verbunden");

            stmt = con.prepareStatement("insert into ktzvtest (Name, 
  Vorname)  values (?, ?)");
            stmt.setString(1, var_Name);
            stmt.setString(2, var_Vorname);
            stmt.executeUpdate();
            System.out.print("erfolgreich");
        }

    } catch (Exception e) {
        e.printStackTrace();

    }
     try {
        con.close();
     } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }
  }

So the two variables var_Name and var_Vorname are alway NULL. What's the problem ?

legalizeSINCE88
  • 97
  • 1
  • 11

1 Answers1

1

Because you use enctype="multipart/form-data" you can not retrieve parameters using plain request.getParameter.

Remove enctype="multipart/form-data" it will work

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • in addition to the above answer check this link to know more about enctype https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean – Arun Prasat Jun 27 '19 at 05:56