`

javaWeb小功能点杂记

 
阅读更多

1、获取项目文件路劲

public class UserControl {
	@Autowired
	private UserService userService;
	
	/**
	 * 登陆
	 */
	@RequestMapping(value = "/login")
	public String login(HttpServletRequest request,HttpServletResponse response) { 
		public String login(HttpServletRequest request,HttpServletResponse response) {
		//web项目的classpath绝对路劲
		URL rrr = Thread.currentThread().getContextClassLoader().getResource("");
		System.out.println("url=" + rrr);
		//输出结果:url=file:/E:/tomcat.6.0.37/webapps/easyUiPowerManage/WEB-INF/classes/
		
		//web项目的classpath绝对路劲
		URL rrr1 = Thread.currentThread().getContextClassLoader().getResource("/");
		System.out.println("url=" + rrr1);
		//输出结果:url=file:/E:/tomcat.6.0.37/webapps/easyUiPowerManage/WEB-INF/classes/
		
		//web项目的classpath绝对路劲
		URL rrr3 = UserControl.class.getResource("/");
		System.out.println("url=" + rrr3);
		//输出结果:url=file:/E:/tomcat.6.0.37/webapps/easyUiPowerManage/WEB-INF/classes/
		
		//UserControl类相对classpath绝对路劲
		URL rrr4 = UserControl.class.getResource("");
		System.out.println("url=" + rrr4);
		//输出结果:url=file:/E:/tomcat.6.0.37/webapps/easyUiPowerManage/WEB-INF/classes/com/control/
	}
}

 

2、获取服务器跟路径

String path = System.getProperty("catalina.home");
System.out.println(path);

    这里输出的是tomcat的路劲:E:\tomcat.6.0.37,并且需要服务启动重页面跳转到后台才行。

 

3、根据系统不同获取系统分隔符

File.separator

    如:win系统为"/"

 

4、将request参数装换为map

protected void initResponseMap(HttpServletRequest request, Map<String, String> respMap) {
        Enumeration<?> temp = request.getParameterNames();
        if (null != temp) {
            while (temp.hasMoreElements()) {
                String en = (String) temp.nextElement();
                String value = request.getParameter(en);
                respMap.put(en, value);
            }
        }
        logger.info("initResponseMap" + JSON.toJSONString(respMap));
    }

 

5、打印request参数所有请求参数和值

Map map = new HashMap();  
Enumeration paramNames = request.getParameterNames();  
while (paramNames.hasMoreElements()) {  
	String paramName = (String) paramNames.nextElement();  

	String[] paramValues = request.getParameterValues(paramName);  
	if (paramValues.length == 1) {  
		String paramValue = paramValues[0];  
		if (paramValue.length() != 0) {  
			map.put(paramName, paramValue);  
		}  
	}  
}  

Set<Map.Entry<String, String>> set = map.entrySet();  
System.out.println("------------------------------");  
for (Map.Entry entry : set) {  
	System.out.println(entry.getKey() + ":" + entry.getValue());  
}  
System.out.println("------------------------------");

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics