2009年8月26日 星期三

HTML input中button和submit的區別

type=button 就單純是按鈕功能

type=submit 是發送表單

但是對於從事WEB UI的人應該要注意到,使用submit來提高頁面易用性:

使用submit後,頁面支持鍵盤enter鍵操作,而很多WEB軟件設計師,可能沒有註意到submit統一.

用button後往往頁面不支持enter鍵了。所以需要支持enter鍵,必須要設置個submit,默認enter鍵對頁面第一個submit進行操作。

<input type="submit" name="b1" value="提交" onClick="bt_submit_onclick()">

執行完onClick,轉到action。可以自動提交不需要onClick。所以說onclick這裡可以不要。

<input type="button" name="b1" value="提交" onClick="bt_submit_onclick()">

執行完onClick,跳轉文件在js文件裡控制。提交需要onClick。

比如:

1,onclick="form1.action='a.jsp';form1.submit();" & nbsp; 這樣就實現了submit的功能了。

2,<form name="form1" method="post" action="http:/ /www.sina.com.cn">

<input type="button" name="Button" value="Button" onClick="submit()"> </form>

3,<input type="button" name="Button" value="Button" ; onClick="javascript:windows.location.href="你的url"">


type=button 就單純是按鈕功能

type=submit 是發送表單


但是對於從事WEB UI的人應該要注意到,使用submit來提高頁面易用性:


使用submit後,頁面支持鍵盤enter鍵操作,而很多WEB軟件設計師,可能沒有註意到submit統一.


用button後往往頁面不支持enter鍵了。所以需要支持enter鍵,必須要設置個submit,默認enter鍵對頁面第一個submit進行操作。


<input type="submit" name="b1" value="提交" onClick="bt_submit_onclick()">



執行完onClick,轉到action。可以自動提交不需要onClick。所以說onclick這裡可以不要。



<input type="button" name="b1" value="提交" onClick="bt_submit_onclick()">


執行完onClick,跳轉文件在js文件裡控制。提交需要onClick。


比如:


1,onclick="form1.action='a.jsp';form1.submit();" & nbsp; 這樣就實現了submit的功能了。



2,<form name="form1" method="post" action="http:/ /www.sina.com.cn">



<input type="button" name="Button" value="Button" onClick="submit()"> </form>



3,<input type="button" name="Button" value="Button" ; onClick="javascript:windows.location.href="你的url"">

2009年8月17日 星期一

tomcat內存溢出總結



在生產環境中tomcat內存設置不好很容易出現內存溢出。造成內存原因是不一樣的,當然處理方式也不一樣。
這裡根據平時遇到的情況和相關資料進行一個總結。常見的一般會有下面三種情況:
1.OutOfMemoryError: Java heap space
2.OutOfMemoryError: PermGen space
3.OutOfMemoryError: unable to create new native thread.
對於前兩種情況,在應用本身沒有內存洩露的情況下可以用設置tomcat jvm參數來解決。 (-Xms -Xmx -XX:PermSize -XX:MaxPermSize)
最後一種可能需要調整操作系統和tomcat jvm參數同時調整才能達到目的。

第一種:是堆溢出。
在JVM中如果98%的時間是用於GC且可用的Heap size不足2%的時候將拋出此異常信息。
沒有內存洩露的情況下,調整-Xms -Xmx參數可以解決。
-Xms:初始堆大小
-Xmx:最大堆大小
但堆的大小受下面三方面影響:
1.相關操作系統的數據模型(32-bt還是64-bit)限制;(32位系統下,一般限制在1.5G~2G;我在2003 server系統下(物理內存:4G和6G,jdk:1.6 )測試1612M,64為操作系統對內存無限制。)
2.系統的可用虛擬內存限制;
3.系統的可用物理內存限制。
堆的大小可以使用java -Xmx***M version命令來測試。支持的話會出現jdk的版本號,不支持會報錯。
-Xms -Xmx一般配置成一樣比較好比如set JAVA_OPTS= -Xms1024m -Xmx1024m

