2015年12月29日 星期二

[Tomcat]處理編碼問題

一般來說serlet的post請求可以使用filter或是用request.setCharacterEncoding("UTF-8");
來處理編碼
但是當使用Get方法時,Tomcat沒有用正確的編碼來處理querystring,資料就會變成亂碼。

解決方法是修改tomcat資料夾下的
server.xml檔案,找到Connector標籤,加入URIEncoding="UTF-8"即可。


Ex:
<Connector port="8080" protocol="HTTP/1.1"
 URIEncoding="UTF-8"
 connectionTimeout="30000"
 redirectPort="8443" />

2015年12月27日 星期日

Visual Studio 快速鍵

首先一些常用的快速鍵可以在編輯→進階裡找到
有些快速鍵是用","隔開,
舉例來說註解是Crtl+K,Ctrl+C
那麼你就要先按下Ctrl+K,此時畫面的左下角會顯示這個訊息
然後再按下Ctrl+C就可以把選取範圍註解

還有一些常用的快速鍵如下

快速鍵 效果
CTRL + G 移動到指定行號位置
CTRL + F4 關閉程式頁籤
CTRL + F 尋找
CTRL + SHIFT + F 在檔案中尋找
CTRL + H 取代
CTRL + SHIFT + H 在檔案中取代
CTRL + ALT + L 檢視方案總管
CTRL + ALT + X 檢視工具列
F5 開始偵錯
CTRL + F5 啟動但不偵錯
CTRL + SHIFT + W 在瀏覽器中顯示


2015年12月14日 星期一

[Spring]web.xml簡易設置

再SpringMVC中,控制前端的class為
org.springframework.web.servlet.DispatcherServlet
DispatcherServlet負責將客戶的請求分派給對應於請求的控制物件
DispatcherServlet需要設定在web.xml中

以下是web.xml設定

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>Website</display-name>

     <servlet>

      <servlet-name>Website</servlet-name>

      <servlet-class>

         org.springframework.web.servlet.DispatcherServlet

      </servlet-class>

      <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>/WEB-INF/mvc-config.xml</param-value>

        </init-param>

      <load-on-startup>1</load-on-startup>

   </servlet>

    <servlet-mapping>

        <servlet-name>Website</servlet-name>

        <url-pattern>*.do</url-pattern>

    </servlet-mapping>

  <welcome-file-list>

    <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>

  </welcome-file-list>



  <listener>

    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

  </listener>

</web-app>






其中init-param有個屬性contextConfigLocation是用來設定讀取的xml放哪個位置
預設是/WEB-INF/(servlet-name)-servlet.xml
這邊設定是/WEB-INF/mvc-config.xml
servlet-mapping mapping中設定了spring管理的網頁路徑
此範例設定將所有路徑為.do的交給spring mvc管理

以下為mvc-config.xml的範例

<?xml version="1.0" encoding="UTF-8"?>



<beans xmlns="http://www.springframework.org/schema/beans"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xmlns:context="http://www.springframework.org/schema/context"

 xmlns:aop="http://www.springframework.org/schema/aop"

 xmlns:tx="http://www.springframework.org/schema/tx"

 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">



 <aop:aspectj-autoproxy />

 <context:component-scan base-package="website" />

 

 <!-- viewResolver setup -->

 <context:component-scan base-package="website.controller" />

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />



    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

          <property name="prefix" value="/WEB-INF/jsp/" />

          <property name="suffix" value=".jsp" />

    </bean>



</beans>


ViewResolver是管理view的工具
prefix(此範例值為/WEB-INF/jsp/)表示的是view存放的路徑
suffix(此範例值為.jsp)表示的是檔案的副檔名
若是你的java程式有這段程式碼

   @RequestMapping("/test.do")

    public ModelAndView test(HttpServletRequest req,

            HttpServletResponse resp) throws Exception {

        return new ModelAndView("demo");

    }


那麼/test.do這隻程式就會將網頁導向/WEB-INF/jsp/demo.jap
(檔名式依照ModelAndView內的參數決定)

2015年12月2日 星期三

[雜談]程式效能(二)

之前看到某個系統把陣列轉成字串的功能
臨時想到了關於功能的效能問題
所以就稍微記了一下
他大概的程式碼如下

 public String arrayToString(String[] input){

  StringBuffer result = new StringBuffer();

  if(input!=null){

   boolean isFirstElement = true;

   for(int i = 0; i<input.length; i++){

    if(isFirstElement){

     result.append(input[i]);     

    } else {

     result.append(", ");

     result.append(input[i]);

    }    

   }   

  }      

  return result.toString();

 }





這段大概的意思是將一個陣列轉成字串
第一個字以後都要用", "分開,所以需要用isFirstElement來判斷是不是第一筆資料
可是我覺得這個寫法除了多了一個暫存值以外
每個迴圈都要判斷一次是不是第一個感覺效能不是很好
於是我就思考如何把那個if去掉
後來想到的方法如下


 public String arrayToString(String[] input){

  StringBuffer result = new StringBuffer();

  if(input!=null && input.length>0){

   result.append(input[0]);

   

   for(int i = 1; i<input.length; i++){

    result.append(", ");

    result.append(input[i]);
       

   }  

  }      

  return result.toString();

 }




把for迴圈的i=0,  i<input.length(這句相當於input.length>0)拿掉
將這判斷式移到input!=null的後面
這樣除了把isFirstElement移掉以外
也不會多做if判斷,執行時間跟記憶體空間都能節省到
可讀性的問題也可以用,提供給大家參考!

