JSP/init-param
JSP, Servlet init-param example
Soul-Learner
2009. 1. 20. 14:42
모든 JSP에서 사용할 수 있도록 init-param을 설정하는 예
서블릿과 JSP에 초기화 파라미터를 설정하는 다른 예들
위에서 선언한 JSP url-pattern 도 반드시 필요함.
To retrieve all the init parameters, use
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>SAVE_ROOT</param-name>
<param-value>e:/StMgt</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>SAVE_ROOT</param-name>
<param-value>e:/StMgt</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
서블릿과 JSP에 초기화 파라미터를 설정하는 다른 예들
<web-app> <!-- Init parameters for the servlet ReadInitParams --> |
위에서 선언한 JSP url-pattern 도 반드시 필요함.
-
Reading the Init Parameters from the web.xml file
The
ServletConfig
object provides a handle to the initialization parameters defined inweb.xml
.ServletConfig
's method:getInitParameter()
is used to retrieve the init parameters. The following code snippet illustrates how to read the init parameters from theinit()
and theservice()
methods of a servlet.
public void init(ServletConfig config) throws ServletException { String emailHost = config.getInitParameter("emailHost"); String webMaster = config.getInitParameter("webMaster"); }
or public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String host = getServletConfig().getInitParameter("emailHost");
String master = getServletConfig().getInitParameter("webMaster");}
To retrieve all the init parameters, use
Enumeration
and ServletConfig
's getInitParmeterNames()
. For example,public void init(ServletConfig config) throws ServletException { Enumeration params = config.getInitParameterNames(); // Print the init parameter names and values to the console while (params.hasMoreElements()) { String name = (String) params.nextElement(); System.out.println("Parameter Name: " + name + " Value: " + config.getInitParameter(name)); } } |
Reading the init parameters from the web.xml file in a JSP
The implicit object - ServletConfig
can be read either in the jspInit()
method or directly to retrieve the values of init parameters. Following is the code snippet to retrieve the values from readInitParamJSP.jsp
<%@ page language="java"%> |