第二種:永久保存區域溢出
PermGen space的全稱是Permanent Generation space,是指內存的永久保存區域。這一部分用於存放Class和Meta的信息,Class在被Load的時候被放入PermGen space區域,它和和存放Instance的Heap區域不同,GC(Garbage Collection)不會在主程序運行期對PermGen space進行清理,所以如果你的APP會LOAD很多CLASS的話,就很可能出現PermGen space錯誤。這種錯誤常見在web服務器對JSP進行pre compile的時候。但目前的hibernate和spring項目中也很容易出現這樣的問題。 http://www.javaeye.com/topic/80620?page=1的帖子有討論的這個問題。可能是由於這些框架會動態class,而 且jvm的gc是不會清理PemGen space的,導致內存溢出。
這一個一般是加大-XX:PermSize -XX:MaxPermSize來解決問題。
-XX:PermSize永久保存區域初始大小
-XX:PermSize永久保存區域初始最大值
這一般結合第一條使用,比如set JAVA_OPTS= -Xms1024m -Xmx1024m -XX:PermSize=128M -XX:PermSize=256M
有一點需要注意:java -Xmx***M version命令來測試的最大堆內存是-Xmx與-XX:PermSize的和比如係統支持最大的jvm堆大小事1.5G,那-Xmx1024m -XX:PermSize= 768M是無法運行的。

第三種:無法創建新的線程。
這種現像比較少見,也比較奇怪,主要是和jvm與系統內存的比例有關。
這種怪事是因為JVM已經被系統分配了大量的內存(比如1.5G),並且它至少要佔用可用內存的一半。有人發現,在線程個數很多的情況下,你分配給JVM的內存越多,那麼,上述錯誤發生的可能性就越大。

產生這種現象的原因如下(從這個blog中了解到原因:http://hi.baidu.com/hexiong/blog/item/16dc9e518fb10c2542a75b3c.html):

每一個32位的進程最多可以使用2G的可用內存,因為另外2G被操作系統保留。這裡假設使用1.5G給JVM,那麼還餘下 500M可用內存。這500M內存中的一部分必須用於系統dll的加載,那麼真正剩下的也許只有400M,現在關鍵的地方出現了:當你使用Java創建一 個線程,在JVM的內存裡也會創建一個Thread對象,但是同時也會在操作系統裡創建一個真正的物理線程(參考JVM規範),操作系統會在餘下的400 兆內存裡創建這個物理線程,而不是在JVM的1500M的內存堆裡創建。在jdk1.4裡頭,默認的棧大小是256KB,但是在jdk1.5裡頭,默認的 棧大小為1M每線程,因此,在餘下400M的可用內存裡邊我們最多也只能創建400個可用線程。

這樣結論就出來了,要想創建更多的線程,你必須減少分配給JVM的最大內存。還有一種做法是讓JVM宿主在你的JNI代碼裡邊。

給出一個有關能夠創建線程的最大個數的估算公式:

(MaxProcessMemory - JVMMemory - ReservedOsMemory) / (ThreadStackSize) = Number of threads

對於jdk1.5而言,假設操作系統保留120M內存:
1.5GB JVM: (2GB-1.5Gb-120MB)/(1MB) = ~380 threads
1.0GB JVM: (2GB-1.0Gb-120MB)/(1MB) = ~880 threads
在2000/XP/2003的boot.ini裡頭有一個啟動選項,好像是:/PAE /3G ,可以讓用戶進程最大內存擴充至3G,這時操作系統只能佔用最多1G的虛存。那樣應該可以讓JVM創建更多的線程。
因此這種情況需要結合操作系統進行相關調整。

因此:我們需要結合不同情況對tomcat內存分配進行不同的診斷才能從根本上解決問題。

source:
http://www.blogjava.net/george/

reference:
http://www.javaeye.com/topic/80620?page=1
http://ggmm.blog.sohu.com/117545379.html
http://hi.baidu.com/hexiong/blog/item/16dc9e518fb10c2542a75b3c.html
http://www.wujianrong.com/archives/2006/12/javalangoutofmemoryerror_permg.html

2009年8月13日 星期四

Java讀取Properties文件

使用J2SE API讀取Properties文件的六種方法

1.使用java.util.Properties類的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);

2.使用java.util.ResourceBundle類的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3.使用java.util.PropertyResourceBundle類的構造函數
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

