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

 请求的时候:

aaar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c1AwAAwE8A/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"
});
}

一个Servlet处理增删改查的方法的更多相关文章

  1. 最简单的jsp+servlet的增删改查代码

    package ceet.ac.cn.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.s ...

  2. Java连接MySQL数据库增删改查通用方法

    版权声明:本文为博主原创文章,未经博主允许不得转载. Java连接MySQL数据库增删改查通用方法 运行环境:eclipse+MySQL 以前我们Java连接MySQL数据库都是一个数据库写一个类,类 ...

  3. day84-仿照admin实现一个自定义的增删改查组件

    一.admin的使用 app01的admin.py文件: class BookConfig(admin.ModelAdmin): list_display=[] list_display_links= ...

  4. BitAdminCore框架应用篇:(二)创建一个简单的增删改查模块

    NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookie ...

  5. dbutils中实现数据的增删改查的方法,反射常用的方法,绝对路径的写法(杂记)

    jsp的三个指令为:page,include,taglib... 建立一个jsp文件,建立起绝对路径,使用时,其他jsp文件导入即可 导入方法:<%@ include file="/c ...

  6. Go Web --- 创建一个Article的增删改查

    掌握数据的增删改查之后,就可以做一些小demo,巩固一下基础,让语法更加熟练,所以下面是按照Go web编程里面的文章管理操作,写的一个代码: package main import ( " ...

  7. thinkPHP增删改查的方法案例

    thinkphp对数据库增删改查进行了封装操作,使得使用更加方便,但是不一定灵活. 可以用封装的用,需要写sql,可以执行sql. 1.原始的 $Model = new Model(); // 实例化 ...

  8. 【良心保姆级教程】java手把手教你用swing写一个学生的增删改查模块

    很多刚入门的同学,不清楚如何用java.swing去开发出一个系统? 不清楚如何使用java代码去操作数据库进行增删改查一些列操作,不清楚java代码和数据库(mysql.sqlserver)之间怎么 ...

  9. 使用Servlet编写增删改查

    第一步创建一个表 1 create database liyongzhendb default character set utf8 collate utf8_bin; 2 3 CREATE TABL ...

随机推荐

  1. 关于command 'gcc' failed with exit status 1 解决方法

    Python踩坑之路 Setup script exited with error: command 'gcc' failed with exit status 1 由于没有正确安装Python开发环 ...

  2. 利用vs10和opencv识别图片类型身份证的号码

    遇到的问题: 1 持续灰色图像框 waitkey()要在imshow()之前调用. 2 CvRect 和Rect CvXXX是C语言的接口,cv::XXX是C++语言的接口.两者混在一起容易出错 3 ...

  3. mysql(四)log

    慢查询: https://blog.csdn.net/leshami/article/details/39829605 日志组成: https://blog.csdn.net/leshami/arti ...

  4. 第147天:web前端开发中的各种居中总结

    一.水平居中 方法① :行内元素 (父元素)text-align,(子元素)inline-block .parent{text-align: center;} .child{display: inli ...

  5. MachineLearning ---- lesson 1

    该博文系列是Andrew NG教授的课程笔记,有兴趣的朋友可以在Coursera或者网易公开课上找到该课程. Supervised Learning 下图是一个监督学习回归分析的例子.该图旨在预测房价 ...

  6. Importing/Indexing database (MySQL or SQL Server) in Solr using Data Import Handler--转载

    原文地址:https://gist.github.com/maxivak/3e3ee1fca32f3949f052 Install Solr download and install Solr fro ...

  7. BZOJ 1965 洗牌(扩展欧几里得)

    容易发现,对于牌堆里第x张牌,在一次洗牌后会变成2*x%(n+1)的位置. 于是问题就变成了求x*2^m%(n+1)=L,x在[1,n]范围内的解. 显然可以用扩展欧几里得求出. # include ...

  8. 秒杀多线程第八篇 经典线程同步 信号量Semaphore (续)

    java semaphore实现: Semaphore当前在多线程环境下被扩放使用,操作系统的信号量是个很重要的概念,在进程控制方面都有应用.Java 并发库 的Semaphore 可以很轻松完成信号 ...

  9. BZOJ4890 Tjoi2017城市

    显然删掉的边肯定是直径上的边.考虑枚举删哪一条.然后考虑怎么连.显然新边应该满足其两端点在各自树中作为根能使树深度最小.只要线性求出这个东西就可以了,这与求树的重心的过程类似. #include< ...

  10. BZOJ5315 [JSOI2018]防御网络 【仙人掌 + dp】

    题目链接 BZOJ5315 题解 题目好吓人= =点仙人掌 + 斯坦纳树 我们只需求出对于所有选点的方案的斯坦纳树边长总和 \(n\)那么大当然不能状压,但是考虑一下如果这是一棵树,一个方案的贡献就是 ...