简体   繁体   English

Java Servlet POST操作未从请求接收参数

[英]Java Servlet POST action not receiving parameters from request

Yesterday I tried using Tomcat and Servlets for the first time (I come from IIS/C#/MVC). 昨天,我第一次尝试使用Tomcat和Servlet(来自IIS / C#/ MVC)。

I'm also using AngularJS and Guice. 我也在使用AngularJS和Guice。

I've made a Servlet that has a single method: 我制作了一个具有单个方法的Servlet:

@Singleton
@SuppressWarnings("serial")
public class CommandServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println(req.getParameterMap());
    }

}

I've made a service in Angular that looks like the following: 我已经在Angular中提供了如下服务:

app.factory('console', [ '$http', function($http) {
    var console = {};

    console.execute = function(gameId, command) {
        $http.post("Command/", {
            gameId : gameId,
            command : command
        }).success(function(data, status, headers, config) {

        }).error(function(data, status, headers, config) {

        });
    };

    return console;
} ]);

In my controller I inject the service and expose it to the view via an "execute" function on the scope: 在我的控制器中,我注入服务,并通过作用域上的“执行”函数将其公开给视图:

app.controller('PlayController', function($scope, console) {
    $scope.consoleIn = "";

    $scope.gameId = 1;

    $scope.execute = function(command) {
        $scope.consoleOut = console.execute($scope.gameId, command);
    };
});

And in my view I have a button which calls the function and passes in text from an input element: 在我看来,我有一个按钮,该按钮调用函数并从输入元素中传递文本:

<input ng-model="consoleIn" type="text">
<button class="btn" type="button" ng-click="execute(consoleIn)">Go!</button>

For some reason the console on my Tomcat server is printing an empty Map ( {} ) without the parameters being passed with the POST request. 由于某些原因,我的Tomcat服务器上的控制台正在打印一个空的Map( {} ),而没有随POST请求一起传递参数。 When I look at the network tab in Chrome's Console thingy and see that the parameters are being sent ( {"gameId":1,"command":"a"} ), so my question is 1) Am I doing the right thing to get the values out of the POST request ( getParameterMap() ?) and 2) if I am doing the right thing, what am I doing wrong so that the request my browser makes isn't getting to my servlet properly? 当我在Chrome浏览器的控制台中查看“网络”标签时,发现正在发送参数( {"gameId":1,"command":"a"} ),所以我的问题是1)我在做正确的事情{"gameId":1,"command":"a"}从POST请求中获取值( getParameterMap()和2),如果我做对了,那我在做什么错,以致浏览器发出的请求无法正确到达我的servlet?

EDIT: 编辑:

I ended up using Jersey as my container (I think it's called) instead of Java's default Servlets. 我最终使用Jersey作为我的容器(我认为它被称为),而不是Java的默认Servlet。 I did this after seeing Jersey popping up with Guice often on Google, thinking there would be sufficient documentation to get the two of them working together. 在看到Jersey经常在Google上弹出Guice之后,我这样做了,以为会有足够的文档来使他们两个一起工作。 There are several examples and I sort of drew on several, especially this one . 有几个例子,我有点借鉴了几个,尤其是这个例子。

The snag I ran into getting it to work was this , but now everything's good to go. 我跑进它让工作最大的困难是这个 ,但现在一切都很好走。

Overall I'd say that if you like Guice for your DI and want to make a Java website, Jersey is good. 总体而言,我想说的是,如果您喜欢Guice作为DI并想要建立一个Java网站,Jersey很好。 It appears to be for a different more specialized functionality of RESTful services than regular servlets - but that's what I needed, anyway. 与常规的servlet相比,它似乎是针对RESTful服务的一种不同的更专业的功能-但这正是我所需要的。 From my un-scientific Googling observations besides Tomcat there's Grizzly and Jetty that are popular as well - you may want to look into them if you're having trouble with Tomcat. 从我不科学的Google谷歌搜索结果中,除了Tomcat之外,还有Grizzly和Jetty也很流行-如果您在使用Tomcat时遇到问题,可能需要研究一下。

I hope this edit saves someone the hours I spent yesterday and today getting it to work. 我希望此编辑可以节省我昨天和今天花费的时间。

What does it actually print? 实际打印什么? Some of the Tomcat Map implementations don't print their contents in their toString() method. 某些Tomcat Map实现未在其toString()方法中打印其内容。 Try another way of seeing what's in it. 尝试另一种查看其中内容的方法。 You'll find its all there. 您将在这里找到所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM