Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
more
Archives
Today
Total
관리 메뉴

Hello, Dino

Servlet 생성부터 Servlet Mapping까지 🏃‍♀️ 본문

Web/JSP

Servlet 생성부터 Servlet Mapping까지 🏃‍♀️

초보디노 2020. 1. 10. 00:00

안녕하세요.

지난 시간에는 JSP 생성과 서버로 실행하는 것을 해보았습니다.

이번 시간에는 Servlet을 생성해보고 Mapping 하는 방법까지 정리해보도록 하겠습니다!

 

 

Dynamic Web Project 생성 후 프로젝트 우클릭하여 Servlet를 생성해보겠습니다.

 

 

 

 

Servlet 생성화면입니다. Class 생성화면과는 많이 다른 것 같습니다. 

 

 

[첫 번째 사진]

Java package : Servlet 클래스 파일이 생성될 package 명

Class name   : Servlet 클래스 명

 

[두 번째 사진]

Name : Servlet 명

URL mappings : Servlet 호출을 위해 사용될 주소(문자)

 

[세 번째 사진]

Servlet 관련된 method입니다.

 

 

 

Servlet이 정상적으로 생성되었습니다.  👏👏

 

 

 

 

정말 간단한 것 같습니다.

 

 

서버를 실행하여 Servlet을 호출해보도록 하겠습니다.

 

 

Servlet Mapping을 통해 ServletReview Servlet에 'ServletReview'라는 url을 mapping 하여

'localhost:8090/ServletReview/ServletReview'를 주소창에 입력해 Servlet를 호출할 수 있었습니다.

 

마지막으로 Servlet mapping 방법에 대해 알아보도록 하겠습니다.

 

Servlet을 mapping 하는 방법은 두 가지가 있습니다.

  • Web.xml을 이용한 mapping
  • Annotation을 이용한 mapping

 

 

먼저 Web.xml을 이용한 mapping 방법입니다.

 

 

<servlet> ~</servlet> : servlet name과 servlet 클래스 파일의 위치를 등록하여 servlet 정보를 생성해줍니다.

<servlet-mapping> ~ <servlet-mapping> : 생성한 servlet정보에 url pattern을 등록하여 mapping을 해줍니다.

 

'http://localhost:8090/Context Root/Home' url을 웹브라우저 주소창에 입력했을 때

'com.servletProject.HelloServlet.java'에 있는 'servletMapping' servlet이 호출됩니다.

 

 

Annotation을 이용한 mapping 방법입니다.

 

 

Servlet class 상단에 '@WebServlet("url pattern")' Annotation을 작성해주면 끝입니다.

첫 번째 방법을 이용한 mapping과 동일하게 

'http://localhost:8090/Context Root/Home' url을 웹브라우저 주소창에 입력했을 때

'com.servletProject.HelloServlet.java'에 있는 'servletMapping' servlet이 호출됩니다.

 

 

왜 Servlet mapping을 사용하는 걸까요?

http://localhost:8090/ServletProject/com.servletProject.HelloServlet

[Full Path]

  • Servlet의 경로가 적나라하게 노출되기 때문에 보안에 취약합니다
  • 복잡한 URL 형태입니다

http://localhost:8090/ServletProject/Home

[Servlet]

  • 간결한 URL 형태입니다

 

 

 

 

오늘은 Servlet 생성과 Mapping 방법에 대해 알아보았습니다 😁

 

'Web > JSP' 카테고리의 다른 글

Page 이동 방식 (Forward / Redirect)  (0) 2020.03.10
한글 처리 (encoding)  (0) 2020.03.10
Cookie / Session  (0) 2020.03.09
JSP 개발 환경 설정  (0) 2020.01.07