SSH-Seesion中存放数据

将数据信息放到Seesion中有三种方式'。通过实例,分别说明一下。现有登陆的类,在其方法中实现了,将用户信息放到Session中。

方法一:通过ActionContext访问Servlet API,此种方式没有入侵性

package com.bjpowernode.struts2;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements Action  {
	public String username;
	public 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;
	}
	
	public String execute()throws Exception{
		if("admin".equals(username) && "admin".equals(password)){
			//将登陆信息设置到session中
			ActionContext.getContext().getSession().put("user",username);
			
			//采用如下方式访问request对象
			//ActionContext.getContext().put(key,value);
			
			//采用如下方式访问application对象
			//ActionContext.getContext().getApplication().put(key,value);
			
			//通过request.getParameter()取得数据
			//String username = ActionContext.getContext().getParameters().get("username");
			
			return SUCCESS;
		}else{
			
			return ERROR;
		}
	}
}
package com.bjpowernode.struts2;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class MustLoginAction implements Action {

	public String execute() throws Exception {
                //从Session中读取用户信息
		//getSession()得到的是Map
		if(ActionContext.getContext().getSession().get("user")==null){
			//重定向到登录页面
			return LOGIN;
		}
		return SUCCESS;
	}

}

方法二:通过实现装配接口,完成对Servlet API的访问

package com.bjpowernode.struts2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.Action;

public class LoginAction implements Action,ServletRequestAware,ServletResponseAware  {
	public String username;
	public String password;
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	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;
	}
	
	public String execute()throws Exception{
		if("admin".equals(username) && "admin".equals(password)){
			request.getSession().setAttribute("user", username);
			return SUCCESS;
		}else{
			
			return ERROR;
		}
	}
	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
}

package com.bjpowernode.struts2;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.Action;

public class MustLoginAction implements Action,ServletRequestAware {

	private HttpServletRequest request;
	
	public String execute() throws Exception {
                //从Session中取得用户信息
		if(request.getSession().getAttribute("user")==null){
			//重定向到登录页面
			return LOGIN;
		}
		return SUCCESS;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}

方法三:
package com.bjpowernode.struts2;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class LoginAction implements Action  {
	public String username;
	public 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;
	}
	
	public String execute()throws Exception{
		if("admin".equals(username) && "admin".equals(password)){
			ServletActionContext.getRequest().getSession().setAttribute("user", username);
			return SUCCESS;
		}else{
			
			return ERROR;
		}
	}
}

package com.bjpowernode.struts2;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class MustLoginAction implements Action {

	public String execute() throws Exception {
		if(ServletActionContext.getRequest().getSession().getAttribute("user")==null){
			//重定向到登录页面
			return LOGIN;
		}
		return SUCCESS;
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值