Various Contexts in javaEE and their related summaries (servletContext, httpServletRequest, pageContext, ActionContext, servletActionCon…

Hits: 0

Just suddenly confused, about servletContext, httpServletRequest, pageContext, ActionContext, servletActionContext, [session]

  1. servletContext: one of the four domains (servletContext domain, request domain, session domain, pageContext domain). ServletContext is also called the Application domain. It is created when the web application is loaded and destroyed when the web application is removed or closed. The scope is the entire application. The four fields are mainly used to store variables.

The main methods of servletContext are:

**getAttribute([String](http://java.sun.com/j2se/1.5/docs/api/java/lang/String.html "class or interface in java.lang") name) 、set**Attribute([String](http://java.sun.com/j2se/1.5/docs/api/java/lang/String.html "class or interface in java.lang") name, [Object](http://java.sun.com/j2se/1.5/docs/api/java/lang/Object.html "class or interface in java.lang") object)****

request domain: refers to the entire process from the http request to the end of the server processing and returning the response. In this process, the forward method is used to jump to multiple jsp. On these pages you can use variables in the request field.

session domain: the current session is valid, from the browser opening to the browser closing the process. 

pageContext field: Valid within the page, but invalid when jumping to the page.

The context of a request to the [servlet server.] Mainly used to obtain request,**Cookies**、**Session**、 **ContextPath**()

  1. httpServletRequest is the request object sent by the browser.

Main method:

httpServletRequest.**getCookies**()

httpServletRequest.**getContextPath**()

httpServletRequest.**getMethod**()

httpServletRequest.**getSession**()

`

  1. ActionContext: This object provides a context environment for jsp pages, and is mainly used to access shared data between JSPs. The main methods are:

ActionContext.getAttribute(String name)//Get the attributes stored in the pageContext field, you can use setAttribute to set the attributes

The page is also commonly used ${pageContext.request.contextPath} to get the relative path

  1. ActionContext: Before struts2 executes each action, the core controller StrutsPrepareAndExecuteFilter will create an ActionContext and ValueStack objects.

ActionContext context=ActionContext.getContext(); can get the context of the current action.

ActionContext.getContext().getContextMap();//contextMap itself is a map, which can store data, and contextMap also stores some small maps, mainly ValueStack, application, session, request, parameters.

ActionContext.getContext().getValueStack();//Get the value stack

Request acquisition: (ActionContext does not provide a direct method, use contextMap to acquire)

ActionContext.getContext().getContextMap().get(“request”);

  1. servletActionContext: servletActionContext is a subclass of ActionContext, which provides the function of accessing objects directly related to Servlet.

ServletActionContext.getServletContext();
    ServletActionContext.getPageContext();
    ServletActionContext.getRequest();
    ServletActionContext.getResponse();
    // When request parameters are included, use ServletActionContext to obtain the following
    ServletActionContext.getActionContext(HttpServletRequest request);
    ServletActionContext.getValueStack( HttpServletRequest request);
    ServletActionContext.getContext();// Get ActionContext, then you can get all maps that ActionContext can operate

  1. Session: The reason why the request has the getSession method

A cookie is a browser cache, used locally, to store various web pages and data locally.

The session is established by the server, and the session string is stored locally to match the session of the server to achieve docking. This is also the reason why the request request, the request sent from the client to the server, can obtain the local session (the reason why the request has the getSession method).

Reprinted in: /mlbblkss/p/7146829.html

You may also like...

Leave a Reply

Your email address will not be published.