jsp请求
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'index.jsp' starting page</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>
取得Map类型request,session,application,真实类型 HttpServletRequest, HttpSession, ServletContext的引用:
<ol>
<li>前三者:依赖于容器</li>
<li>前三者:IOC</li> (只用这种)
<li>后三者:依赖于容器</li>
<li>后三者:IOC</li>
</ol>
<br />
<form name="f" action="" method="post">
用户名:<input type="text" name="name"/>
密码:<input type="text" name="password"/>
<br />
<input type="button" value="submit1" οnclick="javascript:document.f.action='login/login1';document.f.submit();" />
<input type="button" value="submit2" οnclick="javascript:document.f.action='login/login2';document.f.submit();" />
<input type="button" value="submit3" οnclick="javascript:document.f.action='login/login3';document.f.submit();" />
<input type="button" value="submit4" οnclick="javascript:document.f.action='login/login4';document.f.submit();" />
</form>
</body>
</html>
struts.xml的配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> <package name="login" extends="struts-default" namespace="/login"> <action name="login*" class="com.lbx.action.LoginAction{1}"> <result>/user_login_success.jsp</result> </action> </package> </struts>
第一种直接获取,先通过(Map)ActionContext.getContext().get("request")获得request,得到request就可以获取session,获得session就可以获得application(注意都是map型的)
package com.lbx.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
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;
}
}
第二中获取方式,通过实现接口获取,提供set方法就可以获取到(map型的)
package com.lbx.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;
@SuppressWarnings("serial")
public class LoginAction2 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 setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request = request;
}
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
}
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
this.application = application;
}
}
第三种获取方式,和第一种差不多,但是是获取到一个真实的类型而不是一个map类型的容器
package com.lbx.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;
@SuppressWarnings("serial")
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;
}
}
第四种获取方式,和第三种差不多,也是通过实现接口,但是这种是获取一个真实的类型而不是一个map容器
package com.lbx.action;
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 void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
this.session = request.getSession();
application = session.getServletContext();
}
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
}