Java 笔记:知识点积累 (2)


1. 不要将数组的获取长度的方法放到for 循环的判断里,如果在for里对该数组做了增加数据的操作,可能会引起死循环。
var resultCellNum = new Array();
      var numTemp = resultCellNum;
      //var numTempLength = numTemp.length;
      for(var i=0;i<numTemp.length;i++){
	
	.....1. .
         resultCellNum.push(i,0);
	......
      }


2. js中有对象引用。对一个对象resultCellNum操作,会引起另一个被引用对象numTemp的数据变化。

3. 页面字符集的编码. 保存页面源代码,注意选择合适的编码集,否则通过浏览器浏览页面时,易出现乱码.

4. cookie的设置和删除: 删除cookie时,指定cookie的路径、设置maxAge为0。设置cookie时,注意设置cookie的路径。
/**
     * 设置cookie
     * @param request 
     * @return
     */
    public static String setCookie(HttpServletRequest request, HttpServletResponse response, String key, String value) {
    	
    	Cookie[]   cookies=request.getCookies();      
    	//cookies不为空,则清除      
    	if(cookies!=null)      
    	{      
    		Cookie cookie;
    		for (int i = 0; i < cookies.length; i++) {
    			cookie = cookies[i];
	    	    String _value = cookie.getName();      
	    	    if(_value.equals(key))      
	    	    {      
	    	         cookie.setMaxAge(0);      
	    	         cookie.setPath(cookie.getPath());
	    	         response.addCookie(cookie);    
	    	    }  
    		}
    	}  
    	
		// 设置cookie
		int time = 60 * 60 * 24;
		// 通过构造Cookie
		Cookie cname = new Cookie(key, value); // 设置cookie
		cname.setMaxAge(60 * 60 * 24);// 设置cookie的有效期.
		cname.setPath("/");
		// response.setCharacterEncoding("utf-8");
		response.addCookie(cname);// 设置cookie,将cookie存放到respones里面
		return "success";
    }


5. 读取inputstream中内容的方法
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://www.google.com.hk/");
		HttpResponse response;
		InputStream instream = null;

		//The block now is 64k, but it could be increased in size for a bit of performance gain.
		final char[] buffer = new char[2048]; //0x10000
		
		try {

			response = httpclient.execute(httpget);

			HttpEntity entity = response.getEntity();
			if (entity != null) {
				log.info("contentType:" + entity.getContentType()
						+ ";\n contentLen:" + entity.getContentLength());
				instream = entity.getContent();
				Reader in = new InputStreamReader(instream);

				StringBuilder resp = new StringBuilder(3000);
				 
				int read;
				do {
					read = in.read(buffer, 0, buffer.length);
					if (read > 0) {
						resp.append(buffer, 0, read);
					}
				} while (read >= 0);
				
				log.info(new String(resp.toString().getBytes(), "Big5"));

			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != instream) {
				try {
					instream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	

你可能感兴趣的:(java)