-1

我想用jsp处理这个表单字段:

<div class="control-group">

                                                <!-- Text input-->
                                                <label class="control-label" for="input01">Email:</label>
                                                <div class="controls">
                                                    <input name="email" placeholder="email"
                                                        class="input-xlarge" type="text"
                                                        value="<%=request.getParameter("email")%>">
                                                </div>
                                            </div>
                                            <div class="control-group">
                                                <!-- Text input-->
                                                <label class="control-label" for="input01">Password:</label>
                                                <div class="controls">
                                                    <input name="password" placeholder="password"
                                                        class="input-xlarge" type="text"
                                                        value="<%=request.getParameter("password")%>">
                                                </div>
                                            </div>

当我按下提交时,我得到一个

空指针异常

这就是我的servlet方法:

@Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.log(Level.INFO, "Creating User!!!");
        logger.info("Request: " +  req.toString());
        PrintWriter out = resp.getWriter();//Here I get the null Pointer exception

        String email = req.getParameter("email");
        String password = req.getParameter("password");
        logger.info(email + password);
        try {
            user.insert(email, password);
            Log.info("Inserted: " + email + "    " + password);
        } catch (Exception e) {
            String msg = DAOUser.getErrorMessage(e);
            out.print(msg);
        }
    }

.

如何修复这个空指针异常?

更新

我的小服务程序:

public class DAOServletUser extends HttpServlet {

    private static final long serialVersionUID = 6820994892755862282L;

    private static final Logger logger = Logger.getLogger(DAOServletUser.class.getCanonicalName());
    /**
     * Get the entities in JSON format.
     */

    public DAOServletUser() {
        super();
    }

    public IDAOUser user;

    /**
     * Create the entity and persist it.
     */
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.log(Level.INFO, "Creating User!!!");
        logger.info("Request: " +  req.toString());

        if(resp==null) {
            System.out.println("Respond is NULL!!!");
        }

        PrintWriter out = resp.getWriter();//Here I get the null Pointer exception



        String email = req.getParameter("email");

        if(email==null) {
            System.out.println("email is null");
        } else {
            System.out.println("email is NOT null");
        }
        String password = req.getParameter("password");
        if(password==null) {
            System.out.println("password is null");
        } else {
            System.out.println("password is NOT null");
        }
        logger.info(email + "::" + password);
        try {
            user.insert(email, password);
            Log.info("Inserted: " + email + "    " + password);
        } catch (Exception e) {
            String msg = DAOUser.getErrorMessage(e);
            out.print(msg);
        }
    }
4

1 回答 1

2

我假设IDAOUser是一个接口。要在 Java 中实现接口,您可以implements在类级别使用关键字,如下所示:

public class DaoUserImpl implements IDAOUser {
    public void insert(String email, String password) {
        // your code for inserting goes here
    }
}

在您的 servlet 类中,而不是

public IDAOUser user;

像这样实例化它(使用new关键字)

public IDAOUser user = new DaoUserImpl();

然后,您可以调用user.insert(email,password)不是null.

但是,如果 DaoUserImpl 使用共享实例数据,则必须小心此解决方案可能出现的多线程问题。您可能希望每个请求都有一个类实例。

于 2013-03-22T23:25:50.580 回答