在JSP上显示为null的表单值

时间:2016-03-05 08:51:37

标签: javascript html forms jsp input

当我通过from提交输入值并尝试在服务器端访问它时, 每个值都显示为null。虽然,当我检查网络请求时,值会正确附加到它们上面。只有当它到达服务器端jsp代码时它才会显示为空。

这是html方面的表格:

<form name="modifyUser" action="/UserController/modifyUser" method="post" >
            <label for="userName">User Name</label>
            <input type="text" name="userName" id="userName" value="test1">    <br> 

        <label for="fName">First Name</label>
        <input type="text" name="fName" id="fName" value="test1"><br>

        <label for="lName">Last Name</label>
        <input type="text" name="lName" id="lName" value="test1"><br>

        <label for="email">Email</label>
        <input type="text" name="email" id="email" value="test@test.net"><br>

        <label for="nPass">New Password</label>
        <input type="text" name="nPass" id="nPass"><br>

        <label for="cPass">Confirm Password</label>
        <input type="text" name="cPass" id="cPass"><br>

        <div id="warning" class="feedback"></div>
        <input type="submit" value="Save" id="submit"/>

        <input type="hidden" name="userID" id="userID" value="1">
    </fieldset>

    <br/>
</form>

以下是服务器端的代码段:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if(requestURI.endsWith("modifyUser")){
        user.setEmail((String)request.getParameter("mail"));
        user.setUserName((String)request.getParameter("name"));
        user.setfName((String)request.getParameter("fmail"));
        user.setlName((String)request.getParameter("lmail"));
        String cPass = (String)request.getParameter("cPass");
        String nPass = (String)request.getParameter("nPass");
    }
}

1 个答案:

答案 0 :(得分:0)

由于我们在请求参数中没有具有此类id(mail / name / fmail / lmail)的值,因此我们获取NULL值(从jsp传递的id的请求与从服务器端获得的值不匹配。

请尝试使用以下服务器端代码。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(requestURI.endsWith("modifyUser")){
    user.setEmail((String)request.getParameter("email"));
    user.setUserName((String)request.getParameter("userName"));
    user.setfName((String)request.getParameter("fName"));
    user.setlName((String)request.getParameter("lName"));
    String cPass = (String)request.getParameter("cPass");
    String nPass = (String)request.getParameter("nPass");
}

}