SevletContext的四种使用方法

web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;

一、共享数据

在一个Servlet中保存的数据,可以在另一个Servlet中拿到

public class Servlet1 extends HttpServlet{
@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String name = "名字";
        context.setAttribute("username",name);
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doGet(req, resp);
    }
}
public class Servlet2 extends HttpServlet{
@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String username = (String) context.getAttribute("username");

        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().print("名字:"+username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doGet(req, resp);
    }
}

二、获取初始化参数

<!--配置一些web应用初始化参数-->
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/student</param-value>
    </context-param>
public class Servlet3 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 {
		super.doGet(req, resp);
    }
}

三、请求转发

public class Servlet4 extends HttpServlet{
@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        System.out.println("进入了该方法");
//        RequestDispatcher requestDispatcher = context.getRequestDispatcher("/url");//转发的请求路径
//        requestDispatcher.forward(req,resp);//forward() 转发的作用 ,实现请求转发
        context.getRequestDispatcher("url").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doGet(req, resp);
    }
}

四、读取资源文件

Properties

  • 在java目录下新建tt.properties
  • 在resources目录下新建tt.properties

发现都被打包到了同一个路径下:classes,这个路径俗称classpath;

public class Servlet5 extends HttpServlet{
@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/tt.properties");
        Properties properties = new Properties();
        properties.load(is);
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");

        resp.getWriter().print(username+" "+password);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doGet(req, resp);
    }
}

总结

记得都要配置web.xml来获得请求路径

发布了23 篇原创文章 · 获赞 43 · 访问量 1392

猜你喜欢

转载自blog.csdn.net/qq_41256881/article/details/105244537