JSP config对象详解

config对象代表当前JSP配置信息,实质上是ServletConfig的一个实例,常用来获取Servlet的初始化参数。

getServletName()获取这个servlet实例的名称

<%@ page language="java" contentType="text/html; charset=gb2132"  pageEncoding="ISO-8859-1"%> 
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
</head>  
<body>  
<!-- 直接输出config的getServletName的值 -->  
<%=config.getServletName()%>  
</body>  
</html>  

输出:jsp

所有的JSP都有相同的名字:jsp,所以此行代码将输出jsp。

getInitParameter根据name,获取初始化信息

<%@ page language="java" contentType="text/html; charset=gb2132"  pageEncoding="ISO-8859-1"%> 
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
</head>  
<body>  
<!-- 输出该JSP中名为name的参数配置信息 -->  
name配置参数的值:<%=config.getInitParameter("name")%><br/>  
<!-- 输出该JSP中名为age的参数配置信息 -->  
age配置参数的值:<%=config.getInitParameter("age")%>  
</body>  
</html>  
//web.xml
<servlet> 
  <!--指定servlet的名字-->  
  <servlet-name>config</servlet-name>  
  <!--指定哪一个JSP页面配置成Servlet-->  
  <jsp-file>/configTest2.jsp</jsp-file>  
  <!--配置名为name的参数,值为yeeku-->  
  <init-param> 
    <param-name>name</param-name>  
    <param-value>yeeku</param-value> 
  </init-param>  
  <!--配置名为age的参数,值为30-->  
  <init-param> 
    <param-name>age</param-name>  
    <param-value>30</param-value> 
  </init-param> 
</servlet>
<servlet-mapping> 
  <!--指定将config Servlet配置到/config路径-->  
  <servlet-name>config</servlet-name>  
  <url-pattern>/config</url-pattern> 
</servlet-mapping>

配置文件片段中的</init-param>为该Servlet(其实是JSP)配置了两个参数:name和value。上面的配置片段将configTest2.jsp页面配置成名为config的Servlet,并将该Servlet映射到/config处。这就允许我们使用/config来访问该页面。

例子

//web.xml
<servlet> 
  <servlet-name>Servlet_03</servlet-name>  
  <servlet-class>com.enterise.always.servlet.Servlet_03</servlet-class>  
  <init-param> 
    <param-name>name_01</param-name>  
    <param-value>value_01</param-value> 
  </init-param>  
  <init-param> 
    <param-name>name_02</param-name>  
    <param-value>value_02</param-value> 
  </init-param> 
</servlet>
<servlet-mapping>  
  <servlet-name>Servlet_03</servlet-name>  
  <url-pattern>/servlet/Servlet_03</url-pattern>  
</servlet-mapping>
//ServletTest_03.java
package com.enterise.always.servlet;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletTest_03 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
    IOException {
        doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
    IOException {
        //1.获取ServletConfig对象  
        ServletConfig config = getServletConfig();

        //1.getInitParameter  根据name,获取初始化信息
        String para_01 = config.getInitParameter("name_01");
        String para_02 = config.getInitParameter("name_02");

        System.out.println("para_01----------->" + para_01);
        System.out.println("para_02----------->" + para_02);

        //2.getInitParameterNames  获取所有初始化信息的名称
        Enumeration enumeration = config.getInitParameterNames();
        while (enumeration.hasMoreElements()) {
            String name = (String) enumeration.nextElement();
            String value = config.getInitParameter(name);
            System.out.println("name----------->" + name);
            System.out.println("value----------->" + value);
        }
        //3.getServletContext  获取ServletContext对象
        ServletContext servletContext = config.getServletContext();
        System.out.println("servletContext----------->" + servletContext);
        //4.getServletName  获取这个servlet实例的名称
        String servletName = config.getServletName();
        System.out.println("servletName----------->" + servletName);
    }
}

执行输出:

para_01----------->value_01  
para_02----------->value_02  
name----------->name_02  
value----------->value_02  
name----------->name_01  
value----------->value_01  
servletContext----------->org.apache.catalina.core.ApplicationContextFacade@b398da  
----------->Servlet_03  

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。