ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Tomcat中使用servletContext

2021-03-08 20:33:00  阅读:186  来源: 互联网

标签:web String Tomcat getServletContext servletContext ServletContext 使用 name


Tomcat中使用servletContext

  • 运行在JVM上的每一个web应用程序都有一个与之对应的Servlet上下文(Servlet运行环境)
  • Servlet API提供ServletContext接口用来标识Servlet上下文,ServletContext对象可以被web应用程序中的所有Servlet访问
  • ServletContext对象是web服务器中的一个一直路径的根
  • ServletContext用来解决不同用户之间的数据共享

ServletContext的特点

  • 由服务器创建
  • 所有用户共享同一个ServletContext对象
  • 所有的Servlet都可以访问到同一个ServletContext中的属性
  • 每一个web项目对象的是一个ServletContext

ServletContext的使用

  • 获取ServletContext

    • 方式1ServletContext servletContext = this.getServletContext()
    • 方式2ServletContext servletContext1 = this.getServletConfig().getServletContext();
    • 方式3ServletContext servletContext2 = request.getSession().getServletContext();
  • servletcontext设置属性值

    • servletContext.setAttribute("name", "zhangsan");
  • 读取servletcontext的属性值

    • String name = (String)servletContext.getAttribute("name");
  • 获取web,xml中配置的公共属性

    • <context-param>
      	<param-name>chengdu</param-name>
      	<param-value>beautiful</param-value>
      </context-param>
      <!--  在xml配置中添加公共属性  -->
      
    • String chengdu = servletContext.getInitParameter("chengdu");

  • 获取项目下某个文件的绝对路径

    • String realPath = servletContext.getRealPath("web.xml");
  • 获取web项目的上下文路径(虚拟目录路径)

    • String contextPath = servletContext.getContextPath();
public class ServletContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html:charset=utf-8");

        // 获取ServletContext
        // 方式1
        ServletContext servletContext = this.getServletContext();
        // 方式2
        ServletContext servletContext1 = this.getServletConfig().getServletContext();
        // 方式3
        ServletContext servletContext2 = request.getSession().getServletContext();

        // servletcontext设置属性值
        servletContext.setAttribute("name", "zhangsan");

        // 读取servletcontext的属性值
        String name = (String)servletContext.getAttribute("name");

        //  从web和xml中获取参数值
        String chengdu = servletContext.getInitParameter("chengdu");
        System.out.println(chengdu);

        // 获取项目下某个文件的绝对路径
        String realPath = servletContext.getRealPath("web.xml");
        System.out.println(realPath);

        // 获取web项目的上下文路径
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
    }
}

标签:web,String,Tomcat,getServletContext,servletContext,ServletContext,使用,name
来源: https://www.cnblogs.com/shanlei/p/14502009.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有