잡동사니에도 사랑을

[21.09.13] HelloServlet - init() / doGet() / destroy() 본문

JAVA_EE/Servlet

[21.09.13] HelloServlet - init() / doGet() / destroy()

luvforjunk 2021. 9. 13. 23:05
728x90
반응형

////////HelloServlet

 

package com.hello;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

//@WebServlet("/HelloServlet")

public class HelloServlet extends HttpServlet {

        private static final long serialVersionUID = 1L;

       

        @Override

        public void init(ServletConfig config) throws ServletException {

               System.out.println("실행하자마자 제일 먼저 호출되는 함수 - init()");

        } // Servlet 브라우저 결과를 찾을 init 가장 먼저 찾는다

       

        @Override

        protected void doGet(HttpServletRequest request, HttpServletResponse response)

                       throws ServletException, IOException {

              

               System.out.println("클라이언트 요청이 있을 때마다 호출하는 함수 - service()");

              

               response.setContentType("text/html"); // 이제부터 쓰는 모든 문서를 html 인식해라

               PrintWriter out = response.getWriter();

               out.println("<html>");

               out.println("<head>");

               out.println("</head>");

               out.println("<body>");

               out.println("Hello Servlet!!");

               out.println("</body>");

               out.println("</html>");

        }

        @Override

        public void destroy() {

               System.out.println("서블릿 종료될 호출된다 - destroy()");

        }

}

 

////////web.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

id="WebApp_ID" version="4.0">

  <display-name>helloServlet</display-name>

 

  <servlet>

        <servlet-name>HelloServlet</servlet-name>

        <servlet-class>com.hello.HelloServlet</servlet-class>

  </servlet>

  <servlet-mapping>

        <servlet-name>HelloServlet</servlet-name>

        <url-pattern>/HelloServlet</url-pattern>

  </servlet-mapping>

 

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>

 

결과창

 

Console창

 

728x90
반응형