2021-09-13 JSON+serlvet

serlvet中的代码

package com.entor.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.entor.entity.User;
import com.entor.service.UserService;
import com.entor.service.impl.UserServiceImpl;

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.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opType = request.getParameter("opType");
        switch (opType){
            case "login":
                login(request,response);
                break;
            case "logout":
                logout(request,response);
                break;
            case "add":
                add(request,response);
                break;
            case "addMore":
                addMore(request,response);
                break;
            case "deleteById":
                deleteById(request,response);
                break;
            case "deleteByIds":
                deleteByIds(request,response);
                break;
            case "update":
                update(request,response);
                break;
            case "queryById":
                queryById(request,response);
                break;
            case "queryByPage":
                queryByPage(request,response);
                break;
            default:
                queryByPage(request,response);
                break;
        }
    }
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserService userService = new UserServiceImpl();
        User user = userService.login(username, password);
        //返回数据给客户端
        JSONObject jo = new JSONObject();
        if(user!=null){
            jo.put("code",0);
            jo.put("msg","登录成功");
        }else{
            jo.put("code",-1);
            jo.put("msg","用户名或者密码错误");
        }
        String json = JSON.toJSONString(jo);
        System.out.println(json);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取客户端传递的参数值
        String name = request.getParameter("name");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String birthday = request.getParameter("birthday");
        //封装对象
        User user = new User();
        user.setName(name);
        user.setUsername(username);
        user.setPassword(password);
        user.setSex(Integer.parseInt(sex));
        user.setAge(Integer.parseInt(age));
        try {
            user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //调用业务层对象
        UserService userService = new UserServiceImpl();
        userService.add(user);

        //返回数据给客户端
        JSONObject jo = new JSONObject();
        jo.put("code",0);
        jo.put("msg","新增成功");
        String json = JSON.toJSONString(jo);
        System.out.println(json);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    public void addMore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        UserService userService = new UserServiceImpl();
        int num = userService.deleteById(Integer.parseInt(id));
        //返回数据给客户端
        JSONObject jo = new JSONObject();
        if(num>0){
            jo.put("code",0);
            jo.put("msg","删除成功");
        }else{
            jo.put("code",-1);
            jo.put("msg","删除失败");
        }
        String json = JSON.toJSONString(jo);
        System.out.println(json);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ids = request.getParameter("ids");
        UserService userService = new UserServiceImpl();
        int num = userService.deleteByIds(ids);
        //返回数据给客户端
        JSONObject jo = new JSONObject();
        if(num>0){
            jo.put("code",0);
            jo.put("msg","删除成功");
        }else{
            jo.put("code",-1);
            jo.put("msg","删除失败");
        }
        String json = JSON.toJSONString(jo);
        System.out.println(json);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取客户端传递的参数值
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String birthday = request.getParameter("birthday");
        //封装对象
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setName(name);
        user.setUsername(username);
        user.setPassword(password);
        user.setSex(Integer.parseInt(sex));
        user.setAge(Integer.parseInt(age));
        try {
            user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //调用业务层对象
        UserService userService = new UserServiceImpl();
        userService.update(user);

        //返回数据给客户端
        JSONObject jo = new JSONObject();
        jo.put("code",0);
        jo.put("msg","修改成功");
        String json = JSON.toJSONString(jo);
        System.out.println(json);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    public void queryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    public void queryByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String limit = request.getParameter("limit");
        String page = request.getParameter("page");
        UserService userService = new UserServiceImpl();
        List<User> list = userService.queryByPage(Integer.parseInt(page), Integer.parseInt(limit));
        //拼装客户端接收的数据格式
        JSONObject jo = new JSONObject();
        jo.put("code",0);
        jo.put("msg","");
        jo.put("count",userService.getTotal());
        jo.put("data",list);
        //把对象转换成json格式字符串
        String json = JSONObject.toJSONString(jo);
        System.out.println(json);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值