一个web项目web.xml的配置中<context-param>配置是起什么作用的
展开全部
在任一个 servlet 或 filter 中使用:
@Override
public void init(ServletConfig config) throws ServletException {
String paramValue1 = config.getServletContext().getInitParameter("ParamName1");
}
//or
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String paramValue1 = this.getServletContext().getInitParameter("ParamName1");
...
}
...
}
另外,配置在servlet中的init-param的用法为:
<servlet>
<servlet-name>A Servlet</servlet-name>
<servlet-class>com.controller.TestServlet</servlet-class>
<init-param>
<description>This is an init parameter example</description>
<param-name>InitParam1</param-name>
<param-value>init param value</param-value>
</init-param>
</servlet>
@Override
public void init(ServletConfig config) throws ServletException {
String initParam1 = config.getInitParameter("InitParam1");
}
//or
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String initParam1 = getServletConfig().getInitParameter("InitParam1");
...
}
比较一下:
getServletContext().getInitParameter();
getServletConfig().getInitParameter();
@Override
public void init(ServletConfig config) throws ServletException {
String paramValue1 = config.getServletContext().getInitParameter("ParamName1");
}
//or
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String paramValue1 = this.getServletContext().getInitParameter("ParamName1");
...
}
...
}
另外,配置在servlet中的init-param的用法为:
<servlet>
<servlet-name>A Servlet</servlet-name>
<servlet-class>com.controller.TestServlet</servlet-class>
<init-param>
<description>This is an init parameter example</description>
<param-name>InitParam1</param-name>
<param-value>init param value</param-value>
</init-param>
</servlet>
@Override
public void init(ServletConfig config) throws ServletException {
String initParam1 = config.getInitParameter("InitParam1");
}
//or
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String initParam1 = getServletConfig().getInitParameter("InitParam1");
...
}
比较一下:
getServletContext().getInitParameter();
getServletConfig().getInitParameter();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
传递 参数!
参考资料: sername
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询