使用response.sendRedirect()地址欄將改變
使用request.getRequestDispatcher().forward(request,response)地址欄中的信息保持不變只用通過方法2跳轉 才能在新頁取出來
redirect 會首先發一個response給瀏覽器, 然后瀏覽器收到這個response后再發一個requeset給服務器, 然后服務器發新的response給瀏覽器. 這時頁面收到的request是一個新從瀏覽器發來的.
forward 發生在服務器內部, 在瀏覽器完全不知情的情況下發給了瀏覽器另外一個頁面的response. 這時頁面收到的request不是從瀏覽器直接發來了,可能己經放了數據.
所以:
request.setAttribute存的東西
只用通過方法2跳轉 才能在新頁取出來
用 DecimalFormat 格式化數字
引言
Java中對浮點數的輸出表示
在Java中浮點數包括基本型float、double,以及對象包裝類型的Float和Double,對于這些浮點數的輸出,不管是顯式地還是隱式地調用toString()得到它的表示字串,輸出格式都是按照如下規則進行的
如果絕對值大于0.001、小于10000000,那么就以常規的小數形式表示。
如果在上述范圍之外,則使用科學計數法表示。即類似于1.234E8的形式。
可以使用 java.text.DecimalFormat及其父類NumberFormat格式化數字
本例只淺述DecimalFormat的使用。
Pattern
0 - 如果對應位置上沒有數字,則用零代替
# - 如果對應位置上沒有數字,則保持原樣(不用補);如果最前、后為0,則保持為空。
正負數模板用分號(;)分割
Number Format Pattern Syntax
You can design your own format patterns for numbers by following the rules specified by the following BNF diagram:
pattern := subpattern{;subpattern}
subpattern := {prefix}integer{.fraction}{suffix}
prefix := '\\u0000'..'\\uFFFD' - specialCharacters
suffix := '\\u0000'..'\\uFFFD' - specialCharacters
integer := '#'* '0'* '0'
fraction := '0'* '#'*
DEMO
value
123456.789
pattern
,###.###
output
123,456.789
Explanation
The pound sign (#) denotes a digit,
the comma(逗號) is a placeholder for the grouping separator,
and the period(句號) is a placeholder for the decimal separator.
井號(#)表示一位數字,逗號是用于分組分隔符的占位符,點是小數點的占位符。
如果小數點的右面,值有三位,但是式樣只有兩位。format方法通過四舍五入處理。
value
123.78
pattern
000000.000
output
000123.780
Explanation
The pattern specifies leading and trailing zeros,
because the 0 character is used instead of the pound sign (#).
應用實例 1:
/* * Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved. */
import java.util.*;
import java.text.*;
public class DecimalFormatDemo {
static public void customFormat(String pattern, double value ) {
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);
System.out.println(value + " " + pattern + " " + output);
}
static public void localizedFormat(String pattern, double value, Locale loc ) {
NumberFormat nf = NumberFormat.getNumberInstance(loc);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(pattern);
String output = df.format(value);
System.out.println(pattern + " " + output + " " + loc.toString());
}
static public void main(String[] args) {
customFormat("###,###.###", 123456.789);
customFormat("###.##", 123456.789);
customFormat("000000.000", 123.78);
customFormat("$###,###.###", 12345.67);
customFormat("\u00a5###,###.###", 12345.67);
Locale currentLocale = new Locale("en", "US");
DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
unusualSymbols.setDecimalSeparator('|');
unusualSymbols.setGroupingSeparator('^');
String strange = "#,##0.###";
DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
weirdFormatter.setGroupingSize(4);
String bizarre = weirdFormatter.format(12345.678);
System.out.println(bizarre);
Locale[] locales = { new Locale("en", "US"),
new Locale("de", "DE"),
new Locale("fr", "FR") };
for (int i = 0; i
posted on 2007-08-03 10:01 智者無疆 閱讀(3826) 評論(8) 編輯 收藏 所屬分類: about java