ServletContext
保存一个数据并显示在页面上
public class HelloServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//this.getInitParameter() 初始化参数//this.getServletConfig() Servlet配置//this.getServletContext() Servlet上下文//ServletContext作用就是保存数据//通过 getServletContext()方法获得ServletContext context1 = this.getServletContext();resp.setContentType("text/html");resp.setCharacterEncoding("UTF-8");//将一个数据保存到 ServletContext中,名字为:username,值为usernameString username = "shilin.z";context1.setAttribute("username",username);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}}
ServletContext作用就是保存数据
通过 getServletContext()方法获得 一个ServletContext容器ServletContext context1 = this.getServletContext();
然后通过setAttribute()方法将数据存储到ServletContext对象中
将保存的数据显示在另一个页面上
public class GetServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext context = this.getServletContext();//从ServletContext对象中取出数据,赋值给一个变量String username = (String)context.getAttribute("username");resp.setContentType("text/html");resp.setCharacterEncoding("utf-8");//将刚才取出的数据输出resp.getWriter().print("名字:"+username);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}}
从ServletContext中取出数据需要用到 getAttribute("数据名") 这里的 数据名 是前面放数据的时候定义的,要和放数据的Servlet对应着看。
总结:
感觉ServletContext像是一个可以保存数据的大容器,每个web程序都有一个这样的容器,
通过 this.getServletContext() 方法可以获取一个这样的容器(对象)
通过 setAttribute("数据名","数据值") 方法往里写入数据,
通过 getAttribute("数据名") 方法从中读取数据
请求转发
<!-- 配置一些web应用的初始化参数--><context-param><param-name>url</param-name><param-value>jdbc:mysql://local:3306/mybatis</param-value></context-param>
public class ServletDemo04 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext context = this.getServletContext();System.out.println("进入ServletDemo04");//转发的请求路径//ServletContext.getRequestDispatcher(String url)中的url只能使用绝对路径;//void forward(ServletRequest request,ServletResponse response)// RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");用来传递request的// requestDispatcher.forward(req,resp); //调用forward实现请求转发//合并上面两句话context.getRequestDispatcher("/gp").forward(req,resp);}
RequestDispatcher(String url) 让两个servlet相互通信成为可能,就像是浏览器发送request请求一样。RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");
通过这段代码,我们可以得到一个映射到/gp(URL)的Servlet的RequestDispatcher requestDispatcher.forward(req,resp);
然后通过 forward() 方法来调用RequsetDispather
<servlet><servlet-name>gp</servlet-name><servlet-class>com.dyq.servlet.ServletDemo03</servlet-class></servlet><servlet-mapping><servlet-name>gp</servlet-name><url-pattern>/gp</url-pattern></servlet-mapping><servlet><servlet-name>s4</servlet-name><servlet-class>com.dyq.servlet.ServletDemo04</servlet-class></servlet><servlet-mapping><servlet-name>s4</servlet-name><url-pattern>/s4</url-pattern></servlet-mapping>
总结:
从xml文件可以看到,这是一个网页(/s4)向另一个网页(/gp)请求转发
这个转发是,s4网页向ServletContext请求获得gp的资源,之后再转发给自己。
请求读取文件资源
文件资源(db.properties
username=rootpassword=123456
public class ServletDemo05 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//把资源变成一个流InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");Properties prop = new Properties();prop.load(is);String username = prop.getProperty("username");String password = prop.getProperty("password");resp.getWriter().print(username+":"+password);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp);}}
ServletContext. getResourceAsStream(String path)默认从WebAPP根目录下取资源Properties类表示一组持久的属性。Properties可以保存到流中或从流中加载。属性列表中的每个键及其对应的值都是一个字符串。void load(InputStream inStream) 从输入字节流读取属性列表(键和元素对)。String getProperty(String key) 使用此属性列表中指定的键搜索属性。
