0

我创建了这个 servlet 方法:

/**
 * Create the entity and persist it.
 */
protected void doPut(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    logger.log(Level.INFO, "Creating User!!!");
    PrintWriter out = resp.getWriter();

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

并希望通过以下值插入 GAE DB:

    <form class="form-horizontal" action="/user" method="put">
                                        <fieldset>
                                            <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">
                                                    <%=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">
                                                    <%=request.getParameter("email") %>
                                                </div>
                                            </div>

                                        </fieldset>
                                    </form>
<div class="modal-footer">
                                    <a href="#" class="btn" data-dismiss="modal">Close</a> <a
                                        href="#" class="btn btn-primary" type="submit" value="Submit">Save
                                        Changes</a>
                                </div>

但是,它不起作用,如果 servlet 映射正确:

<servlet-mapping>
        <servlet-name>DAOServletUser</servlet-name>
        <url-pattern>/user</url-pattern>
    </servlet-mapping>

我的错误是什么?

更新 当我重新启动服务器时,我得到了这个:

Mär 22, 2013 6:46:07 AM com.google.appengine.tools.development.ApiProxyLocalImpl log
SEVERE: javax.servlet.ServletContext log: unavailable
javax.servlet.UnavailableException: Servlet class gaeTrader2.server.dao.DAOServletUser is not a javax.servlet.Servlet
    at org.mortbay.jetty.servlet.ServletHolder.checkServletType(ServletHolder.java:362)
    at org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:243)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:685)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:224)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at com.google.appengine.tools.development.JettyContainerService.startContainer(JettyContainerService.java:228)
    at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:255)
    at com.google.appengine.tools.development.AbstractServer.startup(AbstractServer.java:79)
    at com.google.appengine.tools.development.DevAppServerImpl$Servers.startup(DevAppServerImpl.java:451)
    at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:198)
    at com.google.appengine.tools.development.gwt.AppEngineLauncher.start(AppEngineLauncher.java:97)
    at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)
    at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1093)
    at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:836)
    at com.google.gwt.dev.DevMode.main(DevMode.java:311)

为什么?

Servlet 类 gaeTrader2.server.dao.DAOServletUser 不是 javax.servlet.Servlet

我的 Servlet 看起来像这样:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.log.Log;


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

    IDAOUser user;

    /**
     * Create the entity and persist it.
     */
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        logger.log(Level.INFO, "Creating User!!!");
        PrintWriter out = resp.getWriter();

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

2 回答 2

2

我认为您应该在服务器上使用 doGet 或 doPost 并在客户端使用对应的方法。

UPD:您在 web.xml 中的 servlet 描述似乎也不完整:

<servlet>
    <servlet-name>DAOServletUser</servlet-name>
<servlet-class>your.package.name.YourServletClass</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>DAOServletUser</servlet-name>
        <url-pattern>/user</url-pattern>
    </servlet-mapping>

于 2013-03-22T06:49:33.103 回答
1

您的 DAOServletUser 类应该从 HttpServlet 扩展。

于 2013-03-22T07:05:18.943 回答