ApiDemo记录

1、只有查询

package webhandle;

import com.fasterxml.jackson.databind.ObjectMapper;
import utils.DbUtils;

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 java.io.BufferedReader;
import java.io.IOException;

/**
 * Description :
 *
 * @author : HMF
 * Date : Created in 16:38 2023/5/5
 * @version :
 */

class Param {
    public String sql;
}
@WebServlet("/api")
public class ApiDemo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hello World!");
    }
        @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String body=readBody(req);
        //创建ObjectMapper对象,这个是Jackson中的核心类,
        ObjectMapper objectMapper = new ObjectMapper();
        //通过readvalue方法把body这个字符串转成JsonData对象
        Param jsonData = objectMapper.readValue(body,Param.class);
        //System.out.println(jsonData.sql);
        String selSql=jsonData.sql;
        DbUtils sd=new DbUtils();
        String selectResult=sd.execSelect(selSql);
        //返回结果数据
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(selectResult);
    }
    private String readBody(HttpServletRequest req) throws IOException {
        if(req.getMethod().equals("POST")){
            StringBuilder result = new StringBuilder();
            BufferedReader br = req.getReader();
            String line;
            while((line = br.readLine()) != null){
                result.append(line);
            }
            br.close();
            //System.out.println(result.toString());
            return result.toString();
        }
        return null;
    }

}

2、插入和查询

package webhandle;

import com.fasterxml.jackson.databind.ObjectMapper;
import utils.DbUtils;

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 java.io.BufferedReader;
import java.io.IOException;

/**
 * Description :
 *
 * @author : HMF
 * Date : Created in 16:38 2023/5/5
 * @version :
 */

class Param {
    public String sql;
}
@WebServlet("/api")
public class ApiDemo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hello World!");
    }
        @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String body=readBody(req);
        //创建ObjectMapper对象,这个是Jackson中的核心类,
        ObjectMapper objectMapper = new ObjectMapper();
        //通过readvalue方法把body这个字符串转成JsonData对象
        Param jsonData = objectMapper.readValue(body,Param.class);
        //System.out.println(jsonData.sql);
        String excSql=jsonData.sql;
        DbUtils db=new DbUtils();
        String excResult="";
        if(excSql.contains("select")) {
            excResult = db.execSelect(excSql);
        }else{
            int d=db.execOther(excSql);
            if (d==1) {
                excResult ="插入或更新成功";
            }else{
                excResult ="插入或更新失败,请检查";
            }
        }
        //返回结果数据
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(excResult);
    }
    private String readBody(HttpServletRequest req) throws IOException {
        if(req.getMethod().equals("POST")){
            StringBuilder result = new StringBuilder();
            BufferedReader br = req.getReader();
            String line;
            while((line = br.readLine()) != null){
                result.append(line);
            }
            br.close();
            //System.out.println(result.toString());
            return result.toString();
        }
        return null;
    }

}

你可能感兴趣的:(#,Java,Web,java)