本文主要是介绍struts2--获取request、session、application、HttpRequest、HttpSession、ServletContext,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
request、session、application、HttpRequest、HttpSession、ServletContext
四种方法:只使用第二种方法,并且request和application不经常用到,另外,
(1)使用struts2容器提供的ActionContext:(前三者)依赖于容器
<span style="font-size:14px;">package com.struts2.user.action;import java.util.Map;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;/*** @author Demetria* 这种获取web元素的方式是依赖于struts2框架的,因为需要使用ActionContext,这是struts2提供的。*/
public class LoginActionDemo1 extends ActionSupport{private Map request;private Map session;private Map application;public LoginActionDemo1() {//非单例模式,是thread local模式,即以当前线程名作为key值的键值对,在一个线程中是单例的。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;}
}
</span>
(2)使用IoC(控制反转)/DI(依赖注入)(前三者)IoC
实现RequestAware/SessionAware/ApplicationAware
package com.struts2.user.action;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.ActionSupport;/*** @author Demetria* 第二种获取web元素的方式:控制反转(IoC)或依赖注入(DI)* 第一种方式是通过get方法来自己获取* 第二种方式思想:当action实现了RequestAware,SessionAware,ApplicationAware接口后,容器给这个action注入这三个web元素。* request属性依赖于struts2,容器把这个值注入给我,不像第一种方式,自己去get的方式。* 本来这些元素值时自己控制的,但是现在这些值是由容器来控制的,所以控制反转了。* * 以后只用IoC方式。* 将来一般不用request ,因为value stack中的东西都放在了request域中。* 也不用application。*/
public class LoginActionDemo2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{private Map<String,Object> request;private Map<String,Object> session;private Map<String,Object> application;public String execute(){request.put("r1", "r1");session.put("s1", "s1");application.put("a1", "a1");return SUCCESS;}public void setApplication(Map<String, Object> application) {// TODO Auto-generated method stubthis.application=application;}public void setSession(Map<String, Object> session) {// TODO Auto-generated method stubthis.session=session;}public void setRequest(Map<String, Object> request) {// TODO Auto-generated method stubthis.request=request;}}
(3)使用ServletActionContext获得HttpServletRequest对象,进而获得HttpSession、ServletContext对象(后三者)依赖于容器
<span style="font-size:18px;">package com.struts2.user.action;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/*** @author Demetria* 第三种方式:* 使用ServletActionContext获取HttpServletrequest对象,* 然后使用request.getSession()获得HttpSession对象,* 然后使用session.getServletContext()获得ServletContext对象。*/
public class LoginActionDemo3 extends ActionSupport{private HttpServletRequest request;private HttpSession session;private ServletContext application;public LoginActionDemo3(){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;}
}</span>
(4)实现ServletRequestAware接口,
获得HttpServletRequest对象,进而获得HttpSession、ServletContext对象(后三者)依赖于IoC
package com.struts2.user.action;
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.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Demetria
* 第四种方式:
* action实现ServletRequestAware接口,实现setServletRequest(HttpServletRequest request)得到HttpServletRequest对象。
* 然后使用request.getSession()获得HttpSession对象,
* 然后使用session.getServletContext()获得ServletContext对象。
*/
public class LoginActionDemo4 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;
}
public void setServletRequest(HttpServletRequest request) {
this.request=request;
this.session=request.getSession();
this.application=session.getServletContext();
}
}
结果页面:
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>user_login1</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h1>user_login1</h1><s:property value="#request.r1"/>|<%=request.getAttribute("r1") %><br /><!-- 使用#号来访问web元素 --><s:property value="#session.s1"/>|<%=session.getAttribute("s1") %><br /><s:property value="#application.a1"/>|<%=application.getAttribute("a1") %><br /><s:debug></s:debug></body>
</html>
</span>
在jsp页面中,需要使用#号来访问。
这篇关于struts2--获取request、session、application、HttpRequest、HttpSession、ServletContext的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!