0

//Below is my servlet called HtmlTable. I am trying to implement shopping cart like functionality here. addingItems is another class that puts elements in a ArrayList. Whenever I add something from website I want AJAX request to just call the method jusAdding() not the processRequest method. so that when sufficient items are added to the ArrayList I can print it on the screen by calling aI.getItems() which will happen automatically when simply call the servlet. Is it possible?? If yes how should I write the URL in AJAX request.

public class HtmlTable extends HttpServlet {

        addingItems aI = new addingItems();

        public void jusAdding(HttpServletRequest request, HttpServletResponse response){
            aI.addItemsInCart(request, response);
        }
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            List<itemsCart> itemsInCart = aI.getItemsInCart();

            try {

                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet HtmlTable</title>");
                out.println("</head>");
                out.println("<body>");
             //whatever content is in the itemsInCart will be displayed here in body tag   
    out.println("</body>");
                out.println("</html>");
            } finally {
                out.close();
            }
        }



    }

//forgive me if I am not clear. Let me know I'll update as per reader convenience.

Nick Div
  • 5,338
  • 12
  • 65
  • 127

2 Answers2

4

You can't do that.

An HTTP request flowing through a Servlet will always go to the Servlet's service method. What you need is to use the service method as a controller, so it inspects the incoming request and calls jusAdding (or other methods) depending on the request's parameters.

Most likely, you will want to use an already-existing framework for doing that.

The following should give you more information, as well as some ideas how to go about doing so: How to use Servlets and Ajax?

Community
  • 1
  • 1
Isaac
  • 16,458
  • 5
  • 57
  • 81
3

None of your methods are going to be called by the container. You should start with the servlet specification (or at least the HttpServlet javadoc).

The container calls the service() method of your servlet, which in turn, for HttpServlet dispatches to the method corresponding to the request HTTP method: doGet(), doPost() etc. That's where you're supposed to hook your logic (overwrite one of those, or service() itself and put your code there).

In order to distinguish between a "full page request" and an "AJAX request", you need the client to include some discriminator in that call: some request parameter with distinct values, a different HTTP method or whatever. Once you have that, in your doGet() method for instance, you can check that discriminator and invoke either justAdd() or processRequest() according to the client request.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • If I do that and once the execution of jusadding() is completed then it wont redirect the client side page will it. I mean the page from where I am making an AJAX request that page will still remain open right in the browser?? – Nick Div Nov 22 '12 at 09:34
  • Since it's an AJAX call, you couldn't redirect the page even if you wanted to. That can only be achieved via an explicit Javascript invocation in your JS callback. – Costi Ciudatu Nov 22 '12 at 09:35