📄 baseparam.java
字号:
package jaction.datapool;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import jaction.utility.*;
import java.io.Serializable;
/**
* 数据封装类<br>
* 包含数据空间,存放相应的数据<br>
* @author Tony
* @version 1.0
* <p>将数据空间类型Hashtable 改造为HashMap,目的:提高处理速度 <br>modify by Weigang 2003-4-22 10:22
* <p>将数据空间类型HashMap 改造为Map,目的:解决中文问题 <br>modify by Tony 2003-4-24 10:23
* <p>clean by yanger 2003-4-24 11:05
*/
public class BaseParam extends Object implements Serializable {
/**
* 数据存储空间
*/
//HashMap buffer;
Map buffer;
/**
* 数据请求接口
*/
HttpServletRequest httpServletRequest;
/**
* 数据响应接口
*/
HttpServletResponse httpServletResponse;
///////////////////////////////////////////////////////////////////////////////
///////////////// 构造方法 /////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* 构造函数
* @param request 数据请求对象
* @param response 数据返回对象
*/
public BaseParam(HttpServletRequest request, HttpServletResponse response){
buffer = getParametersMapFromRequest(request);
httpServletRequest = request;
httpServletResponse = response;
}
/**
* 构造函数
*/
public BaseParam(){
//buffer = new HashMap();
buffer = Collections.synchronizedMap(new HashMap());
}
/**
* 初始化
* @param request 数据请求对象
* @param response 数据返回对
*/
public void init(HttpServletRequest request, HttpServletResponse response){
buffer = getParametersMapFromRequest(request);
}
/**
* 将buffer中的内容打印到日志文件
*/
public void log(){
Iterator iterator = buffer.keySet().iterator();
while(iterator.hasNext()){
String id = (String)iterator.next();
//一维数组处理
if(buffer.get(id) instanceof String[]){
String[] array=(String[])buffer.get(id);
for(int i=0;i<array.length;i++){
SysLogger.infoLog(id+"["+i+"]= "+array[i]);
}
//字符串处理
}else if(buffer.get(id) instanceof String){
String s =(String)buffer.get(id);
SysLogger.infoLog(id+"= "+s);
//二维数组处理
}else if(buffer.get(id) instanceof String[][]){
String[][] array2 = (String[][])buffer.get(id);
for(int i=0;i<array2.length;i++){
for(int j=0;j<array2[i].length;j++){
SysLogger.infoLog(id+"["+i+"]["+j+"]= "+array2[i][j]);
}
}
//Vector 处理
}else if(buffer.get(id) instanceof Vector){
Vector v = (Vector)buffer.get(id);
for(int i=0;i<v.size();i++){
SysLogger.infoLog(id+"["+i+"]= "+v.elementAt(i));
}
//HashMap处理
}else if(buffer.get(id) instanceof HashMap){
HashMap newHash = (HashMap)buffer.get(id);
int i=0;
Iterator it = newHash.keySet().iterator();
while(it.hasNext()){
String id2 = (String)it.next();
SysLogger.infoLog(id2+"["+i+"]= "+newHash.get(id2));
i++;
}
}//Result 对象处理
/*else if(hash.get(id) instanceof Result){
Result rs = (Result)hash.get(id);
try{
String[][] array2 = rs.toStringArrayNonull();
for(int i=0;i<array2.length;i++){
for(int j=0;j<array2[i].length;j++){
SysLogger.infoLog(id+"["+i+"]["+j+"]= "+array2[i][j]);
}
}
}catch(Exception ex){
SysLogger.error("BaseParam","log",ex.toString());
}
}*/
else{
SysLogger.infoLog("id= "+id+" value= "+buffer.get(id));
}
}
}
/**
* 将输入项打包成Map,把request中的输入字段名作为id,把输入的值作为Value的Hashtable
* @param request 数据请求对象
* @return Map 数据整理散列表
* @Since 1.4 $yanger 2002-11-1 13:16:17
*/
//modify by yanger 2002-11-1
private Map getParametersMapFromRequest(HttpServletRequest request){
return RequestUtil.getAllParameters(request);
}
///////////////////////////////////////////////////////////////////////////////
///////////////// 添加数据 /////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* 添加数据项
* @param id 从数据字典中得到的关键字
* @param obj 从数据字典中得到的数据
*/
public void addItem(String id,Object obj){
buffer.put(id,obj);
}
/**
* 添加数据项
* @param id 唯一标识符
* @param str 字符串
*/
public void addItem(String id,String str){
buffer.put(id,str);
}
/**
* 添加数据项
* @param id 唯一标识符
* @param strs 字符串数组
*/
public void addItem(String id,String[] strs){
buffer.put(id,strs);
}
/**
* 添加数据项
* @param id 唯一标识符
* @param str2s 字符串二维数组
*/
public void addItem(String id,String[][] str2s){
buffer.put(id,str2s);
}
///////////////////////////////////////////////////////////////////////////////
///////////////// 取值方法 /////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* 取得输入项
* @param id Id标识符
* @return 数据格式为object类型的数据
*/
public Object getObject(String id) throws Exception{
if(!isExist(id))
return null;
//throw new Exception("没有数据项"+id);
return(buffer.get(id));
}
/**
* 取得输入项,并转换为字符串
* @param id Id标识符
* @return 数据格式为String类型的数据
*/
public String getString(String id) throws Exception{
if(!isExist(id))
return null;
//throw new Exception("没有数据项"+id);
if(buffer.get(id) instanceof String[]){
String[] str = (String[])buffer.get(id);
if(str.length==1){
return str[0];
}else{
return null;
}
}
return((String)buffer.get(id));
}
/**
* 取得输入项,并转换为int型
* @param id Id标识符
* @return 数据格式为int类型的数据
*/
public int getInt(String id) throws Exception{
try{
if(!isExist(id))
return -1;
//throw new Exception("没有数据项"+id);
if(buffer.get(id) instanceof String[]){
String[] str = (String[])buffer.get(id);
if(str[0].trim().equals(""))
return -1;
//add by yanger 2003-1-9
if(str.length==1){
return Integer.parseInt(str[0]);
}else{
return -1;
}
}
return(Integer.parseInt((String)buffer.get(id)));
}catch(Exception e){
throw new Exception(id+":类型错误,请检查"+e);
}
}
/**
* 取得输入项,并转换为double类型
* @param id Id标识符
* @return 数据格式为double类型的数据
*/
public double getDouble(String id) throws Exception{
try{
if(!isExist(id))
return 0.0;
//throw new Exception("没有数据项"+id);
if(buffer.get(id) instanceof String[]){
String[] str = (String[])buffer.get(id);
if(str[0].trim().equals(""))
return 0.0;
//add by yanger 2003-1-9
if(str.length==1){
return Double.parseDouble(str[0]);
}else{
return 0.0;
}
}
return(Double.parseDouble((String)buffer.get(id)));
}catch(Exception e){
throw new Exception(id+":类型错误,请检查"+e);
}
}
/**
* 取得输入项,并转换为字符串数组
* @param id Id标识符
* @return 数据格式为String[]类型的数据
*/
public String[] getArray(String id) throws Exception{
if(!isExist(id))
//throw new Exception("没有数据项"+id);
return null;
//SysLogger.infoLog("getInt :id="+id+" int="+buffer.get(id));
return((String[])buffer.get(id));
}
/**
* 取得输入项,并转换为字符串二维数组
* @param id Id标识符
* @return 数据格式为String[][]类型的数据
*/
public String[][] get2Array(String id) throws Exception{
if(!isExist(id))
//throw new Exception("没有数据项"+id);
return null;
return((String[][])buffer.get(id));
}
///////////////////////////////////////////////////////////////////////////////
///////////////// 更新数据 /////////////////
///////////////////////////////////////////////////////////////////////////////
///与添加方法类似
/**
* 更新数据项的内容
* @param String key
* @param String 要更新的值
*/
public void updateItem(String key,String value) throws Exception{
//buffer.remove(key);//modify by yanger2002-12-30
buffer.put(key,value);
}
/**
* 更新数据项的内容
* @param String 关键字
* @param Object 要更新的对象
*/
public void updateItem(String key,Object value) throws Exception{
buffer.put(key,value);
}
///////////////////////////////////////////////////////////////////////////////
///////////////// 其他方法 /////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* 判断是否存在数据项
* @param id Id标识符
* @param true 表示有,flase表示没有
*/
//modify by yanger 2002-11-1
public boolean isExist(String id) throws Exception{
if ( buffer.containsKey ( id ) )
return ( true );
else
return ( false );
}
/**
* 得到参数的个数
* @return 参数的个数
*/
public int getParamCount(){
return buffer.size();
}
/**
* 转换成map
* @return Map 散列表
*/
public Map toMap(){
return buffer;
}
///////////////////////////////////////////////////////////////////////////////
///////////////// request & response方法 /////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* 将value of name 放入 session
*/
public void setAttributeToHttpSession(String name,Object value){
HttpSession httpSession = httpServletRequest.getSession() ;
httpSession.setAttribute(name,value);
}
/**
* 将session置为无效
*/
public void invalidateHttpSession(){
HttpSession httpSession = httpServletRequest.getSession() ;
httpSession.invalidate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -