错误:此URL不支持HTTP方法GET

时间:2013-06-15 11:40:48

标签: google-app-engine jsp

当我向我的servlet发出get请求时,我有这个响应:此URL不支持HTTP方法GET。本地我有相同的错误,但随后有查询的输出 这是代码:

    import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * This servlet responds to the request corresponding to users. The class
 * creates and manages the User Entity
 * 
 * @author
 */
@SuppressWarnings("serial")
public class UserServlet extends HttpServlet {

  private static final Logger logger = Logger.getLogger(UserServlet.class.getCanonicalName());

    /**
     * Get the requested customer entities in JSON format
     */
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

    super.doGet(req, resp);
    logger.log(Level.INFO, "Obtaining Customer listing");
    String searchFor = req.getParameter("username");
    String password= req.getParameter("password");
    PrintWriter out = resp.getWriter();
    Iterable<Entity> entities = null;
    if (searchFor == null || searchFor.equals("")) {
     // entities = User.getAllUsers();
     // out.println(Util.writeJSON(entities));
    } else {
      entities = User.getUser(searchFor);
      Entity result=entities.iterator().next();
      if(result!=null){
          if(result.getProperty("password").equals(password)){        
              out.println(Util.writeJSON(entities));
             // resp.sendError(HttpServletResponse.SC_ACCEPTED);
          }else{
              resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
          }
      }else{
          resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
      }

    }
    return;
  }

    /**
     * Insert the new customer
     */
  protected void doPut(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    logger.log(Level.INFO, "Creating Customer");
    String firstName = req.getParameter("name");
    String lastName = req.getParameter("surname");
    String phone = req.getParameter("phone");
    String address = req.getParameter("address");
    String city = req.getParameter("city");
    String state = req.getParameter("state");
    String zip = req.getParameter("zip");
    String email = req.getParameter("email");
    String username=req.getParameter("username");
    String password=req.getParameter("password");
    User.createOrUpdateCustomer( firstName, lastName, phone, address, city, state, zip, email, username, password);

  }

    /**
     * Delete the customer
     */
  protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    String customerName = req.getParameter("id");
    logger.log(Level.INFO, "Deleting User {0}", customerName);
    Key key = KeyFactory.createKey("Customer", customerName);
    try {
        //CASCADE_ON_DELETE
      Iterable<Entity> entities = Util.listChildKeys("Order", key);
      final List<Key> orderkeys = new ArrayList<Key>();
      final List<Key> linekeys = new ArrayList<Key>();
      for (Entity e : entities) {
        orderkeys.add(e.getKey());
        Iterable<Entity> lines = Util.listEntities("LineItem", "orderID",String.valueOf(e.getKey().getId()));
        for(Entity en : lines){
          linekeys.add(en.getKey());
        }
      }
      Util.deleteEntity(linekeys);
      Util.deleteEntity(orderkeys);
      Util.deleteFromCache(key);
      Util.deleteEntity(key);
    } catch (Exception e) {
      String msg = Util.getErrorResponse(e);
      resp.getWriter().print(msg);
    }
  }

    /**
     * Redirect the call to doDelete or doPut method
     */
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    String action = req.getParameter("action");
    //resp.getWriter().print(req.getParameterMap());
//resp.getWriter().print(action);
    if (action.equalsIgnoreCase("delete")) {
      doDelete(req, resp);
      return;
    } else if (action.equalsIgnoreCase("registra")) {
             doPut(req, resp);
             resp.sendError(2);
             resp.sendRedirect("/index.jsp");
      return;
    }
    doGet(req, resp);
  }
}

我看过几篇帖子,但我没有找到答案

1 个答案:

答案 0 :(得分:0)

创建一个简单的BaseServlet扩展HttpServlet; 然后创建UserServlet扩展BaseServlet