使用 enctype="multipart/form-data" 时,请求 getParameter 始终为空; [英] 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.
我已经添加了 enctype="multipart/form-data"
如前所述 这里 但现在它总是将 email
识别为 null
并且我无法在成功的情况下转发到登录页面(当电子邮件不为空时).
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).
注册.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
SignUpAction.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)
}
}
推荐答案
除非您打算使用表单上传文件,否则不需要指定multipart/form-data"的编码类型.
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".
<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:
当使用 enctype="multipart/form-data" 时,所有参数都编码在请求正文中.这意味着 request.getParameter(...) 将为所有发布的参数返回 null."
"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."
输入类型:电子邮件
电子邮件是一种 html5 输入类型.如何使用新的电子邮件、URL 和电话输入类型.
Email is an html5 input type. How To Use The New Email, URL, and Telephone Input Types.
这篇关于使用 enctype="multipart/form-data" 时,请求 getParameter 始终为空;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!