Java HttpServlet没有从backbone.js获得GET / POST(Java HttpServlet not getting GET/POST from backbone.js)

编程入门 行业动态 更新时间:2024-10-23 17:27:23
Java HttpServlet没有从backbone.js获得GET / POST(Java HttpServlet not getting GET/POST from backbone.js)

我是backbone.js和前端开发的新手。 如果我的问题是愚蠢的,请注意。 我搜索了谷歌和stackoverflow并没有得到答案。 这是我的问题。 因为我使用前端的骨干和后端的java,我正在尝试使用backbone的collections.fetch()和save()等与Java Servlet进行通信。 模型就像这样简单:

var Todo = Backbone.Model.extend({ defaults: { title: '', completed: false } }); var TodosCollection = Backbone.Collection.extend({ model: Todo, url: '/todo' }); var todos = new TodosCollection(); todos.fetch();

我在web.xml中

<servlet> <servlet-name>todo</servlet-name> <servlet-class>test.web.TodoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>todo</servlet-name> <url-pattern>/todo</url-pattern> </servlet-mapping>

对于TodoServlet.java,我只需要以下代码来检查它是否获得GET和POST:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("In /todo: doGet"); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("In /todo: doPost"); }

当我在Tomcat上运行它并打开网页时,服务器似乎没有得到GET。 这是一个正确的方法吗? 提前致谢!

I am new to backbone.js and front end development. Appologies if my question is stupid. I searched over google and stackoverflow and didn't get an answer. Here is my question. As I am using backbone for the front end and java for my back end, I am trying to backbone's collections.fetch(), and save(), etc to talk to the Java Servlet. The model is as simple as this:

var Todo = Backbone.Model.extend({ defaults: { title: '', completed: false } }); var TodosCollection = Backbone.Collection.extend({ model: Todo, url: '/todo' }); var todos = new TodosCollection(); todos.fetch();

I have in web.xml

<servlet> <servlet-name>todo</servlet-name> <servlet-class>test.web.TodoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>todo</servlet-name> <url-pattern>/todo</url-pattern> </servlet-mapping>

And for TodoServlet.java, I simply have the following code just to check if it gets GET and POST:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("In /todo: doGet"); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("In /todo: doPost"); }

When I run this on Tomcat and open the webpage, it seems the server is not getting GET. Is this a right way to do it? Thanks in advance!

最满意答案

调用的URL应包括上下文路径,即部署应用程序的路径。 这通常是包含应用程序的文件夹的名称或WAR文件的名称,不带.war扩展名。 因此,如果您的应用程序位于文件夹xxx.war ,请尝试url: "/xxx/todo" 。

还要检查Firebug以查看被调用的确切URL以及服务器的响应。

THe URL called should include the context path, i.e. the path under which the application is deployed. This is usually the name of the folder that contains the application or the name of the WAR file, without the .war extension. So if your application is located in the folder xxx.war, try url: "/xxx/todo".

Also check with Firebug to see the exact URL that gets called and the response from the server.

更多推荐

本文发布于:2023-07-24 10:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1245008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HttpServlet   Java   backbone   POST   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!