프로그래밍 정리/Web

Servlet의 LifeCycle

주누다 2013. 7. 11. 21:54
반응형

초기화 정보 얻기

- init() 메소드에서 서블릿 개체 초기화

- 초기화 정보의 위치

-> web.xml

<servlet>

<servlet-name>~</servlet-name>

<servlet-class>~</servlet-class>

<init-param>

<param-name>name</param-name>

<param-value>value</param-value>


- String getInitParameter(String name)


=================================================================================================================


(InitParameterServlet)

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Enumeration;


import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



public class InitParameterServlet extends HttpServlet{

@Override

public void init(ServletConfig config) throws ServletException {

// TODO Auto-generated method stub

super.init(config);

}

@Override

protected void service(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

// TODO Auto-generated method stub

PrintWriter out = res.getWriter();

out.println("초기 파라미터들....");

Enumeration enumer = getInitParameterNames();

while(enumer.hasMoreElements()){

String name = (String)enumer.nextElement();

out.println(name + " ---> " + getInitParameter(name));

}

}

}



(web.xml)
<servlet>
      <servlet-name>InitParameterServlet</servlet-name>
      <servlet-class>InitParameterServlet</servlet-class>
      <init-param>
          <param-name>tomcat ver</param-name>
          <param-value>7.0.0</param-value>
      </init-param>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>InitParameterServlet</servlet-name>
      <url-pattern>/InitParameterServlet</url-pattern>
  </servlet-mapping>

================================================================================================================

요청 정보 얻기
- HttpServletRequest 개체
-> String getParameter(String name)
-> String[] getParameterValues(String name)
-> String getHeader(String name)
-> String getRemoteAddr()
-> void setCharacterEncoding(String env)

응답 생성
- HttpServletResponse 개체
-> void setContentType(String type)
-> void addHeader(String name, String value)
-> PrintWriter getWriter()
-> ServletOutputStream getOutputStream()


반응형

'프로그래밍 정리 > Web' 카테고리의 다른 글

Cookies  (0) 2013.07.13
Servlet의 요청 처리 메서드  (0) 2013.07.11
Servlet 요청 방식  (0) 2013.07.11
Servlet  (0) 2013.07.11