Servlet中的Beanutils出了点问题

时间:2018-11-05 01:39:16

标签: java apache-commons-beanutils

当我在servlet中使用Beanutils时,我发现这不起作用

package servlet.web15;

import org.apache.commons.beanutils.BeanUtils; import 
javax.servlet.ServletException; import 
javax.servlet.annotation.WebServlet; import 
javax.servlet.http.HttpServlet; import 
javax.servlet.http.HttpServletRequest; import 
javax.servlet.http.HttpServletResponse; import java.io.IOException; 
import java.io.PrintWriter; import 
java.lang.reflect.InvocationTargetException; import java.util.HashMap; 
import java.util.Map;

@WebServlet(name = "RegisterServlet", urlPatterns = "/register") 
public class RegisterServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter responseWriter = response.getWriter();

        Map<String, String[]> parameterMap = request.getParameterMap();

        for (Map.Entry<String, String[]> entry :
                parameterMap.entrySet()) {
            String name = entry.getKey();
            for (String str :
                    entry.getValue()) {
                responseWriter.println(name + ": " + str);
            }
        }

        Bean bean = new Bean();

      responseWriter.println(bean.toString());

        try {
            BeanUtils.populate(bean, parameterMap);
        } catch (IllegalAccessException e) {
            responseWriter.println(e.getCause());
        } catch (InvocationTargetException e) {
            responseWriter.println(e.getCause());
        }
      responseWriter.println(bean.toString()); // this statement doesn't work, something wrong with the try and catch

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


}

和html代码段是这样的

<div class="form form-signup">
        <form action="/register" method="post">
            <lable>FULL NMAE</lable>
            <input type="text"
                   placeholder="Your full name" name="name">
            <lable>E-MAIL</lable>
            <input type="email"
                   placeholder="Your e-mail" name="email">
            <lable>PASSWORD</lable>
            <input type="password"
                   placeholder="Your password" name="password">
            <p class="terms">
                <input type="checkbox">
                I agree all statments in
                <a href="#" class="lined-link">terms of service</a>
            </p>
            <input type="submit"
                   class="form-btn"
                   value="Sign Up"/>
            <a href="#" class="lined-link to-signin-link">I'm already member</a>
        </form>
</div>

这是Bean.class

package servlet.web15;

public class Bean {
    private String name;
    private String email;
    private String password;

    public Bean() {
    }

    public Bean(String name, String email, String password) {
        this.name = name;
        this.email = email;
        this.password = password;
    }

    @Override
    public String toString() {
        return "Bean{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

所以我尝试寻求帮助,然后,我被告知这是this中的错误。 因此,我根据链接的内容进行了此操作,我修改了plugin文件夹的内容,但这无效,并且我的想法被破坏了。

我该如何解决此问题? Tomcat版本:9.0.12

ps:错误消息与该链接相同。就像

11-Apr-2017 18:20:06.973 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\servers\apache-tomcat-8.5.13\webapps\manager

11-Apr-2017 18:20:07.084 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\servers\apache-tomcat-8.5.13\webapps\manager has finished in 112 ms

0 个答案:

没有答案