一个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 Ex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fXb7kobuWXNIuRnVTc1AwAAwE8A/R5hqn53rwXQX/q/DhFb7jhmtF/p5V16hTwAAIBjAf0eYVIhdFvfR7/fR3kFoGRG+XlJLpdVdkMFAADws+xGv5sqT4qq24Ykrdpu/zGqyv0RZ7sRum1JeP+SrB7OcXcgdfKyzUnptZLFg4D73gP2lUBM2unWAiwe7uxEmwXOlzKKtny9J5dL+gU3HAAAjs2u9Pt8TqvW/kP2/WyvubTdpzGq/z2ob13wrcncAu8S7l5rjf2/2cZw9H36jXklcj5pG7s5m9x1tF/p5ZJ8wg0HAIADsyv9zquGbuZt9butUnfDUCbYcfHz4aO/X7i9urdZ+PIgPN+VnWwS54fZ6Yt5T8LGfKyQWdTZ5ZK8f2FlGwAAHJRD6HddeEHyxfrdqfWQ2F/Lk/bl+s3Xsln/29dgdoRsQ8ulWl68Ng78bwAAOD6H0W/HXWZs5H/PjJ8zJ3uQc0+DpVXr/mNg8/evrT8x/w0AAE/AIfSbz0mH8gYEXtTv4Pw3TernwtsqXbx+zXWpHSV2H+BWntXukm7qkRiw/hwAAJ6GY+i38ZeauypEU9n6c3tO4GO4tCFcn1/b9pov0u/SXSjOnOZSSCKRc2kX25OaGgDPfwMAwBOxG/1+UuYHugEAAAAZ6Pdj2eBFLgAAAP6HQL8fywnbhwAAAHgA/wF/nrzd1GFvqwAAAABJRU5ErkJggg==" alt="" />

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"
});
}
上一篇:Fiddler AutoResponder正则替换


下一篇:【转】解决maven无法加载本地lib/下的jar包问题(程序包XXX不存在)