package com.util;
import javax.servlet.http.HttpServletRequest;
import com.app.permission.exception.UserNotLogonException;
import com.app.permission.model.Department;
import com.app.permission.model.User;
/**
* session管理
* @author Administrator
*
*/
public class SessionManager {
/**
* 取得int值
* @param request
* @param s 表单field名称
* @param defaultValue 默认值
* @param region session保存区域
* @param postition 相对位向量位置
* @return
* @throws Exception
*/
public static int getIntFromSession(HttpServletRequest request, String s,int defaultValue,String region,int postition) throws Exception{
String bitset = SessionManager.getBitsetInfo(request);
boolean isReset = SessionManager.isReset(request,bitset,postition);
if(!isReset){
Integer i = (Integer)SessionManager.getAttribute(request,region+"_"+s);
if(i==null){
isReset = true;
}else{
return i.intValue();
}
}
if(isReset){
int value = ParamUtils.getInt(request,s,defaultValue);
SessionManager.setAttribute(request,region+"_"+s,new Integer(value));
return value;
}
return defaultValue;
}
http://www.javalearns.com/
public static Integer getIntegerFromSession(HttpServletRequest request, String s,int defaultValue,String region,int postition) throws Exception{
return new Integer(SessionManager.getIntFromSession(request,s,defaultValue,region,postition));
}
public static String getStringFromSession(HttpServletRequest request, String s,String defaultValue,String region,int postition) throws Exception{
String bitset = SessionManager.getBitsetInfo(request);
boolean isReset = SessionManager.isReset(request,bitset,postition);
if(!isReset){
String i = (String)SessionManager.getAttribute(request,region+"_"+s);
if(i==null){
isReset = true;
}else{
return i;
}
}
if(isReset){
String value = ParamUtils.getString(request,s,defaultValue);
SessionManager.setAttribute(request,region+"_"+s,value);
return value;
}
return defaultValue;
}
/**
* 判断是否重置
* 假如来源不明,或者从主界面访问,session信息重置
* @param request
* @param regionName session区域
*/
private static boolean isReset(HttpServletRequest request,String bitset,int postition){
boolean reset = false;
String url = request.getHeader("Referer");
if(url==null || "".equals(url)){
reset = true;
}else if(url.toLowerCase().indexOf(".jsp")!=-1 || url.toLowerCase().indexOf("menuplan.do")!=-1){
reset = true;
}
if(!reset){
StepUtils su = new StepUtils();
su.setStep(bitset);
reset = su.test(postition);
}
return reset;
}
/**
* 为页码参数,判断是否重置
* @param request
* @return
*/
private static boolean isResetForPateNo(HttpServletRequest request, String pageset){
boolean reset = false;
String url = request.getHeader("Referer");
if(url==null || "".equals(url)){
reset = false;
}else if(url.toLowerCase().indexOf(".jsp")!=-1 || url.toLowerCase().indexOf("menuplan.do")!=-1 ){
reset = true;
}else if("1".equals(pageset)){
reset = true;
}
return reset;
}
/**
* 取得位向量信息
* @param request
* @return
* @throws Exception
*/
public static String getBitsetInfo(HttpServletRequest request) throws Exception {
return ParamUtils.getString(request,"bitset","0000000000");
}
/**
* 添加session
* @param request
* @param sessionName
* @param object
*/
public static void setAttribute(HttpServletRequest request,String sessionName,Object object){
request.getSession().setAttribute(sessionName,object);
}
/**
* 取得session
* @param request
* @param sessionName session名称
* @return
*/
public static Object getAttribute(HttpServletRequest request,String sessionName){
return request.getSession().getAttribute(sessionName);
}
/**
* 删除session
* @param request
* @param sessionName session名称
*/
public static void removeAttribute(HttpServletRequest request,String sessionName){
request.getSession().removeAttribute(sessionName);
}
/**
* 从session中取得用户信息
* @param request
* @return
*/
//TODO 需要实现
public static User getUserInfo(HttpServletRequest request) throws Exception{
if (SessionManager.getAttribute(request,"user") == null){
throw new UserNotLogonException("您没有登录,或长时间未操作,请重新登录系统!");
}
User user = (User)SessionManager.getAttribute(request,"user");
return user;
}
public static Department getShowDepartment(HttpServletRequest request) throws Exception{
Department department = getUserInfo(request).getDepartment();
if(department.getDepartment()!=null){
department = department.getDepartmentForPage();
}
return department;
}
http://www.javalearns.com/
public static String getPageNoFromSession(HttpServletRequest request, String region) throws Exception{
String pageset = SessionManager.getPageSetInfo(request);
boolean isReset = SessionManager.isResetForPateNo(request,pageset);
if(!isReset){
String i = (String)SessionManager.getAttribute(request,region);
if(i==null){
isReset = true;
}else{
return i;
}
}
if(isReset){
SessionManager.setAttribute(request,region,"0");
return "0";
}
return "0";
}
public static int getDefaultPageNoFromSession(HttpServletRequest request, String region)throws Exception{
int pageNo = ParamUtils.getInt(request,"pageNo",0);
String s = SessionManager.getPageNoFromSession(request,region+"pageNo1");
try{
pageNo = Integer.parseInt(s);
}catch(Exception e){
}
if(pageNo==0){
pageNo = 1;
}
SessionManager.setAttribute(request, region+"pageNo1", pageNo+"");
return pageNo;
}
public static String getPageSetInfo(HttpServletRequest request) throws Exception {
return ParamUtils.getString(request,"pageset","0");
}
}
文章转载自 http://www.javalearns.com/Html/?1534.html