Issue
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 href="https://stackoverflow.com/questions/16398374/java-servlet-validation-confused">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).
Code
signup.jsp
<form method="POST" action="signup" enctype="multipart/form-data">
<input type="email" name="email" placeholder="[email protected]">
<input type="submit" value="Submit">
</form>
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)
}
}
Solution
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="[email protected]">
<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."
Input type: email
Email is an html5 input type. How To Use The New Email, URL, and Telephone Input Types.
Answered By - Perdomoff
Answer Checked By - Gilberto Lyons (JavaFixing Admin)