Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- JSON형식의 response
- 김영한
- 피그마
- while문
- Dispatcher
- 한글깨짐
- https://www.inflearn.com/course/lecture?courseslug=%ea%b9%80%ec%98%81%ed%95%9c%ec%9d%98-%ec%8b%a4%ec%a0%84-%ec%9e%90%eb%b0%94-%ea%b8%b0%eb%b3%b8%ed%8e%b8&unitid=194709&category=questiondetail&tab=community&q=1314387
- 매핑 #
- 톰캣
- GET방식
- Spring MVC
- Forwarding
- 요구사항정의서
- WAS
- sendRedirect
- webserver #WAS #ServerApp
- 화면정의서
- Request
- https://www.inflearn.com/course/lecture?courseslug=%ea%b9%80%ec%98%81%ed%95%9c%ec%9d%98-%ec%8b%a4%ec%a0%84-%ec%9e%90%eb%b0%94-%ea%b8%b0%eb%b3%b8%ed%8e%b8&unitid=194690
- https://www.inflearn.com/course/lecture?courseslug=%ea%b9%80%ec%98%81%ed%95%9c%ec%9d%98-%ec%8b%a4%ec%a0%84-%ec%9e%90%eb%b0%94-%ea%b8%b0%eb%b3%b8%ed%8e%b8&unitid=194711
- xml
- Servlet
- Break
- 다형성 #부모타입 #자식타입
- CONTINUE
- POST방식
Archives
- Today
- Total
Step it up now
req/res1) 클라이언트 - 서블릿 본문
클라이언트의 요청에 서블릿이 응답하는 코드
GET 요청에 JSON 형식의 데이터로 응답하여 부서 정보를 나타낸다
//서블릿을 불러들이는 url 패턴
@WebServlet("/mime/json.do")
public class JSonServlet extends HttpServlet {
Logger logger = LoggerFactory.getLogger(JSonServlet.class);
@Override
//지정된 URL 패턴 ("/mime/json.do")으로 GET 요청이 발생할 때 실행
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//응답 콘텐츠 타입을 "application/json"으로 설정하며, 응답이 JSON 형식임을 나
res.setContentType("application/json");//JSON포맷- 마임타임 -> 데이터셋 역할 -> ReactJS와 같은 UI솔루션이나 라이브러리 비벼진다
PrintWriter out = res.getWriter();
//JSON 데이터 생성
List<Map<String,Object>> list = new ArrayList<>();
//데이터 채우기
Map<String,Object>rmap = new HashMap<>();//SELECT deptno, dname, loc FROM dept
rmap.put("deptno", 10);
rmap.put("dname","총무부");
rmap.put("local","서울");
list.add(rmap);
rmap=new HashMap<>();
rmap.put("deptno", 20);
rmap.put("dname","개발부");
rmap.put("local","인천");
list.add(rmap);
rmap=new HashMap<>();
rmap.put("deptno", 30);
rmap.put("dname","운영부");
rmap.put("local","서울");
list.add(rmap);
//Front -End와 Back-End - 만나는 부분에 대한 처리이다(mime type역할이 있다)
//서블릿이라고 쓰고 JSON이라고 읽는다
//Gson 클래스의 인스턴스가 생성됨
//맵의 리스트가 toJson 메서드를 사용하여 JSON 형식의 문자열로 변환된다.
//마지막으로 이 JSON 문자열이 PrintWriter(out)을 사용-> 클라이언트로 전송된다.
Gson g = new Gson();
String temp = g.toJson(list);
out.print(temp);
}
}
'수업 > servlet, JSP' 카테고리의 다른 글
Front Controller (1) | 2023.12.05 |
---|---|
req/res3) forwarding, request (1) | 2023.12.05 |
req/res2) Redirect (1) | 2023.12.05 |
유효범위 (2) | 2023.12.05 |
url 패턴 (1) | 2023.12.05 |