Nick Div
Nick Div

Reputation: 5628

Invoke only a method of a servlet class not the whole servlet

//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.

Upvotes: 0

Views: 1396

Answers (2)

Costi Ciudatu
Costi Ciudatu

Reputation: 38235

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.

Upvotes: 3

Isaac
Isaac

Reputation: 16736

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?

Upvotes: 4

Related Questions