2015年11月23日 星期一

[程式風格]Guard Clause

Guard Clause是一種程式的風格,通常會至少滿足其中一種以下的行為
1.檢查傳入的參數,如果檢驗不通過就回傳錯誤訊息
2.檢查物件的狀態,如果不符合function使用的物件就
3.簡單快速的處理明顯的邏輯

舉個例子像是以下的程式碼

  if(username!=null){

   if(password!=null){

    System.out.println("do something");
    //這邊是要處理的邏輯
   }else {

    System.out.println("password is null");

   }

  }else {

   System.out.println("username is null");

  }


我們可以知道username跟password皆不為null時才會執行程式
這邊我們可以閱讀是因為巢狀的條件還只有兩層,而且有進行縮排,要是越來越多層
或是程式碼一常,我們不見得可以閱讀程式的執行條件是什麼
以這個例子做出Guard Clause的話大概是長這個樣子

  if(username!=null && password!=null){

   System.out.println("do something");
   //這邊是要處理的邏輯
  }else if(password==null){

   System.out.println("password is null");

  }else{

   System.out.println("username is null");

  }


把要處理的邏輯直接表現出來,這樣是否感覺比較容易明白想要執行的邏輯呢?
我自己覺得還是看每個人習慣,不過這種程式風格就給各位當做參考了


參考資料:http://c2.com/cgi/wiki?GuardClause

2015年11月4日 星期三

[雜談]程式效能(一)

前幾天在FB上看到有大學生在某社團問作業:1加到10的程式怎麼做
然後看到一些很有趣的解答
剛好趁機談談程式的效能
由1加到n這個運算
很直觀的做法就是1+2+3+4+5+6.+..+n
用for迴圈表示就是i=1到n,result= result+i;
但是這樣你數字越大他要跑的迴圈就越常
同樣的計算結果你用高斯方法就是n*(n+1)/2
不管你的n有多大,程式永遠做一次加法,一次乘法,一次除法
這就是所謂好的演算法
那對效能有什麼影響呢,以下我們拿這個範例程式
第一個呼叫的是for迴圈的方法
第二個呼叫的是高斯方法
我使用了結果程式的時間減掉程式開始時間來計算每一個方法使用了多少毫秒
程式碼如下


import java.util.Date;



public class SumTest {



 public static void main(String[] args) {

  

  useForLoop(10000000);

  useGaussMethod(10000000);

 }



 public static void useForLoop(long input){

  long result = 0;

  long startTime = new Date().getTime();

  for(long i = 1;i<=input;i++){

   result = result + i;

  }

  System.out.println(result);

  long endTime = new Date().getTime();

  long totalTime = endTime-startTime;

  System.out.println(totalTime);

 }

 

 public static void useGaussMethod(long input){

  long startTime = new Date().getTime();

  long result = input*(input+1)/2;

  System.out.println(result);

  long endTime = new Date().getTime();

  long totalTime = endTime-startTime;

  System.out.println(totalTime);

 }

 

}



然後實驗開始啦,首先是n等於一萬
看起來兩個花的時間都是0毫秒沒什麼差別,於是我將n值放大到十萬
似乎有點差別了,不過可能是誤差,再放大n到100萬看看

差距來到了三毫秒,最後我們再放大n到1000萬
for回圈的消耗時間增加到了25毫秒
所以我們可以知道
在資料量小的時候,我們不一定能感覺出演算法的好壞
但是好的演算法在輸入的值越大的時候,節省的效能會越明顯
雖然一般來講先完成功能就好,但是當功能完成之後應該要去思考如何提升程式效能提高使用品質

之後會不會繼續這個主題就看我有沒有時間囉~_~+

2015年10月30日 星期五

[Java]複製陣列的方法(System.arraycopy)

有時候需要建立一個新陣列,這個新陣列跟舊的陣列前面都一樣
只有最後幾個值不同或是加了幾個值

或是有兩個陣列,我們需要合併這兩個陣列的時候
除了用for迴圈把陣列一個一個倒進去以外
我們可以使用System的arraycopy方法

文件方法如下
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

第一個src要放入的是被複製的陣列
srcPos是指定被複雜的陣列從第幾項開始複製

dest放入的是要複製的陣列
destPos是指定要複製的陣列從第幾項開始寫入

length放入的是你總共要複製幾項資料
以下是最常用的兩個範例


public class copyArrayDemo {



 public static void main(String[] args) {

  System.out.println("This is demo 1");

  int[] arr1 = {1,2,3};

  int[] arr2 = new int[arr1.length+1];

  System.arraycopy(arr1, 0, arr2, 0, arr1.length);

  arr2[arr1.length]= 4;

  // arr2 == {1,2,3,4}

  for(int item:arr2){

   System.out.println(item);

  }

  System.out.println("This is demo 2");



  

  String[] array1 = {"item1","item2","item3"};  

  String[] array2 = {"demo1","demo2","demo3"};

  

  

  String[] sum = new String[array1.length+array2.length];

  System.arraycopy(array1, 0, sum, 0, array1.length);

  System.arraycopy(array2, 0, sum, array1.length, array2.length);

  // sum =={"item1","item2","item3","demo1","demo2","demo3"}

  for(String item:sum){

   System.out.println(item);

  }    

 }

}