[英]request getParameter is always null when using enctype="multipart/form-data"
I am performing validation of inputted data such as email, password, name, etc. But I am already stuck on the first stage of validation which is to check if User entered nothing.我正在验证输入的数据,例如电子邮件、密码、姓名等。但我已经陷入验证的第一阶段,即检查用户是否未输入任何内容。
I already added enctype="multipart/form-data"
as mentioned here but now it is always recognizing email
as null
and I can't forward to the login page in case of success (when email is not null).我已经添加了
enctype="multipart/form-data"
正如这里提到的,但现在它总是将email
识别为null
并且在成功的情况下我无法转发到登录页面(当电子邮件不为空时)。
signup.jsp注册.jsp
<form method="POST" action="signup" enctype="multipart/form-data">
<input type="email" name="email" placeholder="tonystark@mail.com">
<input type="submit" value="Submit">
</form>
SignUpAction.java注册操作.java
public class SignUpAction implements Action {
@Override
public String handleRequest(HttpServletRequest req, HttpServletResponse resp, DAOFactory dao)
throws ServletException, IOException {
String email = req.getParameter("email");
if (email == null || email.isEmpty()) {
return "signup"; // It loads signup page again (it works)
}
return "login"; // It should go to the login page (it doesn't work)
}
}
Unless you're planning to use your form for uploading a file, you don't need to specify the encoding type of "multipart/form-data".除非您打算使用表单上传文件,否则不需要指定“multipart/form-data”的编码类型。
<form method="POST" action="signup">
<input type="text" name="email" placeholder="tonystark@mail.com">
<input type="submit" value="Submit">
</form>
The last paragraph in your link states:您链接中的最后一段指出:
"When using enctype="multipart/form-data", all parameters are encoded in the request body. That means that request.getParameter(...) will return null for all posted parameters then." “当使用 enctype="multipart/form-data" 时,所有参数都在请求正文中编码。这意味着 request.getParameter(...) 将为所有发布的参数返回 null。”
Input type: email
输入类型:电子邮件
Email is an html5 input type.电子邮件是一种 html5 输入类型。 How To Use The New Email, URL, and Telephone Input Types .
如何使用新的电子邮件、URL 和电话输入类型。
Since it is a multipart/form-data (usually used for the puropose of uploading one/more file(s)) form the request.getParameter()
method will always return null
.由于它是一个 multipart/form-data(通常用于上传一个/多个文件的目的)表单
request.getParameter()
方法将始终返回null
。
You can try你可以试试
<form method="POST" action="signup" enctype="application/x-www-form-urlencoded">
Or completely remove the enctype
parameter.或者完全删除
enctype
参数。
Some references in another SO question.另一个 SO 问题中的一些参考资料。
How to upload files to server using JSP/Servlet? 如何使用 JSP/Servlet 将文件上传到服务器?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.