web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
1、共享数据 在这个Servlet中保存了数据,就可以在另外一个servlet取到
首先将数据存入HelloServlet中,然后可以在另外一个Servlet类中取出
@WebServlet("/hello") public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //this.getInitParameter() //初始化参数 //this.getServletConfig() Servlet配置 //this.getServletContext() Servlet 上下文 ServletContext context = this.getServletContext(); String name = "逍遥子";//数据 context.setAttribute("name",name);//将一个数据保存在了 ServletContext 中 System.out.println("数据已经存放!!!!"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } @WebServlet("/getc") public class ReadServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //数据 ServletContext context = this.getServletContext(); String name = (String) context.getAttribute("name"); //响应,设置编码 resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); resp.getWriter().print("名字"+name); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } 2、获取初始化参数可以获得在xml中初始化保存的参数
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee " version="4.0"> <!-- 配置一些web应用的初始化参数 --> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/mybatis</param-value> </context-param> </web-app> @WebServlet("/gp") public class ServletDeom03 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //数据 ServletContext context = this.getServletContext(); String url = context.getInitParameter("url"); resp.getWriter().print(url); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } 3、请求转发转发并不会改变请求的路径,重定向才会改变请求的路径
转发是间接获取到资源,重定向是直接拿到资源
@WebServlet("/sd4") public class Servletdemo04 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //数据 System.out.println("进入了demo04页面"); ServletContext context = this.getServletContext(); //RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//转发路径 //requestDispatcher.forward(req,resp);//调用forward实现请求转发 context.getRequestDispatcher("/gp").forward(req,resp);//请求转发 } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } 4、读取资源文件Properties
在java目录下新建properties
在resource目录下新建properties
发现:都被打包到了同一路径下:classes,我们俗称这个路径为classpath
注:在java目录下创建的properties文件,若想导出成功,还需配置xml文件
<!-- 在build中配置resource,来防止我们资源导出失败的问题--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>读文件思路:需要一个文件流;
@WebServlet("/sd5") public class ServletDemo05 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /** * 1、导入properties流,根据项目的相对地址 * 2、创建properties对象 * 3、将刚刚的流加载到properties对象中 * 4、成功引入properties文件 */ 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 = "+username); resp.getWriter().print("password = "+password); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }