我编写了以下表单和servlet,希望servlet从表单中返回文本字段的值,但它返回 null 。应该纠正什么?
<html>
<head>
<title>Simple form</title>
</head>
<body>
<form method="post" action="theServlet">
<input type="text" id="userName"/>
<input type="submit" value="Post"/>
</form>
</body>
</html>
public class theServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("userName");
response.setContentType("text/html");
PrintWriter writer=response.getWriter();
writer.println("<html>");
writer.println("userName = "+ username);
writer.println("</html>");
}
}
答案 0 :(得分:2)
您应该使用name
属性而不是id
将参数发送到服务器。
<input type="text" id="userName" name="username" />