java 为什么我们使用servletconfig接口和servletcontext接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29623323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:37:11  来源:igfitidea点击:

why do we use servletconfig interface and servletcontext interface

javajspservlets

提问by Varun

Hi all I am new in servlet and jsp I am not clear with servletconfig interface and servletcontext interfaceI started jsp again I encounter the term PageContext. So any body explain me these term with the nice example.

大家好 我是 servlet 和 jsp 的新手 我不清楚servletconfig 接口和 servletcontext 接口我再次启动了 jsp 我遇到了术语 PageContext。所以任何机构都用很好的例子向我解释这些术语。

servletconfig interface and servletcontext interface and PageContext in jsp

jsp中的servletconfig接口和servletcontext接口和PageContext

回答by dut gurung

ServletConfig

Servlet配置

ServletConfig object is created by web container for each servlet to pass information to a servlet during initialization.This object can be used to get configuration information from web.xml file.

ServletConfig 对象是由 web 容器为每个 servlet 创建的,用于在初始化期间向 servlet 传递信息。该对象可用于从 web.xml 文件中获取配置信息。

when to use :if any specific content is modified from time to time. you can manage the Web application easily without modifing servlet through editing the value in web.xml

何时使用:如果不时修改任何特定内容。您可以通过编辑 web.xml 中的值轻松管理 Web 应用程序,而无需修改 servlet

Your web.xml look like :

你的 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>

This way you can get value in servlet :

这样你就可以在 servlet 中获得价值:

public void doGet(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
     //getting paramValue
    ServletConfig config=getServletConfig();  
    String driver=config.getInitParameter("paramName"); 
    } 


ServletContext

服务端上下文

web container create one ServletContext object per web Application. This object is used to get information from web.xml

web 容器为每个 web 应用程序创建一个 ServletContext 对象。该对象用于从 web.xml 中获取信息

when to use :If you want to share information to all sevlet, it a better way to make it available for all servlet.

何时使用:如果您想将信息共享给所有 sevlet,这是使其可用于所有 servlet 的更好方法。

web.xml look like :

web.xml 看起来像:

<web-app>  
 ......  

  <context-param>  
    <param-name>paramName</param-name>  
    <param-value>paramValue</param-value>  
  </context-param>  
 ......  
</web-app>  

This way you can get value in servlet :

这样你就可以在 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

页面上下文

Is class in jsp, its implicit object pageContextis used to set , get or remove attribute from following scope:

是jsp中的类,它的隐式对象pageContext用于从以下范围设置、获取或删除属性:

1.page

1.页面

2.request

2.请求

3.session

3.session

4.application

4.申请

回答by Olivier Croisier

ServletConfig is implemented by GenericServlet (which is a superclass of HttpServlet). It allows the application deployer to pass parameters to the servlet (in the web.xml global config file), and servlet to retrieve those parameters during its initialization.

ServletConfig 由 GenericServlet(它是 HttpServlet 的超类)实现。它允许应用程序部署者将参数传递给 servlet(在 web.xml 全局配置文件中),并允许 servlet 在其初始化期间检索这些参数。

For example, your web.xml could look like :

例如,您的 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>

In you servlet, the "someParam" param can then be retrieved like this :

在您的 servlet 中,可以像这样检索“someParam”参数:

public class MyServlet extends GenericServlet {
    protected String myParam = null;

    public void init(ServletConfig config) throws ServletException {
        String someParamValue = config.getInitParameter("someParam");
    }
}

ServletContextis a bit different. It is quite badly named, and you'd better think of it as "Application scope".

ServletContext有点不同。它的命名非常糟糕,您最好将其视为“应用程序范围”。

It is an application-wide scope (think "map") that you can use to store data that is not specific to any user, but rather belongs to the application itself. It is commonly used to store reference data, like the application's configuration.

它是一个应用程序范围(想想“地图”),您可以使用它来存储不特定于任何用户,而是属于应用程序本身的数据。它通常用于存储参考数据,如应用程序的配置。

You can define servlet-context parameters in web.xml :

您可以在 web.xml 中定义 servlet-context 参数:

<context-param>
        <param-name>admin-email</param-name>
        <param-value>[email protected]</param-value>
</context-param>

And retrieve them in your code like this in your servlet :

并在您的 servlet 中像这样在您的代码中检索它们:

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    String adminEmail = getServletContext().getInitParameter("admin-email")); 
}