登录  /  注册
首页 > Java > java教程 > 正文

Servlet JSP之 ServletConfig对象

(*-*)浩
发布: 2019-10-08 15:29:07
转载
2139人浏览过

servletconfig对象有四个方法。

Servlet JSP之 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&#39;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()

获取某一个文件在服务器上的绝对路径,注意:并非是部署前的路径。

注意我的下面文件存放的目录

java-4.png(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对象的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:CSDN网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号