import
?java.security.MessageDigest;
import
?java.security.NoSuchAlgorithmException;

public
?
class
?PasswordManager?
{
????
????
//
***************************************************************
????
//
?Public?Interface
????
//
***************************************************************
????
/**?*/
/**
?????*?Creates?a?one-way?has?of?the?specified?password.??This?allows?passwords?to?be
?????*?safely?stored?in?the?database?without?any?way?to?retrieve?the?original?value.
?????*?
?????*?
@param
?password?the?string?to?encrypt.
?????*?
?????*?
@return
?the?encrypted?password,?or?null?if?encryption?failed.
?????
*/
????
public
?
static
?String?encryptPassword(?String?password?)?
{
????????

????????
try
?
{
????????????MessageDigest?md?
=
?MessageDigest.getInstance(
"
SHA
"
);
????????
????????????
//
Create?the?encrypted?Byte[]
????????????md.update(?password.getBytes()?);
????????????
byte
[]?hash?
=
?md.digest();
????????????
????????????
//
Convert?the?byte?array?into?a?String
????????????
????????????StringBuffer?hashStringBuf?
=
?
new
?StringBuffer();
????????????String?byteString;
????????????
int
?byteLength;
????????????

????????????
for
(?
int
?index?
=
?
0
;?index?
<
?hash.length;?index
++
?)?
{
????????????????
????????????????byteString?
=
?String.valueOf(?hash[index?]?
+
?
128
?);
????????????????
????????????????
//
Pad?string?to?3.??Otherwise?hash?may?not?be?unique.
????????????????byteLength?
=
?byteString.length();

????????????????
switch
(?byteLength?)?
{
????????????????
case
?
1
:
????????????????????byteString?
=
?
"
00
"
?
+
?byteString;
????????????????????
break
;
????????????????
case
?
2
:
????????????????????byteString?
=
?
"
0
"
?
+
?byteString;
????????????????????
break
;
????????????????}
????????????????hashStringBuf.append(?byteString?);
????????????}
????????????
????????????
return
?hashStringBuf.toString();
????????}
????????
catch
(?NoSuchAlgorithmException?nsae?)?
{
????????????System.out.println(?
"
Error?getting?password?hash?-?
"
?
+
?nsae.getMessage()?);
????????????
return
?
null
;
????????}
????}
}
posted @
2006-03-28 15:45 java小記 閱讀(275) |
評論 (0) |
編輯 收藏
偶爾eclipse3.1的web項目有時候不能自動編譯/WEB-INF/src,即使選擇工程的自動編譯開關也不好使,不知道是不是Bug
我這樣解決的:
1. project->properties->java build path->source->.../WEB-INF/src的output folder不要默認,編輯讓它指向../WEB-INF/classes
然后重新點擊build工程即可自動編譯。
2. 再就是最重要的要看工程下面是否缺少了work目錄,由于CVS控制時不把work加如版本,所以checkout后沒有這個目錄,要手工加上有的工程就能自動編譯了
posted @
2006-03-21 10:50 java小記 閱讀(1320) |
評論 (0) |
編輯 收藏
XP系統一般情況下在裝完系統后會有一個計算機管理員權限的用戶,以后登陸時就顯示這個用戶
進入系統后在控制面板中的用戶帳號下看不到Administrator用戶,就好象丟失了一樣,如何看到呢?
里面有另外一個問題涉及XP自動登陸
單擊"開始/運行",輸入"rundll32 netplwiz.dll,UsersRunDll",按回車鍵后彈出“用戶帳戶”窗口,
這和“控制面板”中打開的“用戶賬戶”窗口不同!
然后取消選定"要使用本機,用戶必須輸入用戶名和密碼"選項,單擊確定.
在彈出的對話框中輸入你想讓電腦每次自動登錄的賬戶(默認Administrator)和密碼即可。
下一次開機自動用Administrator登陸到系統,再看控制面板就有了Administrator。
Xp默認把Administrator隱藏,雖然都是計算機管理員Administrator和有計算機管理員權限的用戶還是有
細微差別的。但是一般情況下使用系統用計算機管理員權限的用戶就足夠了
posted @
2006-02-13 21:40 java小記 閱讀(5288) |
評論 (3) |
編輯 收藏
安全域是Tomcat的內置功能,主要有以下幾種安全域:
JDBCRealm
DataSourceRealm
JNDIRealm
MemoryRealm
JAASRealm
在conf/server.xml中配置應用的<Context......>下面的<Realm className="org.apache.catalina.realm.MemoryRealm" />
從一個XML文件中讀取用戶信息,默認的就是tomcat-users.xml
tomcat-users.xml中的角色定義
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="guest"/>
<user username="lee" password="lee" roles="guest"/>
<user username="suoxin" password="suoxin" roles="tomcat,role1"/>
</tomcat-users>
在應用下的web.xml中加入<security-constraint>元素
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
<role-name>guest</role-name>
</auth-constraint>
</security-constraint>
在應用下的web.xml中加入<login-config>元素
<login-config>
<auth-method>FORM</auth-method>
<!--這里FORM是基于表單的驗證,會跳轉到mylogin.jsp,如果出錯就到myerror.jsp,
還有基于對話筐的是BASIC關鍵字,但是不安全在網絡傳輸中。摘要驗證DIGEST會采
用MD5(Message Digest Algorithm)加密傳輸-->
<form-login-config>
<form-login-page>/mylogin.jsp</form-login-page>
<form-error-page>/myerror.jsp</form-error-page>
</form-login-config>
</login-config>
mylogin.jsp的action和name必須嚴格規范寫
<form name="form1" id="form1" method="post" action="j_security_check">
<input type="text" name="j_username"/>
<input type="text" name="j_password"/>
<input type="submit" name="Submit" value="提交" />
</form>
posted @
2006-02-11 09:11 java小記 閱讀(221) |
評論 (0) |
編輯 收藏
1,下載OSCache, oscache-2.2-full.zip
2,解壓縮oscache-2.2-full.zip后把oscache-2.2.jar拷貝到應用的WEB-INF/lib下 ,
并把etc目下下的oscache.properties拷貝到應用的WEB-INF/classes下.
3, 在應用的web.xml中加入緩存過濾器
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>600</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>application</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>/servlets/UserAllProducts</url-pattern>
</filter-mapping> 以上的/servlets/UserAllProducts訪問需要操作數據庫,并且這些內容一段時間內很少改變,這樣在內存緩存這個URL很有必要
它可以降低數據庫訪問量。
經過以上配置后,當第一次訪問/servlets/UserAllProducts時,從數據庫中查出所有產品列表并緩存到application中600秒。
在600秒內的任何訪問/servlets/UserAllProducts都不會真正執行這個servlet,而直接從application中取出緩存的內容。這樣
就減少了數據庫的訪問量。
posted @
2006-02-09 18:58 java小記 閱讀(291) |
評論 (0) |
編輯 收藏
今天遇到了這樣的問題,就是浮點運算后數據比較出現錯誤,郁悶了半天,網上查了資料才發現浮點數直接用雙目運算符連接會出現結果不準確問題。解決方法如下:
1。所有浮點運算都在數據庫內做好,也就是都用sql實現了
2。用BigDecimal實現,方法如下(僅僅是個例子):

