IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 2021-09-13 JSON+serlvet -> 正文阅读

[网络协议]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();
    }
}
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-09-14 13:44:27  更:2021-09-14 13:46:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 0:38:41-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码