request getParameter is always null when using enctype=“multipart/form-data”

我们两清 提交于 2020-06-05 06:35:00

问题


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

}

回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/34881398/request-getparameter-is-always-null-when-using-enctype-multipart-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!