首页 文章

通过Ajax对请求进行Servlet流程

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

上面的代码是我的Ajax请求,它发送到DeleteAction Servelet . 任何人都可以让我知道下面的代码接下来会发生什么 .

  • 当调用此文件时,首先调用的是什么 .

  • doGet和doPost方法有什么作用?

  • 如何识别doProcess方法?

  • 是否有必要拥有一个构造函数 .

  • 如何将响应发送回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);
}
}

1 回答

  • 2

    1. When this file is called what is first thing getting called. 松散地说,service()方法,假设servlet已经加载 . 否则,init()方法 .

    2. What does the doGet and doPost method do? 它们由 service() 方法调用 . service() 方法检查 request-methodPOSTGETPUT 或....,然后调用相应的方法 . 看看docs .

    3. How does it identify the doProcess method in this? 您自己在doGet()doPost()中调用了它们 .

    4. Is it neccessary to have a constructor. 否.Servlet容器为我们实例化servlet . 如果我们打算在创建时初始化一些东西,我们可以在init()方法中做到这一点 . 这仅仅是出于类似的目的 . 所以,我们可以覆盖那一个 .

    5. How is the response sent back to Ajax. 这是一个JSON字符串 .

相关问题