为什么我们使用servletconfig接口和servletcontext接口
why do we use servletconfig interface and servletcontext interface
大家好,我是 servlet 新手,jsp 我不清楚 servletconfig 接口和 servletcontext 接口 我又开始了 jsp 我遇到了这个词页面上下文。
所以任何人都可以用很好的例子向我解释这些术语。
jsp
中的servletconfig接口和servletcontext接口以及PageContext
ServletConfig 由GenericServlet(它是HttpServlet 的超类)实现。它允许应用程序部署者将参数传递给 servlet(在 web.xml 全局配置文件中),并且 servlet 在其初始化期间检索这些参数。
例如,您的 web.xml 可能看起来像:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.company.(...).MyServlet</servlet-class>
<init-param>
<param-name>someParam</param-name>
<param-value>paramValue</param-value>
</init-param>
</servlet>
在您的 servlet 中,可以像这样检索 "someParam" 参数:
public class MyServlet extends GenericServlet {
protected String myParam = null;
public void init(ServletConfig config) throws ServletException {
String someParamValue = config.getInitParameter("someParam");
}
}
ServletContext
有点不同。它的名字很糟糕,你最好把它想成 "Application scope".
这是一个应用程序范围的范围(认为 "map"),您可以使用它来存储不特定于任何用户但属于应用程序本身的数据。它通常用于存储参考数据,例如应用程序的配置。
您可以在 web.xml 中定义 servlet-context 参数:
<context-param>
<param-name>admin-email</param-name>
<param-value>admin-email@company.com</param-value>
</context-param>
并在您的 servlet 中像这样在您的代码中检索它们:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
String adminEmail = getServletContext().getInitParameter("admin-email"));
}
ServletConfig
ServletConfig 对象由 web 容器为每个 servlet 创建,用于在 initialization.This 期间将信息传递给 servlet 对象可用于从 web.xml 文件中获取配置信息。
何时使用:
如果不时修改任何特定内容。
您可以通过编辑 web.xml
中的值轻松管理 Web 应用程序,而无需修改 servlet
你的 web.xml 看起来像:
<web-app>
<servlet>
......
<init-param>
<!--here we specify the parameter name and value -->
<param-name>paramName</param-name>
<param-value>paramValue</param-value>
</init-param>
......
</servlet>
</web-app>
通过这种方式,您可以在 servlet 中获取值:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//getting paramValue
ServletConfig config=getServletConfig();
String driver=config.getInitParameter("paramName");
}
ServletContext
web 容器为每个 web 应用程序创建一个 ServletContext 对象。此对象用于从 web.xml
获取信息
何时使用:
如果你想将信息共享给所有的 servlet,最好的方法是让它对所有的 servlet 可用。
web.xml 看起来像:
<web-app>
......
<context-param>
<param-name>paramName</param-name>
<param-value>paramValue</param-value>
</context-param>
......
</web-app>
通过这种方式,您可以在 servlet 中获取值:
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
//creating ServletContext object
ServletContext context=getServletContext();
//Getting the value of the initialization parameter and printing it
String paramName=context.getInitParameter("paramName");
}
PageContext
是jsp中的class,其隐式对象pageContext用于设置、获取或删除以下范围的属性:
1.page
2.request
3.session
4.application
大家好,我是 servlet 新手,jsp 我不清楚 servletconfig 接口和 servletcontext 接口 我又开始了 jsp 我遇到了这个词页面上下文。 所以任何人都可以用很好的例子向我解释这些术语。
jsp
中的servletconfig接口和servletcontext接口以及PageContextServletConfig 由GenericServlet(它是HttpServlet 的超类)实现。它允许应用程序部署者将参数传递给 servlet(在 web.xml 全局配置文件中),并且 servlet 在其初始化期间检索这些参数。
例如,您的 web.xml 可能看起来像:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.company.(...).MyServlet</servlet-class>
<init-param>
<param-name>someParam</param-name>
<param-value>paramValue</param-value>
</init-param>
</servlet>
在您的 servlet 中,可以像这样检索 "someParam" 参数:
public class MyServlet extends GenericServlet {
protected String myParam = null;
public void init(ServletConfig config) throws ServletException {
String someParamValue = config.getInitParameter("someParam");
}
}
ServletContext
有点不同。它的名字很糟糕,你最好把它想成 "Application scope".
这是一个应用程序范围的范围(认为 "map"),您可以使用它来存储不特定于任何用户但属于应用程序本身的数据。它通常用于存储参考数据,例如应用程序的配置。
您可以在 web.xml 中定义 servlet-context 参数:
<context-param>
<param-name>admin-email</param-name>
<param-value>admin-email@company.com</param-value>
</context-param>
并在您的 servlet 中像这样在您的代码中检索它们:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
String adminEmail = getServletContext().getInitParameter("admin-email"));
}
ServletConfig
ServletConfig 对象由 web 容器为每个 servlet 创建,用于在 initialization.This 期间将信息传递给 servlet 对象可用于从 web.xml 文件中获取配置信息。
何时使用: 如果不时修改任何特定内容。 您可以通过编辑 web.xml
中的值轻松管理 Web 应用程序,而无需修改 servlet你的 web.xml 看起来像:
<web-app>
<servlet>
......
<init-param>
<!--here we specify the parameter name and value -->
<param-name>paramName</param-name>
<param-value>paramValue</param-value>
</init-param>
......
</servlet>
</web-app>
通过这种方式,您可以在 servlet 中获取值:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//getting paramValue
ServletConfig config=getServletConfig();
String driver=config.getInitParameter("paramName");
}
ServletContext
web 容器为每个 web 应用程序创建一个 ServletContext 对象。此对象用于从 web.xml
获取信息何时使用: 如果你想将信息共享给所有的 servlet,最好的方法是让它对所有的 servlet 可用。
web.xml 看起来像:
<web-app>
......
<context-param>
<param-name>paramName</param-name>
<param-value>paramValue</param-value>
</context-param>
......
</web-app>
通过这种方式,您可以在 servlet 中获取值:
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
//creating ServletContext object
ServletContext context=getServletContext();
//Getting the value of the initialization parameter and printing it
String paramName=context.getInitParameter("paramName");
}
PageContext
是jsp中的class,其隐式对象pageContext用于设置、获取或删除以下范围的属性:
1.page
2.request
3.session
4.application