servlet
j2ee 의 표준 스펙중 하나
servlet, jsp,
servlet : generates dynamic content
java 를 사용하여 동적 웹페이지를 생성하는 표준 스펙
HttpServlet 클래스를 상속하여 작성
Web Application server : was (jboss , IBM 웹스피어)
: Servlet Container + EJB Container
- tomcat 에는 tomcat 에서 담지 않아.
servlet container[Web Container] ( tomcat)
Servlet-container
The servlet container is a part of a web server
mime
: Multipurpose internet mail Extensions
전자 우편을 위한 인터넷 표준 포멧
메일을 전송하기 위한 표준 규약 : smtp 라는 프로토콜을 사용한다
: smtp 에서는 기본적으로 7비트 기준 아스키코드만 전송 가등
영어가 아닌 외국어, 특수기호, 이미지 파일 전송위해 인코딩이 필요 → mime
이것을 http 에서도 차용했다.
요청을 네이버로 요청 →
html 코드를 해석 → 이미지태그, css 등을 만나면 → 한번더 다시 요청
content - type : 응답이나 요청이 어떤 자료인지 알려주는 부가 정보
: ex) applicatuon/javascript ⇒ 웹 표준으로 정해져 있다.
어떤 형식의 ㅏ료를 송/수신 하는지 표시
mime 의 종류를 표시
servlet 생성/ 등록
클래스 생성 (HttpServlet 상속)
url mapping
url-servlet
xml
anotation
- webApplication
- WEB-INF - XML WEB.XML , LIB 폴더 → .JAR , CLASSES → class
프로젝트 관리를 Maven Project 로 관리할 것이다.
프로젝트 템플릿을 선택해서 만들 수 있지만, 기본 설정을 먼저 해볼 것이다.
Maven Project
Group id : 해당 프로젝트를 진행하는 회사에 대한 정보
일반적으로 회사의 도메인 역순으로 기술
[naver.com](<http://naver.com>) → com.naver
[google.com](<http://google.com>) → [com.google](<http://com.google>)
ddit.or.kr → kr.or.ddit
Artifact id : Group id에 해당하는 회사에서 진행하는 프로젝트명
google : Gmail , google drive
Version : 프로젝트의 버전
대.중.소 3.5.2
jar : java archive file : 클래스들을 묶어 놓은 파일, 압축파일
class, html , jsp , image, css, javascript
war : web Archive : 웹 프로그램을 묶어 놓은 파일
tomcat 의 webapps 폴더에 war 파일을 복사 해놓고 톰캣을 기동하면
war 파일을 톰캣이 자동으로 압축을 해제하여 웹 어플리케이션이 서비스 되도록 한다.
src/test / 는 배포할떄는 배포가 안되 그래서 4개 만들어져 있다.
기준이 되는 폴더 : src /main/webapp 폴더
사진에서 봤던 webproject
dynamic web module 3.0 으로 변경 java를 1.8 로 변경
maven project 가 만들어지면
web-inf web.xml 만들기 : java ee tools 눌러서 → generate development stub
project 우클릭 properties 에서 text 형식 utf-8 로 변경해주기
open type → string → string lang → external type (?? 코드로 그냥 넣어갈수있다. )
alt shift j
→ 컴퓨터 이름 나옴
SERVLET life cycle
- init() 초기화
The servlet is initialized by calling the init() method.
서블릿은 클라이언트가 요청해준후 바로 호출이 되는 것이 아니라 객체를 생성하고 초기화 작업을 거친 후 , 요청을 처리한다.
- servlet
service() → doxxx() ,The servlet calls service() method to process a client's requesst.
servlet 에서 service() 를 호출 ⇒ doxxx() 들에서 어떤것을 선택한다 ( get, post , )
- destory()
The servlet is terminated by calling the destroy() method.
The destroy() method is called only once at the end of the life cyle of a servlet. This method gives your servlet a chance to close database , halt background threads , write cookie lists or hit counts to disk, and perform other such cleanup activities
After the destroy() method is called, the servlet object is marked for garbage collection.
- Finally, servlet is garbage collectd by the garbage collector of the JVM
be cited from https://www.tutorialspoint.com/servlets/servlets-life-cycle.htm
Type of Http request type
The servlet container(i.e web server) calls the service() method to handle request coming from the client(browsers) and to write the formatted response back to the client
Each time the server receives a request for servlet, the server spawns a new thread and calls service . The service() method checks the HTTP request type(Get, Post, Put, Delete etc. )
you override either doGet() or doPost() depending on what type of request you receive from the client
- The doGet() Method
A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method
- The doPost() Method
A POST request results from an HTML form that specifically lists POST as the Method and it should be handled by doPost() Method
servlet life-cycle scenario
first the HTTP requests coming to the server are delegated to the servlet container.
The servlet container loads the servlet before invoking the service() method
Then the servlet container handles multiple requests by spawning multiple threads, each executing the service() method of a single instance of the servlet
servlet 의 라이프 사이클
init() ==> service() ==> destroy()
init 은 로딩시 최초 1회 , service 메서드는 사용자가 요청을 할 때마다 , destroy() 는 서버 종료 or reload init 메서드는 해당 서블릿에서 사용하는 자원을 초기화 할 때 사용
로딩시 최초 1회 호출 : 로딩 되는 시점 ==> 해당 서블릿으로 최초 요청이 들어왔을 때 단 web.xml 의 servlet 엘레메트의 하위 엘레멘트이 load-on-startup 엘레멘트 값으로 양의 정수값을 입력할 경우 서버가 가동하면서 바로 init 메소드 호출
servlet container 가 요청을 처리하는 방법 등록된 url 매핑을 참고하여 등록된 서블릿으로 요청을 전달(service 메소드 호출) localhost/basicServlet ==> BasicSrervlet의 service 메소드를 통해 응답생성 localhost/index.jsp ==> servlet 설정에 있는 web.xml 에 등록된 *.jsp , *.jspx url-pattern 에 따라 jsp 라는 이름의 서블릿에서 처리 ( JspServlet) localhost/doc/20201223.txt 정적인 자료 ==>
web.xml 도
댓글
댓글 쓰기