Struts 2 accessing servlet sharing

Time:2021-4-14

1: Access or add the request / session / application property
 

Copy codeThe code is as follows:
public String scope() throws Exception{

   ActionContext ctx = ActionContext.getContext();

ctx.getApplication (). Put (“app”, “application scope”); / / put the app into ServletContext

ctx.getSession (). Put (“session”, “session range”); / / put session into session

ctx.put (“req”, “request range”); / / put req in the request

   return “scope”;

}

 

JSP:

Copy codeThe code is as follows:
 <body>

    ${applicationScope.app} <br>

    ${sessionScope.ses}<br>

    ${requestScope.req}<br>

 </body>

 

2: Get the HttpServletRequest / httpsession / ServletContext / httpservletresponse object
 

Method 1: directly obtain the following information through the servletactioncontext. Class:

Copy codeThe code is as follows:
public String rsa() throws Exception{

       HttpServletRequest request = ServletActionContext.getRequest();

       ServletContext servletContext = ServletActionContext.getServletContext();

       request.getSession()

       HttpServletResponse response = ServletActionContext.getResponse();

       return “scope”;

}

Method 2 implements the specified interface, which is injected by the struts framework runtime

Copy codeThe code is as follows:
public class HelloWorldAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{

       private HttpServletRequest request;

       private ServletContext servletContext;

       private HttpServletResponse response;

       public void setServletRequest(HttpServletRequest req) {

              this.request=req;

       }

       public void setServletResponse(HttpServletResponse res) {

              this.response=res;

       }

       public void setServletContext(ServletContext ser) {

              this.servletContext=ser;

       }

}

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]