views:

33

answers:

1
Ext.Ajax.request({url:'DeleteAction',success: doneFunction,failure: errorFunction,params:{name:rname}});

The above code is my Ajax request which goes to DeleteAction Servelet. Can anyone let me know what happens next in the below code.

  1. When this file is called what is first thing getting called.
  2. What does the doGet and doPost method do?
  3. How does it identify the doProcess method in this?
  4. Is it neccessary to have a constructor.
  5. How is the response sent back to Ajax.

    public class DeleteAction extends HttpServlet implements Servlet {
    
    
    public DeleteAction() {
        super();
    }
    
    
    protected void process(HttpServletRequest request, HttpServletResponse response) {
    
    
    
    try {
        ServletOutputStream sos = response.getOutputStream();
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("text/plain");
    
    
        String name = request.getParameter("name");
    
    
        System.out.println("Name: " + name);
    
    
        String query = "DELETE from CRUD_DATA where name='" + name + "'";
        System.out.println("Query:" + query);
    
    
        OracleDataSource ods = new OracleDataSource();
        ods.setUser("abdel");
        ods.setPassword("password");
        ods.setURL("jdbc:oracle:thin:@//127.0.0.1/XE");
    
    
        Connection conn = ods.getConnection();
        Statement statement = conn.createStatement();
    
    
        statement.executeUpdate(query);
        conn.commit();
        conn.close();
    
    
        sos.print("{success:true}");
        sos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } }
+2  A: 

1. When this file is called what is first thing getting called. Loosely speaking, service() method, assuming that the servlet is already loaded. Otherwise, init() method.

2. What does the doGet and doPost method do? They are there to be invoked by service() method. service() method checks the request-method, POST or GETor PUT or...., and then invoke the respective method. Check out the docs.

3. How does it identify the doProcess method in this? You invoked it yourself in doGet() and doPost(), both.

4. Is it neccessary to have a constructor. No. Servlet container instantiate the servlet for us. In case, we intend to initialise few things at creation time, we can do that in init() method. Which is merely there for similar purpose. So, we can override that one.

5. How is the response sent back to Ajax. Its a JSON string.

Adeel Ansari