首页 > 其他 > 详细

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

时间:2017-08-24 11:58:42      阅读:259      评论:0      收藏:0      [点我收藏+]

处理的思路是在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"
    });
}

 

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

原文:http://www.cnblogs.com/qlqwjy/p/7422276.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!