<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
添加测试的web资源
<%
application.setAttribute("app1", "测试的application") ;
application.setAttribute("app12", "测试的application2") ;
session.setAttribute("ses1", "测试的Session") ; request.setAttribute("req1", "测试的request") ; %>
<br>
<a href="testAction.action?user=tom" >测试Action类访问web资源</a>
<br><br><br>
<a href="testActionAware.action?user=tom&user=rose" >以Aware方式测试Action类访问web资源</a>
<br><br><br>
<a href="testServletAction.action?user=tom" >访问原生web资源</a>
<br><br><br>
<a href="testServletAware.action?user=tom" >以接口方式访问原生web资源</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
显示application的属性
<br>
app1=${app1 }
<br>
app12=${app12 }
<br>
appx=${applicationScope.appx } <br><br> 显示Session的属性
<br><br> ses1=${ses1 }
<br><br>
ses2=${ses2 } <br><br>
req1=${req1 }
<br><br>
req2=${req2 }
<br><br>
<%
String user = request.getParameter("user") ;
Object haha = session.getAttribute("ses1") ;
out.print("user = "+user) ;
out.print("<br><br> ") ;
out.print("haha = "+haha.toString()) ;
String ww = request.getParameter("ww") ;
%>
<br>
<%out.print("ww= "+ww) ; %>
</body>
</html>
Struts
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 设置过滤的扩展名 -->
<constant name="struts.action.extension" value="do,action,,"></constant> <!-- 定义包 -->
<package name="index" extends="struts-default" namespace=""> <action name="testAction" class="com.hanqi.action.TestAction" method="testWeb"> <result type="redirect">test1.jsp</result> </action> <!-- 解耦的接口方式 -->
<action name="testActionAware" class="com.hanqi.TestActionAware" method="testAware"> <result >test1.jsp</result> </action> <!-- 以ServletActionContext方式 -->
<action name="testServletAction" class="com.hanqi.action.TestServletAction"
method="testServletAction"> <result >test1.jsp</result> </action> <action name="testServletAware" class="com.hanqi.action.TestServletActionAware"
method="testServletAware"> <result >test1.jsp</result> </action> </package> </struts>
package com.hanqi.action; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware; public class TestServletActionAware
implements ServletRequestAware, ServletContextAware { private ServletContext sc ;
private HttpServletRequest hsr ;
private HttpSession hs ; @Override
public void setServletContext(ServletContext arg0) {
// TODO 自动生成的方法存根
sc = arg0 ;
} @Override
public void setServletRequest(HttpServletRequest arg0) {
// TODO 自动生成的方法存根 hsr = arg0 ; hs = arg0.getSession() ; } public String testServletAware()
{
System.out.println("app1 = " + sc.getAttribute("app1")); sc.setAttribute("appx", "新的app"); System.out.println("ses1 = " + hs.getAttribute("ses1")); hs.setAttribute("ses2", "新的ses2"); hsr.setAttribute("req2", "新的req2"); System.out.println("user = " +hsr.getParameter("user")); return "success" ;
} }
package com.hanqi.action; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; public class TestServletAction
{ public String testServletAction()
{
//访问原生的web资源 //1得到ServletContext,相当于application
ServletContext sc = ServletActionContext.getServletContext(); System.out.println("app1 = " + sc.getAttribute("app1")); sc.setAttribute("appx", "原生方式放入的内容"); //2访问Session
HttpServletRequest hsr = ServletActionContext.getRequest() ; HttpSession hs = hsr.getSession() ; System.out.println("ses1 = " + hs.getAttribute("ses1")); hs.setAttribute("ses2", "原生方式放入的Session"); //3访问request
System.out.println("req1 = " + hsr.getAttribute("req1")); hsr.setAttribute("req2", "原生方式放入的request"); //4访问parameter
System.out.println("user1 = " +hsr.getParameter("user") );
System.out.println("user2 = " +hsr.getParameter("user") ); return "success" ;
}
}
package com.hanqi.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; //解耦方式
public class TestAction
{ // public String testWeb()
{
System.out.println("hah"); //单例
//Action实例的上下文对象
ActionContext ac = ActionContext.getContext() ; //1获取application资源
Map<String, Object> mapApp = ac.getApplication() ; System.out.println("app1 = " + mapApp.get("app1")); System.out.println("app12 = " + mapApp.get("app12")); //放入新的键值对
mapApp.put("appx", "新放入的application") ; //2.获取Session的资源
Map<String, Object> mapses = ac.getSession() ; System.out.println("ses1=" + mapses.get("ses1")); //放入新的键值对
mapses.put("ses2", "新放入的session") ; //移除
mapses.remove("ses1") ; //3获取request资源
Map<String, Object> mapreq = (Map<String, Object>) ac.get("request"); System.out.println("req1 = " + mapreq.get("req1")); //放入新的键值对
mapreq.put("req2", "新放入的request") ; //4直接获取传递的参数
Map<String, Object> mappar = ac.getParameters() ; //参数值被封装成String数组
String[ ] array = (String[ ])mappar.get("user") ; for(String s : array)
{
System.out.println("user = " + s);
} //放入
mappar.put("ww", new String[ ]{("qweqeq")}) ; return "success" ;
} }
package com.hanqi; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware; public class TestActionAware implements ApplicationAware , SessionAware, RequestAware, ParameterAware{ private Map<String, Object> mapApp ;
private Map<String, Object> mapses ;
private Map<String, Object> mapreq ;
private Map<String, String[ ]> mappar ; //实现ApplicationAware接口的方法
//提供给Struts2进行调用 , 放入Application的数据集合
@Override
public void setApplication(Map<String, Object> arg0) {
// TODO 自动生成的方法存根 //接收
mapApp = arg0 ; } public String testAware()
{ //1访问application
System.out.println("app1 = " + mapApp.get("app1")); mapApp.put("app12", "以Aware方式放入的application") ; //2访问Session
//System.out.println("ses1 = " + mapses.get("ses1")); mapses.put("ses2", "以Aware方式放入的Session") ; //3访问request
System.out.println("req1 = " + mapreq.get("req1")); mapreq.put("req2", "以Aware方式放入的request") ; //4 访问Parameter
String[ ] str = mappar.get("user") ;
for(String t : str)
{
System.out.println("user = " + t);
}
return "success" ;
} @Override
public void setSession(Map<String, Object> arg0) {
// TODO 自动生成的方法存根 mapses = arg0 ; } @Override
public void setRequest(Map<String, Object> arg0) {
// TODO 自动生成的方法存根
mapreq = arg0 ;
} @Override
public void setParameters(Map<String, String[]> arg0) {
// TODO 自动生成的方法存根
mappar = arg0 ;
} }