当我从jsp表单中检索值时,servlet获取空值

时间:2018-03-11 18:36:23

标签: html mysql jsp servlets

这是我的jsp page.where im显示一个表单并将值发送到另一个servlet页面,将数据插入mysql database.im,使用post方法发送所有values.servlet名称为updateVehicle2

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Modify Vehicle</title>
</head>

<center><h1> Modify Vehicle</h1></center>
<center>
<form action="updateVehicle2" method="Post">
    <label >Enter Vehicle name:</label>
    <input type="text" name="vname" required><br><br>
     <label >Enter Vehicle model:</label>
    <input type="text" name="vmodel" required ><br><br>
    <label >Enter Vehicle ID:</label>
    <input type="text" name="vid"  required ><br><br>
     <label >Select Vehicle type:</label>
    <select name="vtype" required>
        <option disabled selected>select type</option>
        <option value="Ac">Ac</option>
        <option value="NonAc">Non Ac</option>
    </select><br><br>

     <label >Rate per hour:</label>
    <input type="text" name="vrph" required ><br><br>
    <label >Rate per km:</label>
    <input type="text" name="vrpk" required ><br><br>
    <input type="submit" value="Add Vehicles">

</form></center>

这是我的servlet页面。我试图通过使用request.getParameter()来检索值,但我在这里得到空值。每个值都是null.i使用name属性来检索值。但是,它仍然返回null values.please帮我解决这个问题超过5个小时。谢谢提前

String vn=request.getParameter("vname");
   String vmodel=request.getParameter("vmodel");
   String vid=request.getParameter("vid");
   String vtype=request.getParameter("vtype");
   String vrph=request.getParameter("vrph");
   String vrpk=request.getParameter("vrpk");
   response.setContentType("text/html");
   final String JDBC_DRIVER="com.mysql.jdbc.Driver";
   final String DB_URL="jdbc:mysql://localhost:3306/startrek";
   final String user="root";
   final String pass="kingmaker";
   Connection conn=null;
   PreparedStatement stmt;
   stmt = null;
   int numRows=0;
   PrintWriter out = response.getWriter();
   out.println(vn);
   out.println(vmodel);
    try 
    {
       Class.forName(JDBC_DRIVER);
        conn=DriverManager.getConnection(DB_URL,user,pass);
        stmt = conn.prepareStatement("update vehicles set vname=?,vmodel=?,vtype=?,vrph=?,vrpk=? where vid=?;");
        stmt.setString(1,vn);
        stmt.setString(2,vmodel);
        stmt.setString(3,vtype);
        stmt.setString(4,vrph);
        stmt.setString(5,vrpk);
        stmt.setString(6,vid);

        numRows=stmt.executeUpdate();
        if(numRows>0)
        {
            out.println("updated successfully");
        }
        else
        {
            out.println("not succesfully");
        }
    }
    catch(Exception e)
    {
        out.println(e);
    } 
}

}

0 个答案:

没有答案