728x90
반응형
[JSP] 7. 서블릿을 사용하여 GET/POST 방식으로 데이터 주고 받기
package com.tenco;
import jakarta.servlet.ServletConfig;
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;
// HttpServlet 클래스를 상속 받아서 내가 정의한 클래스가 선언이 된다.
// Why? HTTP 프로토콜을 통한 request, response 처리를 할 수 있기 때문.
/*
* URL 맵핑에 대한 개념을 알자.
* 클라이언트가 특정 URL을 요청했을 때,
* 해당 URL에 대응하는 서블릿을 실행하도록 설정하는 것을 의미한다.
* URL 맵핑 - webserver 어노테이션 / web.xml
*/
//@WebServlet("/hello-servlet2")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 생성자
public HelloServlet() {
super();
}
// init() : 해당 서블릿 클래스가 인스턴스화 될 때, 단 한 번 실행되는 메서드
public void init(ServletConfig config) throws ServletException {
}
// destroy() : 메모리에서 내려가기 직전에 호출되는 메서드이다.
public void destroy() {
}
// doGet() : Get 요청으로 들어올 때 실행되는 메서드이다.
// 주소 설계 - http://localhost:8080/hello/hello-servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// MIME TYPE - 인터넷 세상에서 데이터의 유형을 나타내는 표준 방식
// 세상에 존재하는 프로그래밍 언어들은 다 다른 데이터 유형을 지니고 있다!
// 그래서 생긴 표준 약속이 MIME TYPE이다!!
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
// 스트림을 어디에서 뽑아야 할까?
response.getWriter().write("<html><body><h1>히힛 오늘 점심 뭐먹지ㅋ</h1></body></html>");
}
// doPost() : Post 요청으로 들어올 때 실행되는 메서드이다.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 문제 : post 요청이 들어오는 것을 확인하고,
// 응용해서 데이터 또는 html 형식으로 응답 처리 하시오!!!
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
// 스트림을 어디에서 뽑아야 할까?
response.getWriter().write("<html><body><h1>버거킹 vs 한솥 vs 밀면</h1></body></html>");
}
}
package com.tenco;
import jakarta.servlet.ServletConfig;
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;
// HttpServlet 클래스를 상속 받아서 내가 정의한 클래스가 선언이 된다.
// Why? HTTP 프로토콜을 통한 request, response 처리를 할 수 있기 때문.
/*
* URL 맵핑에 대한 개념을 알자.
* 클라이언트가 특정 URL을 요청했을 때,
* 해당 URL에 대응하는 서블릿을 실행하도록 설정하는 것을 의미한다.
* URL 맵핑 - webserver 어노테이션 / web.xml
*/
//@WebServlet("/hello-servlet2")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 생성자
public HelloServlet() {
super();
System.out.println("생성자 호출!");
}
// init() : 해당 서블릿 클래스가 인스턴스화 될 때, 단 한 번 실행되는 메서드
public void init(ServletConfig config) throws ServletException {
System.out.println("이닛이닛");
}
// destroy() : 메모리에서 내려가기 직전에 호출되는 메서드이다.
public void destroy() {
}
// doGet() : Get 요청으로 들어올 때 실행되는 메서드이다.
// 주소 설계 - http://localhost:8080/hello/hello-servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// MIME TYPE - 인터넷 세상에서 데이터의 유형을 나타내는 표준 방식
// 세상에 존재하는 프로그래밍 언어들은 다 다른 데이터 유형을 지니고 있다!
// 그래서 생긴 표준 약속이 MIME TYPE이다!!
response.setContentType("application/pdf");
response.setCharacterEncoding("UTF-8");
// 스트림을 어디에서 뽑아야 할까?
response.getWriter().write("<html><body><h1>히힛 오늘 점심 뭐먹지ㅋ</h1></body></html>");
}
// doPost() : Post 요청으로 들어올 때 실행되는 메서드이다.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 문제 : post 요청이 들어오는 것을 확인하고,
// 응용해서 데이터 또는 html 형식으로 응답 처리 하시오!!!
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
// 스트림을 어디에서 뽑아야 할까?
response.getWriter().write("<html><body><h1>버거킹 vs 한솥 vs 밀면</h1></body></html>");
}
}
728x90
반응형
'JSP' 카테고리의 다른 글
[JSP] 9. 서블릿 작성, 배포, web.xml에 대한 이해 (1) | 2024.07.03 |
---|---|
[JSP] 8. 서블릿 생애 주기 (0) | 2024.07.02 |
[JSP] 6. 서블릿 만들기 (0) | 2024.07.01 |
[JSP] 5. 서블릿이란? (0) | 2024.07.01 |
[JSP] 4. 서블릿과 JSP 파일 만들어 보기 (0) | 2024.06.30 |