import java.math.BigDecimal;


public class tt
{


/** *//**
* @param args
*/

public static void main(String[] args)
{
float a = 1.1f;
float b = 2.2f;
tt t = new tt();
System.out.println(t.add(a,b));
System.out.println(t.sub(a,b));
System.out.println(t.mul(a,b));
System.out.println(t.div(a,b));
System.out.println(t.round(a));

}

public float add(float v1,float v2)
{//加法
BigDecimal b1 = new BigDecimal(Float.toString(v1));
BigDecimal b2 = new BigDecimal(Float.toString(v2));
return b1.add(b2).floatValue();
}


public float sub(float v1,float v2)
{//減法
BigDecimal b1 = new BigDecimal(Float.toString(v1));
BigDecimal b2 = new BigDecimal(Float.toString(v2));
return b1.subtract(b2).floatValue();
}


public float mul(float v1,float v2)
{//乘法
BigDecimal b1 = new BigDecimal(Float.toString(v1));
BigDecimal b2 = new BigDecimal(Float.toString(v2));
return b1.multiply(b2).floatValue();
}


public float div(float v1,float v2)
{//除法
BigDecimal b1 = new BigDecimal(Float.toString(v1));
BigDecimal b2 = new BigDecimal(Float.toString(v2));
return b1.divide(b2,3,BigDecimal.ROUND_HALF_UP).floatValue();
}


public float round(float v)
{//截取3位
BigDecimal b = new BigDecimal(Float.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one,3,BigDecimal.ROUND_HALF_UP).floatValue();
}
}

