java后台代码常备记录总结

一些后台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;
}









  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txp1993

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值