当前位置 : 主页 > 编程语言 > c++ >

ajax 实现静态页面分页,增删改查servlet.java

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt package servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.s
gistfile1.txt
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import com.alibaba.fastjson.JSONObject;

import until.DbUtil_IDUS;

@WebServlet("/ajax.do")
public class Ajax extends HttpServlet{

	@Override
	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		//中文乱码
		req.setCharacterEncoding("utf-8");
		res.setContentType("text/html;charset=utf-8");
		PrintWriter out = res.getWriter();
		
		//数据操作对象
		DbUtil_IDUS dbs = new DbUtil_IDUS();
		//当前页
		int currpage = req.getParameter("p")==null ? 1 : Integer.parseInt(req.getParameter("p")); 
		dbs.setPagesize(3);
		List
 
  > list = dbs.page(currpage, "user");
		Object[] objects = new Object[]{list,dbs.pageinfo()};

		String id = req.getParameter("did");
		String name = req.getParameter("name");
		String email = req.getParameter("email");
		String sex = req.getParameter("sex");
		String caoz = req .getParameter("caoz");
		/*System.out.println(name+":"+email+":"+sex);
		System.out.println(currpage);*/
		if (caoz != null) {
			if (caoz.equals("del")){
				int b = dbs.deleteById("user", id);
				out.print(b);
				return;
			}else if (caoz.equals("update")) {
				Map
  
    map = new HashMap<>(); map.put("id", id); map.put("username", name); map.put("email", email); map.put("sex", sex); int b = dbs.update("user", map); out.print(b); return; }else if (caoz.equals("sel")){ Map
   
     map = dbs.queryById("user", id); out.print(JSONObject.toJSON(map)); return; }else if (caoz.equals("insert")){ Object[] ob = new Object[]{name, email, sex}; String sql = "insert into user (username, password, email, sex) values (?, \"111111\",?,?)"; int b = dbs.add(sql, ob); out.print(b); return; } } //转化json out.print(JSONObject.toJSON(objects)); out.flush(); out.close(); } }
   
  
 
网友评论