4.使用class變量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

6.使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

補充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);

2009年7月19日 星期日

JTable Sorter

jdk 6.x required.

http://www.diybl.com/course/3_program/java/javashl/2008510/115089.html

2009年7月16日 星期四

JTable, RowSorter, SelectionModel

JTable 行排序以及排序后如何把视图(view)上的选中行对应到模型(Model)上的行

前提条件: Sun JDK 1.6 及以上版本

排序的简单实现
--------------
最简单的实现办法如下代码示意:
jtable.setRowSorter( new TableRowSorter(model) )

参数 model 代表 TableModel 的实现类.

以上代码仅示意,你可以扩展抽象类 javax.swing.RowSorter 来实现定制.



选中的行 index 和模型上的 index
--------------------------------
自 jdk 1.6 , JTable 新增了方法 public int convertRowIndexToModel(int viewRowIndex)
至此,就获得了行索引在视图与模型间的对应关系.以下列出关键代码

jtable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
int i= jtable.getSelectedRow();

if (i >= 0) {
int R = jtable.convertRowIndexToModel(i);
StudentTableModel model = (StudentTableModel)jtable.getModel();
Student s= model.getRowAt(R);
textArea.setText(String.format("index to model = %d\tindex to view = %d\tobject = %s", R, i, s==null?"?" : s.toString()));
}
}
});

后文贴出完整源代码.源代码总共3个文件: Student.java; StudentTableModel.java; SortableTable.java
SortableTable.java 是启动类

Student.java
----------------
import java.util.*;
/**
* @author Hardneedl
*/
class Student {
private String name;
private String id;
private java.util.Date birthday;

Student(String id, String name, Date d) {
this.setId(id);
this.setName(name);
this.setBirthday(d);
}
Student() {}
public String getName() {return name;}
public void setName(String name) {this.name = name;}

public String getId() {return id;}
public void setId(String id) {this.id = id;}

public java.util.Date getBirthday() {return birthday;}
public void setBirthday(java.util.Date birthday) {this.birthday = birthday;}

public boolean equals(Object obj) {
if (obj instanceof Student) {
Student st = (Student)obj;
String i = st.getId();
return i!=null && i.equals(getId());
}
return false;
}

public String toString() {return getName();}
}


StudentTableModel.java


