servletcontext 在web 应用中担当的功能:
1,数据共享容器。常用方法:public void setAttribut(Sting name,Object obj)
public Object getAttribut(Sting name)
public void removeAttribut(Sting name)
2,读取初始化参数,比如文档路径,二进制字节流返回指定的web资源,取得指定web文档的 转发对象,取得指定文件的mime 类型
public string getRealPath(String path);
publicInputStream getResourceAsStream(String path);
public RequestDispathcer getRequestDispatcher(String path);
public String getMImeType(Sting file);
3,取得web应用基本信息
public int getMajorVersion()
public int getMinorVersion()
public String getServerInfo()
4,web应用日志输出
public void log(String msg)
public void log(Exception e,String msg)
public void log(String msg,Throwable throwable)
web.xml 配置的主要项目有:
1,初始参数 context-param
2,过滤器 filter
3,过滤器映射 filter-mapping
4,监听器 listener
5,servlet 声明servlet
6,servlet 映射 servlet-mapping
7,异常跳转页面 error-page
8,mime 类型映射 mime-mapping
9,会话对象超时 session-config
10,外部资源声明 resource-ref
11,外部标记库描述符文件taglib
servlet 配置对象 servletconfig
要取得config 对象,需要重写init方法,并传递servletconfig 参数
super.init(config);
this.config=config;
web 容器为每个servlet实例创建一个servletconfig 对象,不同的servlet之间 无法共享使用此对象。
servletconfig 方法:
public Sting getInitParameter(Sting name)
public Enumeration getInitParameterNames()
public Sting getServletName()
public ServletContext getServletContext getServletContext()
servletcontext 对象获得:ServletContext cxt=this.ServletContext();
cxt.setAttribute("xx","yy");
ServletContext cxt=config.getServletContext();
取得转发对象的两种方法
RequestDispatcher rd=request.getRequestDispatcher("xx.jsp")
RequestDispatcher rd=this.getServletContext().getRequestDispatcher("xx.jsp")
注意:使用servletcontext 对象取得转发对象时候,要求使用绝对路径,即以“/”开头,否则会抛出 java.lang.IllegalArgumentException --httpstatus--500
1,数据共享容器。常用方法:public void setAttribut(Sting name,Object obj)
public Object getAttribut(Sting name)
public void removeAttribut(Sting name)
2,读取初始化参数,比如文档路径,二进制字节流返回指定的web资源,取得指定web文档的 转发对象,取得指定文件的mime 类型
public string getRealPath(String path);
publicInputStream getResourceAsStream(String path);
public RequestDispathcer getRequestDispatcher(String path);
public String getMImeType(Sting file);
3,取得web应用基本信息
public int getMajorVersion()
public int getMinorVersion()
public String getServerInfo()
4,web应用日志输出
public void log(String msg)
public void log(Exception e,String msg)
public void log(String msg,Throwable throwable)
web.xml 配置的主要项目有:
1,初始参数 context-param
2,过滤器 filter
3,过滤器映射 filter-mapping
4,监听器 listener
5,servlet 声明servlet
6,servlet 映射 servlet-mapping
7,异常跳转页面 error-page
8,mime 类型映射 mime-mapping
9,会话对象超时 session-config
10,外部资源声明 resource-ref
11,外部标记库描述符文件taglib
servlet 配置对象 servletconfig
要取得config 对象,需要重写init方法,并传递servletconfig 参数
super.init(config);
this.config=config;
web 容器为每个servlet实例创建一个servletconfig 对象,不同的servlet之间 无法共享使用此对象。
servletconfig 方法:
public Sting getInitParameter(Sting name)
public Enumeration getInitParameterNames()
public Sting getServletName()
public ServletContext getServletContext getServletContext()
servletcontext 对象获得:ServletContext cxt=this.ServletContext();
cxt.setAttribute("xx","yy");
ServletContext cxt=config.getServletContext();
取得转发对象的两种方法
RequestDispatcher rd=request.getRequestDispatcher("xx.jsp")
RequestDispatcher rd=this.getServletContext().getRequestDispatcher("xx.jsp")
注意:使用servletcontext 对象取得转发对象时候,要求使用绝对路径,即以“/”开头,否则会抛出 java.lang.IllegalArgumentException --httpstatus--500