something wrong with Beanutils in servlet

127 Views Asked by At

When I use Beanutils in a servlet, I find that this doesn't work

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


}

and the html snippet is like this

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

this is 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;
    }
}

so I try to find something help, and then , I was told this is a bug in this. So I did it according to the contents of the link, I modified the contents of the plugin folder, but this does not work, and my idea was broken up.

how do i fix this problem? Tomcat version: 9.0.12

ps: the error message was same as that link. it like

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

There are 0 best solutions below