一个Servlet处理增删改查的方法

处理的思路是在servlet中定义不同的增删改查方法,页面请求 的时候携带请求的参数,根据参数判断调用不同的方法。

package cn.xm.small.Servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import cn.xm.small.bean.Register;
import cn.xm.small.service.RegisterService;
import cn.xm.small.service.impl.RegisterServiceImpl;

/**
 * @author liqiang
 * @version 创建时间:2017年8月23日 下午8:39:34
 * @description:
 */
@WebServlet("/productServlet")
public class ProductServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RegisterService service = new RegisterServiceImpl();
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        String type = request.getParameter("type");
        if (type != null && "add".equals(type)) {
            try {
                this.add(request, response, service);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (type != null && "query".equals(type)) {
            try {
                this.query(request, response, service);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (type != null && "update".equals(type)) {
            try {
                this.update(request, response, service);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (type != null && "delete".equals(type)) {
            try {
                this.delete(request, response, service);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    @SuppressWarnings("unused")
    public void add(HttpServletRequest request, HttpServletResponse response, RegisterService service)
            throws Exception {

    }

    // 根据id删除
    public void delete(HttpServletRequest request, HttpServletResponse response, RegisterService service)
            throws Exception {

    }

    // 修改
    public void update(HttpServletRequest request, HttpServletResponse response, RegisterService service)
            throws Exception {
        
    }

    // 查询
    public void query(HttpServletRequest request, HttpServletResponse response, RegisterService service)
            throws Exception {

    }

}

 请求的时候:

 

ajax请求:

// ajax异步删除后刷新页面
function deleteInfo(id) {
    alert("dele");
    $.ajax({
        url : "/small/productServlet",
        async : true,
        type : "POST",
        data : {
            "type" : "delete",
            "id" : id
        },
        success : function(data) {
            alert(data);
            // 删除成功后刷新页面
            window.location.reload();
        },
        error : function() {
            alert("请求失败");
        },
        dataType : "text"
    });
}

 

posted @ 2017-08-24 11:47  QiaoZhi  阅读(3500)  评论(0编辑  收藏  举报