zoukankan      html  css  js  c++  java
  • ServletConfig

    ServletConfig 

    Servlet配置

     比如web程序中的某一个Servlet需要配置一些初始化信息,需要在web.xml中进行配置

    <servlet>
        <servlet-name>servletDemo1</servlet-name>
        <servlet-class>cn.myTest.servletDemo1</servlet-class>
        
        <init-param>
            <param-name>data</param-name>
            <param-value>abc</param-value>
        </init-param>
      </servlet>

    使用配置好了之后,web服务器会自动生成一个servletConfig对象,然后在调用Servlet对象的init方法时传递给它。

    public class servletDemo1 extends HttpServlet{    
        
        private ServletConfig config;       //1. 首先定义一个ServletConfig对象
        
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {        
            
            String value = config.getInitParameter("data");       //3. 可以使用这个对象
            System.out.print(value);
        }
        
        @Override                          //2. 然后初始化这个对象
        public void init(ServletConfig config) throws ServletException {
            this.config = config;
        }
    }

    那么在实际开发中,是不需要这么做的,Servlet的父类已经把获取servletConfig对象的方法封装好了

    public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {        
            
            String value =  this.getServletConfig().getInitParameter("data");  
            
        }
        

    一般需要配置的有

    servlet采用哪个码表,Servlet连接数据库,Servlet使用哪个配置文件。

  • 相关阅读:
    【Cookie】java.lang.IllegalArgumentException An invalid character [32] was present in the Cookie value
    【会话技术】Cookie技术 案例:访问时间
    进程池
    管道和Manager模块(进程之间的共享内容)
    队列
    锁Lock,信号量Semaphore,事件机制Event
    multiprocess模块
    进程
    reduce
    struct模块
  • 原文地址:https://www.cnblogs.com/tech-bird/p/3830926.html
Copyright © 2011-2022 走看看