1.分析
话不多说直接上图
2.源码分析
public interface ServletContext {
public String getContextPath();
//方法入参为uriPath(String),是一个资源定位符的路径。返回一个ServletContext实例
public ServletContext getContext(String uripath);
//分别返回当前servlet容器支持的Servlet规范最高版本和最低版本。
public int getMajorVersion();
public int getMinorVersion();
//分别返回当前应用基于的Servlet规范最高版本和最低版本
public int getEffectiveMajorVersion();
public int getEffectiveMinorVersion();
//返回servlet容器的名称和版本,格式为servername/versionnumber。
public String getServerInfo();
//返回应用的名称,这里的名称是web.xml里面配置的display-name
public String getServletContextName();
//返回文件的MIME类型,MIME类型是容器配置的。可用通过web.xml进行配置
public String getMimeType(String file);
public void log(String msg);
public void log(String message, Throwable throwable);
//根据传入的路径,列出该路径下的所有资源路径
public Set<String> getResourcePaths(String path);
//将指定路径的资源封装成URL实例并返回
public URL getResource(String path) throws MalformedURLException;
//获取指定路径资源的输入流InputStream并返回
public InputStream getResourceAsStream(String path);
public RequestDispatcher getRequestDispatcher(String path);
public RequestDispatcher getNamedDispatcher(String name);
//根据资源虚拟路径,返回实际路径。
public String getRealPath(String path);
//用来获取应用的初始化参数相关数据的,参数的作用域是整个应用
public String getInitParameter(String name);
public Enumeration<String> getInitParameterNames();
public boolean setInitParameter(String name, String value);
public Object getAttribute(String name);
public Enumeration<String> getAttributeNames();
public void setAttribute(String name, Object object);
public void removeAttribute(String name);
}
3.ServlerContext总结
ServletContext是个很重要的东西,在每次的servlet规范更新中,这个接口都有较大的变化。
因为ServletContext是容器和应用沟通的桥梁,从一定程度上讲ServletContext就是servlet规范的体现。
4.简单示例
1.存入
public class ServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.printf("----Hello Serclet----");
// this.getInitParameter(); 初始化参数
// this.getServletContext(); Servlet上下文
// this.getServletConfig(); Servlet配置
javax.servlet.ServletContext context= this.getServletContext();
String Myname="tian";
context.setAttribute("Myname",Myname);//将数据保存在了ServletContext中,名字为:Myname 。值 Myname
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.取出
public class GetServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context= this.getServletContext();
String myname=(String) context.getAttribute("Myname"); //获取上下文的值
resp.setCharacterEncoding("utf-8");
resp.setHeader("Content-Type","text/html;charset=utf-8");
resp.getWriter().print("获取的信息"+myname);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.web.xml 配置
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Context</servlet-name>
<servlet-class>ServletContext.ServletContext</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Context</servlet-name>
<url-pattern>/Context</url-pattern>>
</servlet-mapping>
<servlet>
<servlet-name>GetContext</servlet-name>
<servlet-class>ServletContext.GetServletContext</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetContext</servlet-name>
<url-pattern>/GetContext</url-pattern>>
</servlet-mapping>
</web-app>
5.初始化配置
在Web.xml中新增节点
<!-- 配置初始化-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://XXXXXXXXXXXX:3306/javawebuser</param-value>
</context-param>
读取
public class ServletContextApply extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context= this.getServletContext();
String URL= context.getInitParameter("url");//获取初始化参数 web.xml
resp.getWriter().print(URL);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
6.转发
public class ServletContextTransmit extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.printf("***************转发执行****************");
ServletContext context= this.getServletContext();
context.getRequestDispatcher("/ContextApply").forward(req,resp);
}
}
6.1转发与重定向
7.读取资源文件
Properties
-
在java目录下新建properties
-
在resources目录下新建properties
发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath:
思路:需要一个文件流;
public class ServletProperties extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("--------------------------------");
//获取项目文件路径
InputStream is= this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
//新建Properties对象
Properties prop = new Properties();
//读取流
prop.load(is);
//获取流信息
String use= prop.getProperty("userame");
String pass= prop.getProperty("password");
resp.getWriter().print(use+":"+pass);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}