2013년 11월 13일 수요일

[서블릿 초기화 정보얻기,getInitParameterNames,init-param]서블릿학원,자바학원JSP학원

[서블릿 초기화 정보얻기,getInitParameterNames,init-param]서블릿학원,자바학원JSP학원
 
간단히 아래 예문을 보도록 하죠.
 
package onj;
 
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class InitParamServlet extends HttpServlet {
 
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  } 
 
  public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {

     res.setContentType("text/plain;charset=euc-kr");
     PrintWriter out = res.getWriter();
     out.println("초기 파라미터.."); 
     Enumeration enum = getInitParameterNames();
     while(enum.hasMoreElements()) {
       String name = (String) enum.nextElement();
       //web.xml에서 설정한 오라클자바 community, oraclejavanew.kr 값 출력
       out.println(name + " ---> " + getInitParameter(name));
     }
   }
 }
 
 [web.xml]

<servlet> 
        <servlet-name>initServlet</servlet-name> 
        <servlet-class>onj.InitParamServlet</servlet-class> 
        <init-param>
          <param-name>your name</param-name>
          <param-value>오라클자바 community</param-value>
   <param-name>your url</param-name>
          <param-value>oraclejavanew.kr</param-value>
        </init-param>
    </servlet> 
    <servlet-mapping> 
        <servlet-name>initServlet</servlet-name> 
        <url-pattern>/InitParamServlet</url-pattern> 
    </servlet-mapping>
 

댓글 없음:

댓글 쓰기