import javax.swing.table.*;
import java.util.*;
/**
* @author Hardneedl
*/
class StudentTableModel extends AbstractTableModel {
private java.util.List studentList = new java.util.ArrayList(0);

StudentTableModel() {}
StudentTableModel(List studentList) {
this();
this.studentList.clear();
if (studentList != null) this.studentList.addAll(studentList);
}

void setDatas(java.util.Liststudents){
studentList.clear();
if (students!=null) studentList.addAll(students);
}

void addStudent(Student s){
if(s!=null) {
studentList.add(s);
fireTableDataChanged();
}
}
void removeStudent(Student s){
if (s!=null) {
studentList.remove(s);
fireTableDataChanged();
}
}

public int getRowCount() {return studentList.size();}
public int getColumnCount() {return 3;}
public Object getValueAt(int r, int c) {
Student st = studentList.get(r);
switch(c){
case 0:return st.getId();
case 1:return st.getName();
case 2:return st.getBirthday();
default:return null;
}
}

public String getColumnName(int column) {
switch(column){
case 0:return "ID";
case 1:return "Name";
case 2:return "Birthday";
default:return "???";
}
}

public void setValueAt(Object value, int rowIndex, int columnIndex) {
Student st = studentList.get(rowIndex);
if (st == null) return;
switch(columnIndex) {
case 0:
st.setId(value.toString());
break;

case 1:
st.setName(value.toString());
break;

case 2:
if (value instanceof Date)
st.setBirthday((Date)value);
break;

}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {return true;}
Student getRowAt(int r){return studentList.get(r);}
}


SortableTable.java

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.*;
import java.text.*;
/**
* @author Hardneedl
*/
class SortableTable extends JFrame {
private static final Dimension minSize = new Dimension(300, 200);
private static final Dimension maxSize = new Dimension(1024, 768);
private static final Dimension preferredSize = new Dimension(600, 400);

private JTable jtable;
private JTextArea textArea;
public Dimension getMaximumSize() {return maxSize;}
public Dimension getMinimumSize() {return minSize;}
public Dimension getPreferredSize() {return preferredSize;}
public String getTitle() {return "JTable Sort Demo";}

SortableTable() throws HeadlessException {init();doLay();attachListeners();}

private void init() {
StudentTableModel model=new StudentTableModel();
model.addStudent(new Student("1", "Martin", new Date(Calendar.getInstance().getTimeInMillis())));
model.addStudent(new Student("2", "Rose", new Date(Calendar.getInstance().getTimeInMillis())));
model.addStudent(new Student("3", "Daisy", new Date(Calendar.getInstance().getTimeInMillis())));
model.addStudent(new Student("4", "Tom", new Date(Calendar.getInstance().getTimeInMillis())));
model.addStudent(new Student("5", "Needl", new Date(Calendar.getInstance().getTimeInMillis())));

jtable = new JTable(model);
jtable.setRowSorter( new TableRowSorter(model) );
jtable.getColumnModel().getColumn(2).setCellRenderer(new DefaultTableCellRenderer(){
private SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component cmp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setText(dateFormat.format(value));
setHorizontalAlignment(SwingConstants.RIGHT);
return cmp;
}
});


jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

textArea = new JTextArea(){
public Color getBackground() {return Color.ORANGE;}
public Color getForeground() {return Color.BLUE;}
public boolean isEditable() {return false;}
};
}

private void doLay() {
Container container = getContentPane();
container.add(new JScrollPane(jtable), BorderLayout.CENTER);
container.add(textArea, BorderLayout.SOUTH);
pack();
}

private void attachListeners() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
int i= jtable.getSelectedRow();

if (i >= 0) {
int R = jtable.convertRowIndexToModel(i);
StudentTableModel model = (StudentTableModel)jtable.getModel();
Student s= model.getRowAt(R);
textArea.setText(String.format("index to model = %d\tindex to view = %d\tobject = %s", R, i, s==null?"?" : s.toString()));
}
}
});
}

public static void main(String[] args) {new SortableTable().setVisible(true);}
}



source: http://hi.baidu.com/hardneedl/blog/

2009年7月15日 星期三

Apache + Tomcat整合

實踐apache和tomcat的整合,原來這麼簡單.

1:安裝tomcat,jdk,這些就不說了,這個大家應該都會

2:下載apache_2.2.11-win32-x86-no_ssl.msi,這在apache網站上就有,

3:下載mod_jk-1.2.28-httpd-2.2.3.so,這個是apache和tomcat整合必須的,在apache網站上也有,

4:安裝好apache http server後,進入到apache的安裝目錄下, Apache2.2\conf ,在此新建一個workers.properties文件,將以下內容copy到workers.properties文件中
# Defining a worker named worker1 and of type ajp13
worker.list=ajp13w
worker.ajp13w.type=ajp13
worker.ajp13w.host=127.0.0.1
worker.ajp13w.port=8009
worker.ajp13w.lbfactor=1

5: copy mod_jk-1.2.28-httpd-2.2.3.so文件到Apache2.2\modules目錄下,並且改名為mod_jk.so(為了方便)

6:配置Apache2.2\conf目錄下httpd.conf文件,增加以下內容:
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkMount /*.jsp ajp13w
JkMount /*.jspx ajp13w
JkMount /servlet/* ajp13w
JkMount /*.servlet ajp13w
JkMount /* ajp13w
上面都配置好之後,啟動tomcat ,啟動apache,輸入http://localhost ,看到tomcat的首頁就ok了

source: 夢源著http://www.blogjava.net/mengyuan760

2009年7月7日 星期二

iReport 3.5.2 org.xml.sax.SAXParseException

the report jrxml file edit using iReport 3.5.2

try to run my report unit from there I get an error:

com.jaspersoft.jasperserver.api.JSExceptionWrapper: org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'splitType' is not allowed to appear in element 'band'.



Just configure compatibility with JasperReports 3.5.0 like this:

Tools/Options/General/Compatibility Select the JR version you use on your JS...