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

Var*_*run 7 java jsp servlets

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

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

小智 18

的ServletConfig

ServletConfig对象由Web容器创建,用于在初始化期间将信息传递给servlet.此对象可用于从web.xml文件获取配置信息.

何时使用: 如果任何特定内容不时被修改.您可以轻松地管理Web应用程序,而无需通过编辑web.xml中的值来修改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>
Run Code Online (Sandbox Code Playgroud)

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

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

ServletContext中

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

何时使用: 如果您想要将信息共享给所有sevlet,那么这是一种让它可用于所有servlet的更好方法.

web.xml看起来像:

<web-app>  
 ......  

  <context-param>  
    <param-name>paramName</param-name>  
    <param-value>paramValue</param-value>  
  </context-param>  
 ......  
</web-app>  
Run Code Online (Sandbox Code Playgroud)

这样你就可以在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");   
}  
Run Code Online (Sandbox Code Playgroud)

用PageContext

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

1.page

2.request

3.session

4.应用


Oli*_*ier 8

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>
Run Code Online (Sandbox Code Playgroud)

在你的servlet中,然后可以像这样检索"someParam"参数:

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

    public void init(ServletConfig config) throws ServletException {
        String someParamValue = config.getInitParameter("someParam");
    }
}
Run Code Online (Sandbox Code Playgroud)

ServletContext有点不同.名字很糟糕,你最好把它想象成"应用范围".

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

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

<context-param>
        <param-name>admin-email</param-name>
        <param-value>admin-email@company.com</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

并在servlet中的代码中检索它们:

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