getInitParameter、 getInitParameterNames、 getServletName
(1)getInitParameter、 getInitParameterNames用于猎取Web.xml中的参数名、参数值。
(2)getServletName 猎取 Web.xml中的 Servlet-name。
实例
下面是Web.xml的文件内容:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>TestServletConfig</servlet-name> <servlet-class>com.djun.serveleMapping.TestServletConfig</servlet-class> <!--设置Servlet的初始化参数--> <!-- 怎样猎取初始化的参数? 1、getInitParameter(String name) Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist. 2、 getInitParameterNames() Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters. --> <init-param> <param-name>username</param-name> <param-value>admin</param-value> </init-param> <init-param> <param-name>passworld</param-name> <param-value>admin</param-value> </init-param> <!-- 指定Servlet JSP被建立的机遇 若数值 a<0,则仅在第一次的时刻被建立。 若 a>=0 , 则在当前运用被Servlet容器加载时建立实例 数值越小越早被建立 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>TestServletConfig</servlet-name> <!--只需后缀为html的文件都由该类处置惩罚--> <url-pattern>/servletConfig</url-pattern> </servlet-mapping> </web-app>
import javax.servlet.*; import java.io.IOException; import java.util.Enumeration; public class TestServletConfig implements Servlet { @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("Init TestServletConfig..."); System.out.println("-----------实行getInitParameter--------"); String username = servletConfig.getInitParameter("username"); String passworld = servletConfig.getInitParameter("passworld"); System.out.println("username: " + username+"\n"+"password : "+passworld); System.out.println("----------实行getInitParameterNames------"); Enumeration<String> names = servletConfig.getInitParameterNames(); while(names.hasMoreElements()){ String name = names.nextElement(); String value = servletConfig.getInitParameter(name); System.out.println("username: " + name+"\n"+"password : "+value); } String servletName = servletConfig.getServletName(); System.out.println(servletName); } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("TestServletConfig...."); } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
getServletContext
(1)Servlet为每一个Web运用程序都建立了一个对应的ServletContext对象,ServletContext对象被包含在ServletConfig对象中,经由过程挪用 ServletContext.getServletContext()要领能够返回ServletContext对象的援用。
(2) 因为一个Web运用程序中的一切Servlet都同享同一个ServletContext对象,所以,ServletContext对象被称为application对象(也就是web运用程序对象)。
(1) getRealPath()
猎取某一个文件在服务器上的绝对途径,注重:并非是布置前的途径。
注重我的下面文件寄存的目次
猎取当前Web运用的某一个文件对应的输入流。
System.out.println("getContextPath() -----------"); String contextPath = servletContext.getContextPath(); System.out.println(contextPath); String fileName = "application.properties"; try { File file = new File(realPath+ "/" + fileName); ClassLoader classLoader = getClass().getClassLoader(); InputStream is = classLoader.getResourceAsStream(realPath + "/" + fileName); System.out.println(realPath+ "/" + fileName); System.out.println("1. "+ is); } catch (Exception e) { e.printStackTrace(); }
以上就是Servlet JSP之 ServletConfig对象的细致内容,更多请关注ki4网别的相干文章!