首页 > 其他 > 详细

select context and config

时间:2018-11-03 01:02:20      阅读:112      评论:0      收藏:0      [点我收藏+]
 
     
     @Override
     public void service(HttpServletRequest  request,HttpServletResponse response){
           //获取servletContext对象,写入数据,servletContext对象由服务器创建
            生命周期:服器启动到关闭
           ServletContext sc=this.getServletContext();//获取ServletContext对象
           sc.setAttribute("uname", "javaee");//写入数据
     
       
    @Override
    public void service(HttpServletRequest  request,HttpServletResponse response) {
           ServletContext sc=this.getServletContext();//获取对象  数据。
           String uname=(String) sc.getAttribute("uname");
           System.out.println(uname);
     }
     @Override
     public void service(HttpServletRequest  request,HttpServletResponse response) 
            //获取ServletConfig对象,获取Servlet的私有配置属性
           ServletConfig config=this.getServletConfig();
           String value=config.getInitParameter("charset");
           System.out.println(value);
     }
    //配置如下,使用<init-param>
    <servlet>
           <servlet-name>servlet11</servlet-name>
           <servlet-class>com.j2ee.ConfigServlet</servlet-class>
           <init-param>
                <param-name>charset</param-name>
                <param-value>utf-8</param-value>
           </init-param>
      </servlet>
       <servlet-mapping>
        <servlet-name>servlet11</servlet-name>
        <url-pattern>/config</url-pattern>
    </servlet-mapping>

select context and config

原文:https://www.cnblogs.com/lastingjava/p/9899058.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!