Struts 2 accessing servlet sharing


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”;




Copy codeThe code is as follows:

    ${} <br>





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();


       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) {



       public void setServletResponse(HttpServletResponse res) {



       public void setServletContext(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 […]