• javaEE中各种Context及其相关的总结(servletContext、httpServletRequest、pageContext、ActionContext、servletActionContext)


    刚刚突然蒙了,关于servletContext、httpServletRequest、pageContext、ActionContext、servletActionContext、session

    1、servletContext:四大域之一(servletContext域、request域、session域、pageContext域 )。ServletContext 又叫Application域,在Web应用被加载时创建,Web应用被移除或关闭时销毁,作用范围为整个应用。四大域主要用于存放变量。

      servletContext的主要方法有:

        getAttribute(String name) 、setAttribute(String name, Object object)

        getContextPath():获取的是webRoot的路径,即根路径

        getRealPath(String path):是根路径+path

      request域:指从http请求到服务器处理结束,返回响应的整个过程。在这个过程中使用forward方式跳转多个jsp。在这些页面里你都可以使用request域的变量。

       session域:当前会话有效,从浏览器打开到浏览器关闭这个过程。 

       pageContext域:在页面内有效,跳转页面就失效。

    对servlet服务器一次请求的上下文环境。主要用于获取CookiesSession request、ContextPath()

    2、httpServletRequest,是浏览器发出的请求对象。

      主要方法:

        httpServletRequest.getCookies()

        httpServletRequest.getContextPath()

        httpServletRequest.getMethod()

        httpServletRequest.getSession()

        httpServletRequest.getParameter(String name)//获取请求参数

        httpServletRequest.getAttribute(String name)//获取request域存放的属性值,也可用set方法设置值

        ...

    3、ActionContext:这个对象为jsp页面提供上下文环境,主要用于访问JSP之间的共享数据。主要方法有:

        ActionContext.getAttribute(String name)//获取pageContext域中存放的属性,可以用setAttribute设置属性

        

        页面中还常用${pageContext.request.contextPath}获取相对路径

    4、ActionContext:struts2在每次动作执行前,核心控制器StrutsPrepareAndExecuteFilter都会创建一个ActionContextValueStack对象。

        ActionContext context=ActionContext.getContext();可以获取当前action的上下文环境。

        ActionContext.getContext().getContextMap();//contextMap本身是一个map,可以存放数据,contextMap中也存放着一些小map,主要有ValueStack、application、session、request、parameters。

        ActionContext.getContext().getValueStack();//获得值栈

        ActionContext.getContext().getApplication();//获得application

        ActionContext.getContext().getSession();//获得Session

        

        request获取:(ActionContext没有提供直接方法,用contextMap获取)

        ActionContext.getContext().getContextMap().get("request");

    5、servletActionContext:servletActionContext是ActionContext的子类,它提供了直接与Servlet相关对象访问的功能。

        

        ServletActionContext.getServletContext();
        ServletActionContext.getPageContext();
        ServletActionContext.getRequest();
        ServletActionContext.getResponse();

        //在含有request请求参数的时候,用ServletActionContext获取以下
        ServletActionContext.getActionContext(HttpServletRequest request);
        ServletActionContext.getValueStack(HttpServletRequest request);

        ServletActionContext.getContext();//获得ActionContext,然后可以得到ActionContext可以操作的所有map

    6、session:request有getSession方法的原因

        cookie是浏览器缓存,用于本地,存储各样的网页、数据在本地。

        而session是服务端建立的,在本地存放了session串,用来匹配服务端的session,从而实现对接。这也是request请求,从客户端向服务器发送的请求,能够获取本地session的原因(request有getSession方法的原因)。

  • 相关阅读:
    MySql的基本架构续
    MySql的基本架构演变
    多列索引
    索引对单个表查询的影响
    技术在短期内总会被高估,但长期内,总会被低估
    MySql初步II
    验证启用了不安全的HTTP方法
    Struts2漏洞利用实例
    sar命令的使用
    HttpWatch的时间分析
  • 原文地址:https://www.cnblogs.com/mlbblkss/p/7146829.html
Copyright © 2020-2023  润新知