Struts2访问ServletAPI(向JSP内置对象request,session,Application传值)

JSP:         request                                 session                                   application

Servlet:HttpServletRequest            HttpSession                            ServletContext

Struts2可通过ActionContext类,ServletActionContext类和IoC(接口)方式访问这三个对象。

ActionContext类:

package com.hp.easycar.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginServletAPI1 extends ActionSupport {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public void validate() {
		if (username==null||username.trim().equals("")) {
			addFieldError("username", "输入的用户名不能为空!!");
		}
		if (password==null||password.trim().equals("")) {
			addFieldError("password", "输入的用户名不能为空!!");
		}
	}
	
	@Override
	public String execute() throws Exception {
		if (username.equals("hp")&&password.equals("hp123")) {
			//获取ActionContext对象
			<strong>ActionContext context=ActionContext.getContext();</strong>
			//在请求request中保存欢迎信息
			<strong>context.put("welcome", "欢迎您登录HP EasyCar系统!");</strong>
			//在Session中保存用户名
			<strong>context.getSession().put("username", "HP");</strong>
			//统计用户量,在Apllacation中保存
			<strong>Integer count=(Integer) context.getApplication().get("counter");</strong>
			if (count==null) {
				count=1;
			} else {
				count++;
			}
			<strong>context.getApplication().put("counter", count);</strong>
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
}

ServletActionContext类:

package com.hp.easycar.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginServletAPI2 extends ActionSupport {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public void validate() {
		if (username==null||username.trim().equals("")) {
			addFieldError("username", "输入的用户名不能为空!!");
		}
		if (password==null||password.trim().equals("")) {
			addFieldError("password", "输入的用户名不能为空!!");
		}
	}
	
	@Override
	public String execute() throws Exception {
		if (username.equals("hp")&&password.equals("hp123")) {
			//在请求request中保存欢迎信息
			<strong>ServletActionContext.getRequest().setAttribute("welcome", "欢迎您登录HP EasyCar系统!");</strong>
			//在Session中保存用户名
			<strong>ServletActionContext.getRequest().getSession().setAttribute("username", "HP");</strong>
			//统计用户量,在Apllacation中保存
			<strong>Integer count=(Integer)ServletActionContext.getServletContext().getAttribute("counter");</strong>
			if (count==null) {
				count=1;
			} else {
				count++;
			}
			<strong>ServletActionContext.getServletContext().setAttribute("counter", count);</strong>
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
}

IoC

package com.hp.easycar.action;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginServletAPI3 extends ActionSupport implements
		<strong>ServletRequestAware, SessionAware, ServletContextAware</strong> {
<strong>	private static final long serialVersionUID=1004770319661315412L;
	private HttpServletRequest request;
	private Map<String,Object> session;
	private ServletContext context;</strong>
	

	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public void validate() {
		if (username==null||username.trim().equals("")) {
			addFieldError("username", "输入的用户名不能为空!!");
		}
		if (password==null||password.trim().equals("")) {
			addFieldError("password", "输入的用户名不能为空!!");
		}
	}
	
	@Override
	public String execute() throws Exception {
		if (username.equals("hp")&&password.equals("hp123")) {
			//在请求request中保存欢迎信息
			<strong>request.setAttribute("welcome", "欢迎您登录HP EasyCar系统!");</strong>
			//在Session中保存用户名
			<strong>session.put("username", "HP");</strong>
			//统计用户量,在Apllacation中保存
			<strong>Integer count=(Integer)context.getAttribute("counter");</strong>
			if (count==null) {
				count=1;
			} else {
				count++;
			}
		<strong>	context.setAttribute("counter", count);</strong>
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	/

	<strong>@Override
	public void setServletContext(ServletContext arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub

	}</strong>

}

使用HttpServletRequest:实现ServletRequestAware接口,并重写public void setServletContext(ServletContext arg0)方法

使用HttpServletSession:实现 SessionAware,接口,并重写public void setSession(Map<String, Object> arg0)方法

使用ServletContext实现ServletContextAware接口,并重写public void setServletRequest(HttpServletRequest arg0) 方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值