Servlet JSP之 ServletConfig对象
互联网
19-10-8
ServletConfig对象有四个方法。

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
(2) 由于一个Web应用程序中的所有Servlet都共享同一个ServletContext对象,所以,ServletContext对象被称为application对象(也就是web应用程序对象)。
(1) getRealPath()
获取某一个文件在服务器上的绝对路径,注意:并非是部署前的路径。
注意我的下面文件存放的目录
(2) getContextPath()
获取当前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对象的详细内容,更多内容请关注技术你好其它相关文章!
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场