來處理編碼
但是當使用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" />
<Connector port="8080" protocol="HTTP/1.1"
URIEncoding="UTF-8"
connectionTimeout="30000"
redirectPort="8443" />
| 快速鍵 | 效果 |
| 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 | 在瀏覽器中顯示 |
<?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>
<?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>
@RequestMapping("/test.do")
public ModelAndView test(HttpServletRequest req,
HttpServletResponse resp) throws Exception {
return new ModelAndView("demo");
}
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(); }
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(); }
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"); }
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"); }
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);
}
}
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);
}
}
}
import java.util.StringTokenizer;
public class StringDemo {
public static void main(String[] args) {
String demo = "String,int,long,double";
StringTokenizer st = new StringTokenizer(demo,",");
System.out.println("st has "+st.countTokens()+"tokens");
while(st.hasMoreTokens()){
System.out.println();
}
}
}
import java.util.HashMap;
import java.util.Map;
public class RightLeft {
Map<String, Integer> left;
Map<String, Integer> right;
public void setUp(){
left = new HashMap<String, Integer>();
left.put("a", 1);
left.put("b", 2);
left.put("c", 3);
right = new HashMap<String, Integer>();
right.put("b", 2);
right.put("c", 4);
right.put("d", 5);
}
/*
* <pre>
* 備住:有兩個Map left right,請在Test()內完成程式碼輸出以下內容
*
* 1.key一樣value不一樣的內容
* 2.key一樣value一樣的內容
* 3.key只存在left不存在right的內容
* 4.key只存在right不存在left的內容
*
*/
public void Test(){
//answer of 1
System.out.println("1.");
for(Object key:left.keySet()){
if(right.get(key)!=null){
if(!right.get(key).equals(left.get(key))){
System.out.println("left key="+key+", value="+left.get(key));
System.out.println("right key="+key+", value="+right.get(key));
}
}
}
//answer of 2
System.out.println("2.");
for(Object key:left.keySet()){
if(right.get(key)!=null){
if(right.get(key).equals(left.get(key))){
System.out.println("left: key="+key+", value="+left.get(key));
System.out.println("right: key="+key+", value="+right.get(key));
}
}
}
//answer of 3
System.out.println("3.");
for(Object key:left.keySet()){
if(right.get(key)==null){
System.out.println("left: key="+key+", value="+left.get(key));
}
}
//answer of 4
System.out.println("4.");
for(Object key:right.keySet()){
if(left.get(key)==null){
System.out.println("right: key="+key+", value="+right.get(key));
}
}
}
public static void main(String[] args) {
RightLeft demo = new RightLeft();
demo.setUp();
demo.Test();
}
}
public class Gcd {
public static void main(String[] args) {
//demo1
System.out.println(gcd(18,12));
//demo2
System.out.println(gcd2(30,18));
}
public static int gcd(int m, int n){
int result = 1;
while(m%n!=0){
result=n;
n=m%n;
m=result;
}
result=n;
return result;
}
public static int gcd2(int m, int n){
if(m%n==0){
return n;
} else {
return gcd2(n,m%n);
}
}
}
public class Gcd {
public static void main(String[] args) {
//demo1
int[] x = new int[] {18,12,30};
System.out.println(dogcd(x));
//demo2
int[] y = new int[] {15,18,30,42,9};
System.out.println(dogcd(y));
}
public static int dogcd(int[] input){
for(int i=0;i<input.length-1;i++){
input[i+1] = gcd(input[i],input[i+1]);
}
return input[input.length-1];
}
public static int gcd(int m, int n){
int result = 1;
while(m%n!=0){
result=n;
n=m%n;
m=result;
}
result=n;
return result;
}
}
public class FibonacciTest {
public static void main(String[] args) {
System.out.println(fibonacci(50));
}
public static long fibonacci(int x){
if(x==1||x==2){
return 1;
}else {
return fibonacci(x-1)+fibonacci(x-2);
}
}
}
public class FibonacciDemo {
public static void main(String[] args) {
FibonacciDemo demo = new FibonacciDemo();
System.out.println(demo.fibonacci(50));
}
public long fibonacci(int n){
if(n==0){
return 0;
} else {
long x_1 = 0;
long x_2 = 1;
for(int i=0;i<n;i++){
if(i>0){
x_2 = x_2 + x_1;
x_1 = x_2 - x_1;
}
}
return x_2;
}
}
}
public class StringBuilderDemo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("This is ");
sb.append("a star");
System.out.println(sb.toString());//This is a star
System.out.println(sb.length());//14
System.out.println(sb.indexOf("star"));//10
sb.insert(sb.indexOf("star"), "new ");
System.out.println(sb.toString());//This is a new star
System.out.println(sb.reverse().toString());//rats wen a si sihT
}
}
public class Demo {
public static void main(String[] args) {
String a = null;
String b = "a";
System.out.println(a==null||a.equals(b));//true
System.out.println(a!=null&&a.equals(b));//false
}
}
public static void main(String[] args) { String a = null; String b = "a"; System.out.println(a==null|a.equals(b));//true System.out.println(a!=null&a.equals(b));//false } }
![]() |
| 請務必在project explorer上右鍵不是點下面的Repository不然之後拉下來會長的很怪 |
public class Test {
public static void main(String[] args) {
int N = 77777777;
long t;
{
StringBuffer sb = new StringBuffer();
t = System.currentTimeMillis();
for (int i = N; i --> 0 ;) {
sb.append("");
}
System.out.println(System.currentTimeMillis() - t);
}
{
StringBuilder sb = new StringBuilder();
t = System.currentTimeMillis();
for (int i = N; i --> 0 ;) {
sb.append("");
}
System.out.println(System.currentTimeMillis() - t);
}
}
}
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="18dp" android:text="帳號" /> <TextView android:id="@+id/textView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView01" android:layout_marginTop="14dp" android:text="密碼" /> <EditText android:id="@+id/editText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView01" android:layout_toRightOf="@+id/textView01" android:ems="10" android:inputType="textPersonName" > <requestFocus /> </EditText> <EditText android:id="@+id/editText02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView02" android:layout_toRightOf="@+id/textView02" android:ems="10" android:inputType="textPassword" /> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/editText02" android:layout_below="@+id/editText02" android:layout_marginTop="16dp" android:text="送出" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView00"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="Large Text"android:textAppearance="?android:attr/textAppearanceLarge" /></RelativeLayout>
public class MainActivity extends Activity {TextView textView00 = null;EditText editText01 = null;EditText editText02 = null;Button button01 =null;public void onCreate(Bundle savedInstanceState){setContentView(R.layout.login);editText01 = (EditText) findViewById(R.id.editText01);button01 = (Button) findViewById(R.id.button01);button01.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String name = editText01.getText().toString();setContentView(R.layout.result);textView00 = (TextView) findViewById(R.id.textView00);textView00.setText("你好:"+name);}});}}