Servlet增删改查案例经典实现
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Add
public class Add extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//RequestDispatcher dp=request.getRequestDispatcher("/Add/selectAll");//转发方式
//dp.forward(request, response);
}
}
public class DelAll extends HttpServlet {
}
// Post方法处理post的请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
String addr = request.getParameter("address");
int userid = Integer.parseInt(id);
//调用dao层的方法进行添加到数据库的操作
Person p = new Person();
p.setId(userid);
p.se源自文库Name(name);
throws ServletException, IOException {
doGet(request, response);
}
}
Update
public class UpdateHtml extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//编码问题
response.setContentType("text/html;charset=GBK");//浏览器显示的编码,默认的是iso
response.setCharacterEncoding("GBK");//浏览器到数据库取数据时候使用的编码
IPersonDao person=new PersonDaoImpl();
int id=Integer.parseInt(request.getParameter("id").trim());
person.delPersonById(id);
response.sendRedirect("/Jsp/selectAll");//重定向查询全部页面
return;
}
String name = request.getParameter("user");
// String name=new String(name1.getBytes(("GBK")));//"ISO-8859-1",
int userage = Integer.parseInt(request.getParameter("age"));
}
}
Delete
public class Delete extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
IPersonDao person = new PersonDaoImpl();
//使用for循环进行删除操作
for (int i = 0; i < s.length; i++) {
// System.out.println(s[i]);
int id = Integer.parseInt(s[i]);
//必须为post方式才行的
//获取所提交表单的值
String id = request.getParameter("id");
//严谨的编码:
if ("".equals(id) || id == null) {
response.sendRedirect("./Main.html");
// System.out.println("zzz");
//循环删除操作
person.delPersonById(id);
}
response.sendRedirect("/Add/selectAll");//跳到查询全部页面
}
// Post方法处理post的请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取多个box的值的方法
String[] s = request.getParameterValues("like");// ?
p.setAge(userage);
p.setAddress(addr);
IPersonDao person = new PersonDaoImpl();
person.addPerson(p);
//添加过后则重定向,跳向查询全部的页面
response.sendRedirect("/Jsp/selectAll");
//编码问题
response.setContentType("text/html;charset=GBK");//浏览器显示的编码,默认的是iso
response.setCharacterEncoding("GBK");//浏览器到数据库取数据时候使用的编码
request.setCharacterEncoding("GBK");//浏览器到数据库存入数据时候所使用的编码方式
public class Add extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//RequestDispatcher dp=request.getRequestDispatcher("/Add/selectAll");//转发方式
//dp.forward(request, response);
}
}
public class DelAll extends HttpServlet {
}
// Post方法处理post的请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
String addr = request.getParameter("address");
int userid = Integer.parseInt(id);
//调用dao层的方法进行添加到数据库的操作
Person p = new Person();
p.setId(userid);
p.se源自文库Name(name);
throws ServletException, IOException {
doGet(request, response);
}
}
Update
public class UpdateHtml extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//编码问题
response.setContentType("text/html;charset=GBK");//浏览器显示的编码,默认的是iso
response.setCharacterEncoding("GBK");//浏览器到数据库取数据时候使用的编码
IPersonDao person=new PersonDaoImpl();
int id=Integer.parseInt(request.getParameter("id").trim());
person.delPersonById(id);
response.sendRedirect("/Jsp/selectAll");//重定向查询全部页面
return;
}
String name = request.getParameter("user");
// String name=new String(name1.getBytes(("GBK")));//"ISO-8859-1",
int userage = Integer.parseInt(request.getParameter("age"));
}
}
Delete
public class Delete extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
IPersonDao person = new PersonDaoImpl();
//使用for循环进行删除操作
for (int i = 0; i < s.length; i++) {
// System.out.println(s[i]);
int id = Integer.parseInt(s[i]);
//必须为post方式才行的
//获取所提交表单的值
String id = request.getParameter("id");
//严谨的编码:
if ("".equals(id) || id == null) {
response.sendRedirect("./Main.html");
// System.out.println("zzz");
//循环删除操作
person.delPersonById(id);
}
response.sendRedirect("/Add/selectAll");//跳到查询全部页面
}
// Post方法处理post的请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取多个box的值的方法
String[] s = request.getParameterValues("like");// ?
p.setAge(userage);
p.setAddress(addr);
IPersonDao person = new PersonDaoImpl();
person.addPerson(p);
//添加过后则重定向,跳向查询全部的页面
response.sendRedirect("/Jsp/selectAll");
//编码问题
response.setContentType("text/html;charset=GBK");//浏览器显示的编码,默认的是iso
response.setCharacterEncoding("GBK");//浏览器到数据库取数据时候使用的编码
request.setCharacterEncoding("GBK");//浏览器到数据库存入数据时候所使用的编码方式