4

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).


Code

signup.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

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)
    }

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
rand0rn
  • 678
  • 2
  • 12
  • 29

2 Answers2

2

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:

"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.

Perdomoff
  • 938
  • 2
  • 7
  • 26
1

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.

You can try

 <form method="POST" action="signup" enctype="application/x-www-form-urlencoded">

Or completely remove the enctype parameter.

Some references in another SO question.

How to upload files to server using JSP/Servlet?

Community
  • 1
  • 1
Marcx
  • 6,806
  • 5
  • 46
  • 69