728x90
반응형
[JSP] 11. Servlet을 사용하여 JSON 파싱하기
package com.tenco;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class TodoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TodoServlet() {
super();
}
// get 요청시 동작
// 주소 설계 : http://localhost:8080/d4/todo-servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("GET 요청 확인");
response.setContentType("text/plain");
String strJson=" {\r\n"
+ " \"userId\": 1,\r\n"
+ " \"id\": 1,\r\n"
+ " \"title\": \"delectus aut autem\",\r\n"
+ " \"completed\": false\r\n"
+ " }";
PrintWriter pw=response.getWriter();
pw.print(strJson);
}
// post 요청시 동작
// 주소 설계 : http://localhost:8080/d4/todo-servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.tenco;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class TodoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TodoServlet() {
super();
}
// get 요청시 동작
// 주소 설계 : http://localhost:8080/d4/todo-servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("GET 요청 확인");
Todo todo1=new Todo();
todo1.setId(100);
todo1.setTitle("오늘은 JSON 리턴하기 연습");
todo1.setCompleted(false);
response.setContentType("text/plain");
// String strJson=" {\r\n"
// + " \"userId\": 1,\r\n"
// + " \"id\": 1,\r\n"
// + " \"title\": \"delectus aut autem\",\r\n"
// + " \"completed\": false\r\n"
// + " }";
PrintWriter pw=response.getWriter();
pw.print(todo1.toString());
}
// post 요청시 동작
// 주소 설계 : http://localhost:8080/d4/todo-servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.tenco;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Todo {
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
private int id;
private String title;
private boolean completed;
}
package com.tenco;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class TodoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TodoServlet() {
super();
}
// get 요청시 동작
// 주소 설계 : http://localhost:8080/d4/todo-servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("GET 요청 확인");
Todo todo1=new Todo();
todo1.setId(100);
todo1.setTitle("오늘은 JSON 리턴하기 연습");
todo1.setCompleted(false);
response.setContentType("text/plain");
// String strJson=" {\r\n"
// + " \"userId\": 1,\r\n"
// + " \"id\": 1,\r\n"
// + " \"title\": \"delectus aut autem\",\r\n"
// + " \"completed\": false\r\n"
// + " }";
PrintWriter pw=response.getWriter();
pw.print(todo1.toString());
}
// post 요청시 동작
// 주소 설계 : http://localhost:8080/d4/todo-servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.tenco;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
public class Todo {
@Override
public String toString() {
return "{ \"id\" : "+id+" }";
}
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
private int id;
private String title;
private boolean completed;
}
728x90
반응형
'JSP > 기본 이론' 카테고리의 다른 글
[JSP] 13. 쿼리 스트링 (0) | 2024.07.03 |
---|---|
[JSP] 12. URI와 URL (0) | 2024.07.03 |
[JSP] 10. WAR 파일 생성 및 배포 (0) | 2024.07.03 |
[JSP] 9. 서블릿 작성, 배포, web.xml에 대한 이해 (1) | 2024.07.03 |
[JSP] 8. 서블릿 생애 주기 (0) | 2024.07.02 |