[Spring] URL 매핑 : URL 패턴 Servlet URL Mapping / DispatcherServlet

2024. 2. 2. 01:35·Spring
728x90

 

 

 

 

1. URL 패턴

- URL 패턴 규칙을 생각하고 매핑 설정을 해야, 예상한 Servlet / Controller으로 요청할 수 있다

- Servlet과 Spring 모두 아래 URL 패턴 규칙을 사용 가능하다 (1. 2. ... : 우선순위)

종류 URL pattern 매칭 URL
1. exact mapping /test/hello.do http://localhost/syun/test/hello.do
2. path mapping /test/* http://localhost/syun/test/
http://localhost/syun/test/hello
http://localhost/syun/test/hello.do
http://localhost/syun/test/cat
3. extension mapping *.do http://localhost/syun/hi.do
http://localhost/syun/test/hello.do
4. default mapping / http://localhost/syun/
http://localhost/syun/hello.do
http://localhost/syun/test/
http://localhost/syun/test/hello
http://localhost/syun/test/hello.do

 

 

 

 

 

2. 서블릿(Servlet)의 URL 매핑

 

2.1. 서블릿의 URL 매핑 방법

1. @WebServlet("/URL") 어노테이션을 사용한 매핑

@WebServlet("/ClassName")
public class ClassName extends HttpServlet {

- 다음과 같이 하나의 서블릿에 여러 URL 주소를 매핑할 수 있다

@WebServlet(urlPatterns = {"/main", "/test1", "/test2"})
public class Controller extends HttpServlet {

- 다음과 같이, 서블릿은 기본적으로 lazy-init이지만 early-init으로 설정 가능하다

    •  =1  : early-init 하는 서블릿 중 우선순위 설정 가능

@WebServlet("/test", loadOnStartup=1)
public class Controller extends HttpServlet {

 

2. web.xml을 통한 매핑

	<servlet>
		<servlet-name>Name</servlet-name>
		<servlet-class>com.java.example.ClassName</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>Name</servlet-name>
		<url-pattern>main</url-pattern>
	</servlet-mapping>

 

 

2.2. 서블릿 URL 매핑 과정

1. 요청과 연결된 서블릿을 찾기 위해 servletMappings 확인

    - servletMappings : URL 패턴에 따른 서블릿 이름 매핑

2. 매핑된 서블릿 이름을 children(서블릿)에서 찾아 서블릿이 처리할 수 있도록 한다

    - children : 서블릿 이름에 해당하는 서블릿 매핑

 

 

 

 

 

 

3. 스프링(Spring)의 URL 매핑

- 스프링에서는 모든 요청을 "/" 즉, Default Servlet으로 처리

- Default Servlet 대신 DispatcherServlet을 연결

- DispatcherServlet에서 요청에 해당하는 URL을 해당 Controller로 매핑한다

 

3.1. Handler Mapping

- 요청에 대해 어떤 Controller가 처리해야 하는지 결정

- DispatcherServlet은 등록된 HandlerMapping을 참조하여 요청을 적절한 Controller로 전달

 

 

3.2. 어노테이션 : 컨트롤러 등록과 URL 매핑

- @Controller : 컨트롤러를 등록하는 어노테이션

- @RequestMapping() : 요청 주소와 Controller를 매핑하는 어노테이션 (GET, POST 둘 다 가능)

- @GetMapping() : GET 요청의 주소와 Controller를 매핑하는 어노테이션

- @PostMapping() : POST 요청의 주소와 Controller를 매핑하는 어노테이션

@Controller
@RequestMapping("/test")
public Class TestController {
	@RequestMapping("/hello")    // "/test/hello"
	public String hello() {

 

 

 

 

 

 

 

 

 

📌  References

- https://codevang.tistory.com/194

 

JSP, Servlet(서블릿)의 URL 주소 매핑

[ Servlet - URL 매핑 ] 서블릿의 @WebServlet("/URL") 어노테이션을 사용한 매핑 web.xml을 통한 매핑 서블릿은 위의 두 가지 방법으로 URL 매핑이 가능합니다. 먼저 첫번 째 방법은 이클립스에서 서블릿 파

codevang.tistory.com

- https://sharonprogress.tistory.com/196

 

Handler Mapping과 Request Mapping이란?

HANDLER MAPPING, REQUEST MAPPING 모두 웹 애플리케이션에서 요청을 처리하는데 사용되는 개념이다. 이 두 개념은 Spring MVC에서 주로 사용되는 개념으로 웹 애플리케이션에서 클라이언트의 요청을 처리

sharonprogress.tistory.com

 

 

 

 

 

728x90
저작자표시 비영리 (새창열림)

'Spring' 카테고리의 다른 글

[Spring] 빌드 관리 도구 Maven vs Gradle  (0) 2025.06.04
[Spring] JSP와 Spring에서의 Forward와 Redirect : InternalResourceView / RedirectView  (0) 2024.02.02
[Spring] MVC 패턴이란? Model / View / Controller / 메인 메서드 반환타입  (1) 2024.01.31
[Spring] HttpServletRequest, HttpServletResponse 객체  (1) 2024.01.31
[Spring] 웹 개발을 하는 세 가지 방법(3) : API  (0) 2022.02.08
'Spring' 카테고리의 다른 글
  • [Spring] 빌드 관리 도구 Maven vs Gradle
  • [Spring] JSP와 Spring에서의 Forward와 Redirect : InternalResourceView / RedirectView
  • [Spring] MVC 패턴이란? Model / View / Controller / 메인 메서드 반환타입
  • [Spring] HttpServletRequest, HttpServletResponse 객체
s_ih_yun
s_ih_yun
  • s_ih_yun
    CODESYUN
    s_ih_yun
  • 전체
    오늘
    어제
    • 분류 전체보기 (339)
      • Web (8)
      • Java (7)
      • Spring (19)
      • Git (16)
      • MyBatis (1)
      • HTML & CSS (14)
      • JavaScript (11)
      • DevOps (4)
      • Cloud (8)
      • Lanuage (13)
        • C++ (3)
        • Python (10)
      • DB (1)
        • MySQL (1)
        • Oracle (2)
      • Computer Science (26)
        • Concept (3)
        • Algorithm (23)
      • Baekjoon (104)
        • 단계별로 풀어보기 (78)
      • CodeUp (98)
        • Python 기초 100제 (98)
      • Programmers (2)
      • Books (3)
      • etc (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

    • Syun's Pages
  • 인기 글

  • 태그

    Cloud
    codeup
    SourceTree
    myBatis
    aws
    CodeUp 기초 100제
    C
    Programmers
    c++
    MySQL
    web
    단계별로 풀어보기
    자료구조
    Tistory
    VS Code
    spring
    github
    BOJ
    db
    Python
    JavaScript
    oracle
    clean code
    HTML
    CSS
    git
    웹
    java
    알고리즘
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
s_ih_yun
[Spring] URL 매핑 : URL 패턴 Servlet URL Mapping / DispatcherServlet
상단으로

티스토리툴바