티스토리 뷰
서블릿 작동순서
웹브라우저 - 웹서버 - 웹어플리케이션서버(WAS) - servlet컨테이너(스레드생성, 서블릿객체생성)
서블릿 라이프 사이클(생명주기)
1. servlet 객체생성 (최초한번)
2. init()호출 (최초한번)
3. service(), doGet(), doPost() 호출 (요청시 매번)
4. destory()호출 (마지막 한번 자원 헤제- 서블릿수정, 서버재가동 등)
서블릿의 최초 요청시 객체가 만들어져 메모리에 로딩되고 이후 요청시에는 기존의 객체를 재활용 -> 동작속도가 빠르다
- package ex.mystudy.am;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- @WebServlet("/LC")
- public class LifeCycle extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public LifeCycle() {
- super();
- }
- @Override
- public void init() throws ServletException {
- super.init();
- }
- public void destory() {
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //response.getWriter().append("Served at: ").append(request.getContextPath());
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
- }
->콘솔창
서블릿 선처리(@PostConstruct), 후처리(@PreDestroy)
1. 선처리 : init() 호출 전
2. 후처리 : destory() 호출 후
- package ex.mystudy.am;
- import java.io.IOException;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- @WebServlet("/LC")
- public class LifeCycle extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public LifeCycle() {
- super();
- }
- @Override
- public void init() throws ServletException {
- super.init();
- }
- public void destory() {
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //response.getWriter().append("Served at: ").append(request.getContextPath());
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
- @PostConstruct
- private void postconstruct() {
- }
- @PostConstruct
- private void predestory() {
- }
- }
->콘솔창
'JSP' 카테고리의 다른 글
[JSP] Request, Response 간단한 회원가입창 예제 (0) | 2018.07.10 |
---|---|
[JSP] Request, Response 객체 (0) | 2018.07.10 |
[JSP] Servlet - form값 받기 (0) | 2018.06.21 |
[JSP] Servlet - doGet, doPost (0) | 2018.06.20 |
[JSP] 개념? (0) | 2018.06.20 |
댓글
최근에 올라온 글
최근에 달린 댓글
링크
- Total
- Today
- Yesterday