이전 설정에서 새로운 프로젝트 생성 및 클래스 파일 생성
-> 별도의 Main 클래스가 없더라도 실행이 가능
public class ex01Servlet extends HttpServlet
-> 모든 Servlet 클래스는 HttpServlet 클래스의 상속을 받음
동적 페이지를 만들기 위해 제일 먼저 해야 하는 것
->Servlet 객체의 별칭 설정
※자바에서의 어노테이션('@')은 주석과 같이 의미의 서술이나 메모와 같은 기능을 가지고 있으나, 필요에 따라 Servlet 객체의 별칭의 설정이 가능하다.
@WebServlet("/별칭명")
- URLMapping : Servlet을 동작시키기 위해 문자열(별칭)과 연결(실제 Servlet 동작을 위한 명칭은 매우 길기 때문에 별칭을 사용)
- 별칭은 /문자열 로 설정
- 각 Servlet 별칭은 고유한 값이어야 함
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package ex0502; import java.io.IOException; import javax.security.auth.message.callback.PrivateKeyCallback.Request; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //404 -> Not Found, 주소를 잘못 입력한 경우 //405 -> get, post 방식을 틀렸을 경우 //500 //기능을 갖는 어노테이션 //문자열로 servlet 별명 설정 //Servlet 을 동작시키기 위해 지정해주는 별명 //Servlet의 별명값, context 값 또한 고유한 값을 가져야 한다 @WebServlet("/ex01") // 원래의 이름 : URLMapping public class ex01Servlet extends HttpServlet { // 기본 Servlet 구조 //sevlet 만드는 방법 //servlet을 실행할 때 필요한 main 메서드 @Override protected void service(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // http://localhost:8081/Servlet/ex01 // http:// : protocol // localhost : 서버의 ip 주소 // :8081 : 지정된 포트번호 // /Servlet : context path, 컴파일 된 파일이 위치 // ex01 : UrlMapping System.out.println("접속 성공!"); //request : 요청에 대한 정보를 담고있는 객체 //request 기본 메서드 //request.getRemoteAddr() : 접속한 컴퓨터의 ip 주소 리턴 String addr = requset.getRemoteAddr(); System.out.println(addr); } } | cs |
getRemoteAddr()
- 현재 웹 페이지에 접속한 기기/단말의 주소(ip)를 문자열로 반환하는 함수
- 사용자가 직접 본인 단말에 접속한 경우, 0:0:0:0:0:0:0:1 를 반환
+)url 분석
http://localhost:8081/Servlet/ex01
- http:// : protocol 형식
- localhost : 서버의 ip 주소
- :8081 : 지정된 포트번호
- /Servlet : context path, 컴파일 된 파일이 위치
- ex01 : UrlMapping
댓글
댓글 쓰기