jetty

항상 정리하기·2022년 6월 11일
0
public class Main {

	static final Logger log = Logger.getGlobal();

	public static void main(String[] args) throws Exception {
		log.setLevel(Level.INFO);

		HttpServer server = new HttpServer();
		server.start();
	}
}

public class HttpServer { 
	private Server server;
	public void start() {
		server = new Server();
		ServerConnector http = new ServerConnector(server);
		http.setHost("127.0.0.1");
		http.setPort(8080);
		server.addConnector(http);

		ServletHandler servletHandler = new ServletHandler();
		servletHandler.addServletWithMapping(MyServlet.class, "/*");
		server.setHandler(servletHandler);

		try {
			server.start();
			server.join();
		} catch (Exception e) {
		}
	}	
	
	public void stop() {
		try {
			server.stop();
		} catch (Exception e) {
		}
	}
}


public class MyServlet extends HttpServlet {

    static final Logger log = Logger.getGlobal();

    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        String path = req.getPathInfo().toString();
        String [] paths = path.split("/");
        log.info("paths: " + String.join(", ", paths));

        switch(paths[0]) {
            case "cmd1":
                break;
            case "cmd2":
                break;
            default:
                break;
        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        String path = req.getPathInfo().toString();
        String [] paths = path.split("/");
        log.info("paths: " + String.join(", ", paths));

        switch(paths[0]) {
            case "cmd1":
                Class body = getBody(req, cls);
                break;
            case "cmd2":
                break;
            default:
                break;
        }
    }

    public <T> T getBody(HttpServletRequest req, Class<T> cls) throws IOException {
        return new Gson().fromJson(getBody(req), cls);
    }

    public String getBody(HttpServletRequest req) throws IOException {

        String body = null;
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;

        try {
            InputStream inputStream = req.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            }
        } catch (IOException ex) {
            throw ex;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }
        }

        body = stringBuilder.toString();
        return body;
    }

}

참고: https://blog.naver.com/PostView.naver?blogId=ambidext&logNo=222434508009&categoryNo=0&parentCategoryNo=10&viewDate=¤tPage=1&postListTopCurrentPage=1&from=search

profile
늦은 것 같지만 이제부터라도 차근차근 하나씩 정리하기

0개의 댓글