zoukankan      html  css  js  c++  java
  • 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方法的原因)。

  • 相关阅读:
    HDU4474 Yet Another Multiple Problem BFS搜索
    HDU4473 Exam 数学分析
    2013ACM多校联合(4)
    POJ1273 网络流...
    HDU4472 Count 递推
    POJ1149 PIGS 网络流
    UVA10881 Piotr's Ants 想法题
    javascript js string.Format()收集
    修改 设置 vs.net 网站 调试 设为 起始页
    【转】HTML5杂谈 概念与现行游戏 割绳子 宝石迷阵
  • 原文地址:https://www.cnblogs.com/mlbblkss/p/7146829.html
Copyright © 2011-2022 走看看