posted @
2006-02-08 16:43 java小記 閱讀(277) |
評論 (0) |
編輯 收藏
問題:Spring+Hibernate的應用中,定義了兩個業務Service,這里分別稱它們為serivceA,ServiceB。
它們的關系簡單點來說是這樣的:
serviceA需要引用serviceB,在serviceB中定義了一個接口列表,serverA必須在serviceB初始化時設置進列表。
在純bean的情況下,也就是這兩個類不需要設置其他bean的情況下,循環引用是正常的,可以通過的。例如下面配置所表示:
<bean id="serviceA" class="A" autowire="byName" lazy-init="true">
<property name="serviceB"><ref local="serviceB"/></property>
</bean>
<bean id="serviceB" class="B" autowire="byName" lazy-init="true">
<property name="serviceA"><ref bean="serviceA"/></property>
</bean>
但是作為一個業務接口,它應該是不需要關心事務,回滾這些無關的東西,
但現實又有這樣的需求,所以我們必須保證透明的實現這個功能,于是引
入了AOP方式解決該問題,利用的是Spring自帶的org.springframework.t
ransaction.interceptor.TransactionProxyFactoryBean.
重新聲明文件如下:
<bean id="baseTxProxy" lazy-init="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyTargetClass"><value>true</value></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="serviceA" parent="baseTxProxy">
<property name="target"><ref local="serviceAImpl"/></property>
</bean>
<bean id="serviceAImpl" class="serviceA" autowire="byName" lazy-init="true">
<property name="serviceB">
<ref bean="serviceB"/>
</property>
</bean>
<bean id="serviceB" parent="baseTxProxy" lazy-init="true">
<property name="target"><ref local="serviceBImpl"/></property>
</bean>
<bean id="serviceBImpl" class="D" lazy-init="true">
<property name="serviceA">
<ref bean="serviceA"/>
</property>
</bean>
于是問題就出現了,Spring報了FactoryBeanCircularReferenceException,無法繼續完成設置工作。
查看TransactionProxyFactoryBean源碼,其實現了FactoryBean和InitializingBean接口,應該是
做了代理之后,兩個代理Bean需要等待所有Bean設置完成后才會標識狀態為初始化完畢,于是造成了
沖突。
由于兩個業務服務互相調用的路徑是不相交的,所以采用了一種變通的方法,在聲明serviceA時,
直接定義serviceB:
<bean id="serviceAImpl" class="serviceA" autowire="byName" lazy-init="true">
<property name="serviceB">
<bean class="B" autowire="byName"/>
</property>
</bean>
相當于serviceB和serviceA中使用的serviceB不是同一個實例。
但是如果確實調用重合時怎么辦?
解決方法是這樣的:
<bean id="serviceAImpl" class="serviceA" autowire="byName" lazy-init="true">
<property name="serviceB">
<ref bean="serviceBImpl"/>
</property>
</bean>
非常簡單,serviceAImpl調用時,可能已經在事務環境中了,不需再使用serviceB代理的事務支持,
于是直接引用serviceB實例。這個方法是我寫這篇文章時想到的,-_-!!!,看來知識果真還是好好
整理呀。
posted @
2006-02-08 16:32 java小記 閱讀(898) |
評論 (0) |
編輯 收藏
關于網站開發中連接的可移植性總結

網絡主機地址http://localhost:8080/

