简体   繁体   中英

Java HttpServlet not getting GET/POST from backbone.js

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!

THe URL called should include the context path, ie 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.

Send something back to the caller so that it will get a response of 200.

PrintWriter out = response.getWriter();
out.println("Hello World");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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