标签:sys 属性 数据 Servle ice map sel param his
@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
标签:sys 属性 数据 Servle ice map sel param his
原文地址:https://www.cnblogs.com/lastingjava/p/9899058.html