Struts2 four ways to access web elements (request,session,application)

Source: Internet
Author: User
Tags xmlns

The action does not directly access request,session and application, and how to get the Web element. There are four ways to note: The second way is common.

Struts2 encapsulates HttpRequest as an object of type map, and can obtain a Web element by acquiring the object, or a real reference to Httpservletrequest,httpsession,servletcontext.

The first two ways to obtain the STRUTS2 encapsulated map type of object, the latter two ways to obtain a real reference to the Httpservletrequest,httpsession,servletcontext.

The first and third methods depend on the container, obtained according to the provided member variable, the second and fourth Way is the IOC mode, which is automatically set by the container.

The first type:

Actioncontext.getcontext (). Get ("request");

Actioncontext.getcontext (). getsession;

Actioncontext.getcontext (). Getapplication ();

Import Java.util.Map;

Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;

public class LoginAction1 extends Actionsupport {
	
	private Map request;
	Private Map session;
	Private MAP Application;
	
	Public LoginAction1 () {
		request = (MAP) actioncontext.getcontext (). Get ("request");
		Session = Actioncontext.getcontext (). getsession ();
		application = Actioncontext.getcontext (). Getapplication ();
	}
	
	Public String Execute () {
		request.put ("R1", "R1");
		Session.put ("S1", "S1");
		Application.put ("A1", "A1");
		return SUCCESS; 
	}
	
}


The second kind: Implement Requestaware,sessionaware, Applicationaware interface and override set method, automatically set the value to it by the container

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 LoginAction2 extends Actionsupport implements Requestaware,sessionaware, Applicationaware {private map&lt ;
	String, object> request;
	Private Map<string, object> session;
	
	Private map<string, object> application;
		DI Dependency Injection//IOC inverse of control public String execute () {request.put ("R1", "R1");
		Session.put ("S1", "S1");
		Application.put ("A1", "A1"); 
	return SUCCESS;
	} @Override public void Setrequest (map<string, object> request) {this.request = Request;
	} @Override public void Setsession (Map<string, object> session) {this.session = session; } @Override public void Setapplication (map<string, object> application) {This. application = Application; }
	
	
}


The third type:

Servletactioncontext.getrequest ();

Servletactioncontext.getrequest (). GetSession ();

Servletactioncontext.getrequest (). Getservletcontext ();

Import Javax.servlet.ServletContext;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpSession;

Import Org.apache.struts2.ServletActionContext;

Import Com.opensymphony.xwork2.ActionSupport;

public class LoginAction3 extends Actionsupport {
	
	private httpservletrequest request;
	Private HttpSession session;
	Private ServletContext Application;
	
	Public LoginAction3 () {
		request = Servletactioncontext.getrequest ();
		Session = Request.getsession ();
		application = Session.getservletcontext ();
	}
	
	Public String Execute () {
		request.setattribute ("R1", "R1");
		Session.setattribute ("S1", "S1");
		Application.setattribute ("A1", "A1");
		return SUCCESS; 
	}
	
}


Fourth: Implement the Servletrequestaware interface and override the Setservletrequest () method

Import Javax.servlet.ServletContext;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpSession;

Import Org.apache.struts2.interceptor.ServletRequestAware;

Import Com.opensymphony.xwork2.ActionSupport;

public class LoginAction4 extends Actionsupport implements Servletrequestaware {
	
	private httpservletrequest request ;
	Private HttpSession session;
	Private ServletContext Application;
	
	
	
	Public String Execute () {
		request.setattribute ("R1", "R1");
		Session.setattribute ("S1", "S1");
		Application.setattribute ("A1", "A1");
		return SUCCESS; 
	}



	@Override public
	void Setservletrequest (HttpServletRequest request) {
		this.request = Request;
		This.session = Request.getsession ();
		This.application = Session.getservletcontext ();
	}
	
}


Action page

<?xml version= "1.0" encoding= "GB18030"?> <%@ page language= "java" contenttype= "text/html; charset=gb18030 "pageencoding=" GB18030 "%> <% String context = Request.getcontextpath (); %> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  


Show Page

<?xml version= "1.0" encoding= "GB18030"?>
<%@ page language= "java" contenttype= "text/html; charset= GB18030 "
    pageencoding=" GB18030 "%>
    <% @taglib uri="/struts-tags "prefix=" s "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">



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.