Servlet——ServletContext

一、ServletConfigjavax.servlet.ServletContext包;

  //对应jsp中的application

  //其中定义了很多方法使得Servlet可以和tomcat进行数据交互和罗基交换;

  //ServletContext在当前web程序和jvm中唯一,允许在多个Servlet之间数据的共享;

二、获取ServletContext对象:

  (1)通过GenericServlet提供的 getServletContext();

    //this.getServletContext();

  (2)通过ServletConfig提供的getServletContext();

    //this.getServletConfig().getServletContext();

  (3)通过HttpServletRequest获取;

    //req.getServletContext();

  (4)通过HttpSession获取;

    //req.getSession().getServletContext();

四、操作数据方法://域方法都包含属性方法;

  1.设置属性:void setAttribute(String name, Object object);//多次调用同个name属性会覆盖;

  2.获取属性:Object getAttribute(String name);//获取没设置过的属性名返回null;

  3.删除属性:void removeAttribute(String name);

  4.获取所有本域的属性名:Enumeration getAttributeNames();

@WebServlet("/test")
public class Test extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext servletContext = getServletContext();
        //设置属性
        servletContext.setAttribute("name", "zs");
        servletContext.setAttribute("name", "ww");
        servletContext.setAttribute("age", 14);
        //获取指定属性
        System.out.println(servletContext.getAttribute("name"));
        //删除属性
        servletContext.removeAttribute("name");
        //获取所有属性
        Enumeration<String> attributeNames = servletContext.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String name = attributeNames.nextElement();
            Object value = servletContext.getAttribute(name);
            System.out.println(name+":"+value);
        }
    }

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

五、获取资源方法 

  (1)获取项目名:String getContextPath();

    //返回的项目名开头包含/斜杠;

  (2)获取真实路径:String getRealPath(String path);

    //传入的path前必须包含斜杠;“/a.txt”

  (3)获取路径下所有资源路径:Set getResourcePaths("/WEB-INF");

  (4)获取资源流:InputStream getResourceAsStream("/WEB-INF/b.txt");

  (5)获取参数值:String getInitParameter(String name);

    //根据名字返回当前程序所包含的初始化参数的值;<context-param>

  (6)获取参数值:Enumeration<String>        getInitParameterNames()

    //返回当前程序中所有的初始化参数的名字的枚举的引用(集合);

  (7)获取编码:String getRequestCharacterEncoding();

  (8)设置编码:void setRequestCharacterEncoding(String encoding);

@WebServlet("test")
public class Test extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象;
        ServletContext servletContext = getServletContext();
        //返回项目名,带/斜杠;/review
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
        //返回文件的真实路径;E:\JavaProject\review2\web\index.jsp
        String realPath = servletContext.getRealPath("/index.jsp");
        System.out.println(realPath);
        //返回路径下所有资源路径;
        Set<String> resourcePaths = servletContext.getResourcePaths("/WEB-INF");
        for (Iterator<String> iterator = resourcePaths.iterator(); iterator.hasNext(); ) {
            String next = iterator.next();
            System.out.println(next);
        }
        //获取资源流
        InputStream stream = servletContext.getResourceAsStream("/WEB-INF/b.txt");
        System.out.println(stream);
    }

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

原文地址:https://www.cnblogs.com/Tractors/p/11263454.html

时间: 2024-10-09 23:32:39

Servlet——ServletContext的相关文章

Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext

场景描述 项目中用到spring boot进行带页面的开发,从github上的simples中下载了对应的spring-boot-sample-web-jsp项目,经简单修改之后死活启动不起来,各种异常.spring-boot以简单容易上手为核心宗旨,可为什么在引入jar时就会出现这么多问题呢? 异常信息 异常一: 2016-06-17 11:04:14.216 ERROR 4684 --- [ main] o.s.boot.SpringApplication : Application sta

java.lang.NoClassDefFoundError: javax/servlet/ServletContext

方法1:把SpringBoot中main方法所在的class不再继承org.springframework.boot.context.web.SpringBootServletInitializer即可 因为继承org.springframework.boot.context.web.SpringBootServletInitializer,并重写里面的configure方法,即可将这个项目打成war,并部署到tomcat或其它jee容器中. protected SpringApplicatio

【原】tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决

现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig 解决: 把tomcat目录下的lib/servlet-api.jar文件拷贝到$JAVA_HOME/jre/lib/ext目录下,再启动tomcat问题解决

报错org.apache.catalina.LifecycleException caused by java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader()Ljava/lang/ClassLoader

今天将tomcat6更换成了tomcat7,报错的内容 严重: A child container failed during startjava.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/webandr

Cannot find the class file for javax.servlet.ServletContext.

当eclipse中新导入的Java Project的时候,往往会碰到各种各样的问题,下面是个典型的问题: 如何解决该问题: 方案1. Project--->Properties-->Libraries 中添加 Tomcat中的 serverlet-api.jar 方案2. 添加配置tomcat服务器到Java Project中 Cannot find the class file for javax.servlet.ServletContext.

tomcat7 启动项目报错 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()

JDK版本:jdk1.8.0_77 Tomcat 版本:apache-tomcat-7.0.47 异常重现步骤: 1.完成项目部署 2.启动Tomcat 异常头部信息:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig() 解决方案: 把apache-tomcat-7.0.47/lib/servlet-api.jar 拷贝到$JAVA_HOME/jre/lib/ext目录下去 参考博文:

如果遇到java.lang.NoClassDefFoundError: javax/servlet/ServletContext问题具体代码和解决方案如下面所示

出现的问题: java.lang.NoClassDefFoundError: javax/servlet/ServletContext at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.getDeclaredMethods(Unknown Source) at org.spring

java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader()Ljava/lang/ClassLoader;

在实验室上电脑搭建服务器端时,已有tomcat6.0版,不支持servlet3.0,于是重新搭建tomcat7.0版出现下面信息 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader()Ljava/lang/ClassLoader; 查找网上信息,低版本有叫改web.xml的,servlet3.0根本不需要修改. 后查stackoverflow也晕菜.搜索网上,解决方法如下: 修改jre/lib\ext里

Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.linewell.wechat.WechatApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.spri