How to obtain request, session, and application in Struts2

Source: Internet
Author: User

There are four methods
First:
Obtain the request, session, and application of the MAP type.
Write in a java File

package com.xjtu.st;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport{private Map request;private Map session;private Map application;public LoginAction(){request = (Map)ActionContext.getContext().get("request");session = ActionContext.getContext().getSession();application = ActionContext.getContext().getApplication();}public String execute(){request.put("rs", "rs");session.put("ss", "ss");application.put("as", "as");return SUCCESS;}}
ActionContext. getContext () is to obtain the context of the Action, and then obtain the request of the MAP type.
Parameters are available on the output page.
<% @ Page language = "java" import = "java. util. * "pageEncoding =" GBK "%> <% @ taglib uri ="/struts-tags "prefix =" s "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %>              My JSP 'myjsp. jsp 'starting page    
 
 
     
 
 
       Hello



<% = Request. getAttribute ("rs") %>
<% = Session. getAttribute ("ss") %>
<% = Application. getAttribute ("as") %>

Second:
Implement the RequestAware, SessionAware, and ApplicationAware interfaces. This method can be DI (dependency injection) or IOC (control inversion ). The Code is as follows:
package com.xjtu.st;import java.util.Map;import org.apache.struts2.interceptor.ApplicationAware;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{private Map request;private Map session;private Map application;public String execute(){request.put("rs", "rrs");session.put("ss", "sss");application.put("as", "aas");return SUCCESS;}public void setRequest(Map
 
   request) {// TODO Auto-generated method stubthis.request=request;}public void setSession(Map
  
    session) {// TODO Auto-generated method stubthis.session= session;}public void setApplication(Map
   
     application) {// TODO Auto-generated method stubthis.application = application;}}
   
  
 

The first two types are Map types, and the last two are HtteServletRequest, HttpSession, and ServletContext types. The third is:
The Code is as follows:
package com.xjtu.st;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import org.apache.struts2.interceptor.ApplicationAware;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {private HttpServletRequest request;private HttpSession session;private ServletContext application;public LoginAction(){request= ServletActionContext.getRequest();session = request.getSession();application = session.getServletContext();}public String execute(){request.setAttribute("rs", "rrrs");session.setAttribute("ss", "ssss");application.setAttribute("as", "aaas");return SUCCESS;}}
This method is not commonly used:
It is also an ioc method.
The Code is as follows:
package com.xjtu.st;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import org.apache.struts2.interceptor.ApplicationAware;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport implements ServletRequestAware{private HttpServletRequest request;private HttpSession session;private ServletContext application;public String execute(){request.setAttribute("rs", "rrrrs");session.setAttribute("ss", "sssss");application.setAttribute("as", "aaaas");return SUCCESS;}public void setServletRequest(HttpServletRequest request) {// TODO Auto-generated method stubthis.request = request;session = request.getSession();application = session.getServletContext();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.