package hello.servlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan //서블릿 직접등록후 사용하겠음.
@SpringBootApplication
public class ServeltApplication {
public static void main(String[] args) {
SpringApplication.run(ServeltApplication.class, args);
}
}
package hello.servlet.hello.basic;
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;
@WebServlet(name="helloServlet", urlPatterns = "/hello") //hello url로 들어오는 요청 처리
public class HelloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
System.out.println("HelloServlet.service");
System.out.println("req = " + req);
System.out.println("resp = " + resp);
String username = req.getParameter("username");
System.out.println("username = " + username);
}
}
url 입력
http://localhost:8080/hello?username=kim
* 응답도 찍어보기
package hello.servlet.hello.basic;
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;
@WebServlet(name="helloServlet", urlPatterns = "/hello") //hello url로 들어오는 요청 처리
public class HelloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
System.out.println("HelloServlet.service");
System.out.println("req = " + req);
System.out.println("resp = " + resp);
String username = req.getParameter("username");
System.out.println("username = " + username);
resp.setContentType("text/plain"); //응답으로 문자를 보낼거임
resp.setCharacterEncoding("utf-8");
resp.getWriter().write("hello " + username);
}
}
* servlet이 http 요청 파싱, 응답자동생성등 다해준다. (다 해줬잖아?)
* 터미널에서 http 로그 찍는법
* welcome html 추가
main > wepapp 경로에 index.html은 별도 설정없이 , spring이 자동 홈페이지로 설정한다.
'Java > Spring-MVC' 카테고리의 다른 글
Servlet 진행 도중 화이트 라벨 에러 해결 (0) | 2024.08.25 |
---|---|
Lombok 설정법 (0) | 2024.08.25 |
IntelliJ 빌드 속도 올리는법 (0) | 2024.08.25 |
Incompatible because this component declares an API of a component compatible with Java 17 and the consumer needed a runtime of a component compatible with Java 11 해결 (0) | 2024.08.25 |