1. 相對于根目錄的連接,形如: "/another.jsp"
/DIR/目錄下的
<a href="/another.jsp">Link</a>
<html:link href="/another.jsp">Link</html:link>
<html:link page="/another.jsp">Link</html:link>
在默認ROOT應用中,分別連接到地址:
http://localhost:8080/another.jsp
http://localhost:8080/another.jsp
http://localhost:8080/another.jsp
在應用test中,分別連接到地址:
http://localhost:8080/another.jsp
http://localhost:8080/another.jsp
http://localhost:8080/test/another.jsp
2. 相對于當前目錄的連接,形如: "./another.jsp" 或 "another.jsp"
/DIR/目錄下的
<a href="./another.jsp">Link</a>
<html:link href="./another.jsp">Link</html:link>
<html:link page="./another.jsp">Link</html:link>
在默認ROOT應用中,都分別連接到地址:
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
在應用test中,分別連接到地址:
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/test./another.jsp 錯誤連接(而且與DIR無關,都連接到此地址)
/DIR/目錄下的
<a href="another.jsp">Link</a>
<html:link href="another.jsp">Link</html:link>
<html:link page="another.jsp">Link</html:link>
在默認ROOT應用中,都分別連接到地址:
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
在應用test中,分別連接到地址:
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/
/DIR/another.jsp
http://localhost:8080/testanother.jsp 錯誤連接(而且與DIR無關,都連接到此地址)
3總結
由于在網站開發時經常不使用默認的WEB應用,而可能把一個模塊開發成一個WEb應用(可能多人合作),
但是在發布的時候要把所有模塊發布為一個WEB應用,并通常是默認的WEB應用,為了使URL不依賴于Web應用
和是否是默認WEB應用,建議如下
a.對于相對于WEB應用根目錄的連接,形如: "/another.jsp"
使用<html:link page="/
..">Link </html:link>
b.對于相對于當前目錄的連接,形如: "./another.jsp" 或 "another.jsp"
使用<html:link href="./another.jsp">Link </html:link>
<a href="./another.jsp">Link </a>或
<html:link href="another.jsp">Link </html:link>
<a href="another.jsp">Link </a>
不要使用<html:link page="./another.jsp">Link </html:link>
<html:link page="another.jsp">Link </html:link> 此二者不可移植
c.建議使用struts的html標簽庫<html:link
..標簽,因為當用戶關閉Cookie時會自動重寫URL,所以概括
兩句話:相對應用根目錄用<html:link page="/.."
相對當前的目錄用<html:link href="./XXXXX.jsp" 或 <html:link href="XXXXX.jsp"
4.補充
還有一個標簽<html:link forward="forwardname"> Link </html:link> 上面沒有提到,現做以說明。
forward屬性和struts配置文件<global-forwards>中的一個<forward>元素匹配,不能和<action>中<forward>匹配。
例子:<global-forwards>
<forward name="forwardname" path="/
/another.jsp"/>
</global-forwards>
<html:link forward="forwardname"> Link </html:link>
相當于<html:link page="/
/another.jsp"> Link </html:link>
需要注意的是:<global-forwards>中<forward>的path要使用相對于WEB應用的根目錄路徑,包括<action>中也是。
在struts1.1多應用模塊時<html:link forwad
. 有時不好使,不知道為什么????(好像是模塊切換狀態
不對)所以最好不用。
替代方法;
網頁中用連接
<html:link page="/forwardaction.do">forward</html:link>
在action-mapping配置如下action
<action path="/forwardaction" forward="/index.jsp" />
posted @
2006-02-06 20:58 java小記 閱讀(210) |
評論 (0) |
編輯 收藏
package aa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//for test6
import static java.lang.Math.*;


public class Test
{


public static void main(String[] args)
{
// 1
Double d = 123.45;
d += 20.601;
System.out.println(d);
double dd = d + 45.123;
System.out.println(dd);

// 2,3
List<List<String>> list = new ArrayList<List<String>>();

List<String> line1 = new ArrayList<String>();
line1.add("hello");
line1.add(",");
line1.add("world");
// line1.add(new Integer(123));//complie error
list.add(line1);

List<String> line2 = new ArrayList<String>();
line2.add("hello2");
line2.add(",");
line2.add("world2");
list.add(line2);


for (List<String> g : list)
{

for (String str : g)
{
System.out.print(str);
}
System.out.println();
}

// 4
Color bg = Color.Red;
System.out.println(bg);


for (Color c : Color.values())
{
System.out.println(c);
}

// 5
print("hello", ",", "World", new Date(), 123456);
//6
double i=2*PI;
print(i);

}


public static void print(Object
objects)
{

for (Object obj : objects)
{
System.out.println(obj);
}
}

}


enum Color
{
Red, Green, Blue
}

posted @
2006-02-05 10:55 java小記 閱讀(233) |
評論 (0) |
編輯 收藏
eclipse2不支持jdk1.5支持1.4
eclipse3.0好象也不支持jdk1.5
eclipse3.1支持jdk1.5
jbuilder9不支持jdk1.5支持1.4
jbuilder2006好象集成的就是jdk1.5,沒用過
posted @
2006-02-05 10:52 java小記 閱讀(238) |
評論 (0) |
編輯 收藏