您的位置:首页 > 编程语言 > Java开发

java后台代码常备记录总结

2015-01-30 11:54 295 查看
一些后台java代码应用中常用到的部分知识,记录下来,以备后用:

1.java获取request对象:

HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
HttpServletRequest request = ServletActionContext.getRequest();


2.java获取session对象:

ServletActionContext.getRequest().getSession()


3.java通过java.util.UUID类获取uuid:

public static String getUUId() {
UUID uuid = UUID.randomUUID();
String uid = uuid.toString().replace("-", "");
return uid;
}


4.判断一个字符串不为空或null:

StringUtils.isNullOrEmpty(value.toString())


5.java获取response:

HttpServletResponse response = ServletActionContext.getResponse();


6.java获取application.properties里的变量值:

String dbType = PropertiesBean.getInstance().getProperty("conf.rightdata.type");


7.java中获取一个实体的类的类型:

Class.forName("com.dhcc.dfis.entity.className");


8.java中将一个字符串替换成一个字符串在连接一个字符串:

String relation_id = new String(mainTableName).replace("t_", "").concat("_id");


9.java中执行hql的更新和删除语句:

super.updateByHqlWithFreeParam(hql,value);


10.java后台乱码问题:

String endText = new String(startText.getBytes("ISO8859-1"), "UTF-8");


11.通过oracle数据库链接url截取ip和sid的方法:

//截取ip的方法
public static String getIpByUrl(String url){
Pattern p = Pattern.compile("@.*?:");
Matcher m = p.matcher(url);
String ipStr = "";
if(m.find()){
ipStr = url.substring(m.start()+1,m.end()-1);
}
return ipStr;
}

//截取sid的方法
public static String getSidByUrl(String url){
String sid="";
if(url!=null&&!url.equals("")){
sid=url.substring(url.lastIndexOf(":")+1,url.length());
}
return sid;
}


12.通过sql语句查询oracle或mysql中指定表的所有字段和字段描述信息

mysql:
select COLUMN_NAME from information_schema.COLUMNS where table_name = 'your_table_name';                                                                  
select column_name from information_schema.columns where table_schema='your_db_name' and  table_name='your_table_name';





oracle:
select a.TABLE_NAME,a.COMMENTS,b.COLUMN_NAME,b.COMMENTS from USER_TAB_COMMENTS a,USER_COL_COMMENTS b where a.table_name=b.table_name and a.table_name="your_table_name";



13.c3p0获取连接池:

private static com.mchange.v2.c3p0.ComboPooledDataSource connectionSource = SpringContextHolder.getBean("dataSource");


14.前台js中获取应用根路径方法

/**
* 获得应用的根路径
*/
function getContextPath(){
var strFullPath=window.document.location.href;
var strPath=window.document.location.pathname;
var pos=strFullPath.indexOf(strPath);
var prePath=strFullPath.substring(0,pos);
var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
var basePath = prePath;
//if(canBeAccess(prePath + postPath)){
/**
*
*/
basePath = prePath + postPath;
//}
return basePath;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: