ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 10. ServletConfig
    개발자 수업/JSP 2021. 12. 3. 10:22

    1. ServletConfig 기능
        1) ServletContext 객체를 얻는 기능
        2) 서블릿에 대한 초기화 작업 기능

    2. @WebServlet 어노테이션을 이용한 서블릿 설정
        1) urlPatterns
            - 웹 브라우저에서 서블릿 요청시 사용하는 매핑 이름
        2) name
            - 서블릿 이름
        3) loadOnStartUp
            - 컨테이너 실행시 서블릿이 로드되는 순서 지정
        4) initParams
            - 매개변수 추가하는 기능

     

    //servlet 만들 때 설정하는 것
    package kr.co.ezenac.servletconfig;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebInitParam;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class initParamServlet
     */
    @WebServlet(
    		urlPatterns = { 
    				"/sinit", 
    				"/sinit2"
    		}, 
    		initParams = { 
    				@WebInitParam(name = "email", value = "admin@ezen.com"), 
    				@WebInitParam(name = "tel", value = "010-1588-2022")
    		})
    public class initParamServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("text/html;charset=utf-8");
    		PrintWriter out = response.getWriter();
    		
    		//설정한 매개변수의 name으로 값을 가져옴
    		String email = getInitParameter("email");
    		String tel = getInitParameter("tel");
    		
    		out.print("<html><body>");
    		out.print("<table><tr>");
    		out.print("<td>email : </td><td>" + email + "</td></tr>");
    		out.print("<tr><td>tel : </td><td>" + tel + "</td></tr>");
    		out.print("</table></body></html>");
    	}
    
    }

    //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>chap06_Servlet</display-name>
      <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>
      
      <servlet>
      	<servlet-name>sinit</servlet-name>
      	<servlet-class>kr.co.ezenac.servletconfig.initParamServlet</servlet-class>
      	<init-param>
      		<param-name>email</param-name>
      		<param-value>admin@ezen.com</param-value>
      	</init-param>
      	<init-param>
      		<param-name>tel</param-name>
      		<param-value>010-1588-2022</param-value>
      	</init-param>
      </servlet>
      <servlet-mapping>
      	<servlet-name>sinit</servlet-name>
      	<url-pattern>/first</url-pattern>
      </servlet-mapping>
    </web-app>

     

     

    '개발자 수업 > JSP' 카테고리의 다른 글

    12. 서블릿 스코프, URL 패턴  (0) 2021.12.06
    11. 웹 페이지 연결 기능  (0) 2021.12.03
    9. ServletContext  (0) 2021.12.02
    8. 서블릿 API 사용하기2  (0) 2021.12.02
    7. 서블릿 API 사용하기  (0) 2021.11.27

    댓글