javaEE Servlet开发框架

大家好! 这里分享一下javaEE Servlet开发框架!

1、首先是POST和GET入口以及接收处理文件

复制代码
package com.sl.imps;

import java.io.IOException;
import java.io.PrintWriter;

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 com.sl.dao.CountyDao;
import com.sl.func.Message;
import com.sl.func.Receive;

/**
 * Servlet implementation class County
 */
@WebServlet("/Post")
public class Post extends HttpServlet {

    private static final long serialVersionUID = 1L;

    // post传输数据的接收入口
    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException {
        req.setCharacterEncoding("UTF-8");
        res.setContentType("text/json");
        res.setCharacterEncoding("UTF-8");

        // 实例化输出obj
        PrintWriter out = res.getWriter();

        String json = null;// 返回

        // String cmd = Receive.getjson(req, "cmd");
        String cmd = Receive.getJsonByPost(req);

        // 获取列表
        if ("getlist".equals(cmd)) {
            CountyDao countyDao = new CountyDao();
            try {
                json = Message.back(0, "执行成功", countyDao.getStu());
            } catch (Exception e) {
                json = Message.back(1, "执行失败" + e.getMessage());
            }
        }

        json = json == null ? "JSON参数错误" : json;

        out.print(json);
        
        out.close();
        out.flush();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
        request.getParameter("");
    }
}
复制代码

GET:

复制代码
package com.sl.imps;

import java.io.IOException;
import java.io.PrintWriter;

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 com.sl.dao.CountyDao;
import com.sl.func.Message;
import com.sl.func.Receive;

/**
 * Servlet implementation class GETIN
 */
@WebServlet("/Get")
public class Get extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Get() {
        super();
    }

    // post传输数据的接收入口
    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException {
        req.setCharacterEncoding("UTF-8");
        res.setContentType("text/json");
        res.setCharacterEncoding("UTF-8");

        // 实例化输出obj
        PrintWriter out = res.getWriter();

        String json = null;// 返回

        String cmd = Receive.getJsonByGet(req, "cmd");

        // 获取列表
        if ("getlist".equals(cmd)) {
            CountyDao countyDao = new CountyDao();
            try {
                json = Message.back(0, "执行成功", countyDao.getStu());
            } catch (Exception e) {
                json = Message.back(1, "执行失败" + e.getMessage());
            }
        }

        json = json == null ? "JSON参数错误" : json;

        out.print(json);
        out.close();
        out.flush();
    }

    
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
复制代码

2、Receive类(接收传递的JSON)数据

复制代码
/*
 * Project Name:Solution-java-master 
 * File Name:Receive.java 
 * Package Name:com.sl.func 
 * Date:2017年3月24日下午2:55:12 
 * Copyright (c) 2017, chinhiyoshi@163.com 陈飞  Rights Reserved. 
 * 
 */
package com.sl.func;

import java.io.BufferedReader;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import com.google.gson.Gson;
import com.sl.model.Receivem;

/*
     * ClassName:Receive  
     * Function:   ADD FUNCTION.  
     * Reason:     ADD REASON.  
     * Date:     2017年3月24日 下午2:55:12  
     * @author   guosen 
     * @version  
     * @since    JDK 1.6   
     */
public class Receive {

    // 获取接收POST的json数据
    public static String postjson(HttpServletRequest req) throws IOException {

        BufferedReader reader = req.getReader();

        String recvjson = null;

        StringBuilder sb = new StringBuilder();

        while ((recvjson = reader.readLine()) != null)
            sb.append(recvjson);

        return sb.toString();
    }

    // 获取接收GET的传输参数值
    public static String getJsonByPost(HttpServletRequest req) throws IOException {

        BufferedReader reader = req.getReader();

        String recvjson = null;

        StringBuilder sb = new StringBuilder();

        while ((recvjson = reader.readLine()) != null)
            sb.append(recvjson);

        Receivem m = new Gson().fromJson(sb.toString(), Receivem.class);

        return m.cmd;
    }

    // 获取接收GET的传输参数值
    public static String getJsonByGet(HttpServletRequest req, String key) throws IOException {
        return req.getParameter(key);
    }
}
复制代码

3、DAO处理

复制代码
/*
 * Project Name:Solution 
 * File Name:CountyDao.java 
 * Package Name:com.sl.dao 
 * Date:2016年8月25日下午5:38:41 
 * Copyright (c) 2016, chinhiyoshi@163.com 陈飞  Rights Reserved. 
 * 
 */
package com.sl.dao;

import java.util.List;
import java.util.Map;
import com.sl.db.DBHelper;

/*
 * ClassName:CountyDao  
 * Function:   ADD FUNCTION.  
 * Reason:     ADD REASON.  
 * Date:     2016年8月25日 下午5:38:41  
 * @author   chinhiyoshi 
 * @version  V1.0
 * @info      
 */
public class CountyDao {
    // 查询国家数据
    public List<Map<String, Object>> getStu() {
        String sql = "SELECT * FROM  StudentXK ";
        return DBHelper.query(sql);
    }
}
复制代码

4、返回处理

复制代码
/*
 * Project Name:Solution-java-master 
 * File Name:Message.java 
 * Package Name:com.fun 
 * Date:2017年3月24日下午2:03:27 
 * Copyright (c) 2017, chinhiyoshi@163.com 陈飞  Rights Reserved. 
 * 
 */
package com.sl.func;

import java.util.List;
import java.util.Map;
import com.sl.model.JsonBack;

/*
 * ClassName:Message  
 * Function:   ADD FUNCTION.  
 * Reason:     ADD REASON.  
 * Date:     2017年3月24日 下午2:03:27  
 * @author   guosen 
 * @version   
 * @since    JDK 1.6   
 */

//再次封装消息返回格式
public class Message {
    // 成功
    public static String back(int code, String msg, List<Map<String, Object>> data) {
        return new JsonBack().BackJson(code, msg, data);
    }
    // 失败
    public static String back(int code, String msg) {
        return new JsonBack().BackJson(code, msg, null);
    }
}
复制代码

 

这里上一张项目架构图:

 

接下来用姑姑postman测试一下:

 

 

返回的JSON的如下:

复制代码
{
  "code": 0,
  "msg": "执行成功",
  "data": {
    "result": [
      {
        "ShangBaoRiQi": "Aug 6, 2017 12:00:00 AM",
        "Status": "1",
        "BeiZhu": 1,
        "CreatTime": "Aug 8, 2017 12:00:00 AM",
        "BanGongShiDianHua": 1,
        "WangDianDaiMa": 1,
        "XingMing": "name",
        "KaoShiChengJi": 1,
        "Name": "zhagsan",
        "KaoShiTongGuoRiQi": 1,
        "ID": "12545",
        "IDHaoMa": "1",
        "NiRenGangWei": "1",
        "XianRenGangWei": "1",
        "ShouJiHaoMa": 1
      }
    ]
  }
}
复制代码

觉得好用的就点击下载:             https://files.cnblogs.com/files/IBPM/javaee-solution.zip

 

posted @ 2017-09-22 16:52 araki 阅读(...) 评论(...) 编辑 收藏