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小记 阅读(274) |
评论 (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小记 阅读(1319) |
评论 (0) |
编辑 收藏
XP系统一般情况下在装完系统后会有一个计算机管理员权限的用户,以后登陆时就显示这个用户
进入系统后在控制面板中的用户帐号下看不到Administrator用户,就好象丢失了一样,如何看到呢?
里面有另外一个问题涉及XP自动登陆
单击"开始/运行",输入"rundll32 netplwiz.dll,UsersRunDll",按回车键后弹出“用户帐户”窗口,
这和“控制面板”中打开的“用户账户”窗口不同!
然后取消选定"要使用本机,用户必须输入用户名和密码"选项,单击确定.
在弹出的对话框中输入你想让电脑每次自动登录的账户(默认Administrator)和密码即可。
下一次开机自动用Administrator登陆到系统,再看控制面板就有了Administrator。
Xp默认把Administrator隐藏,虽然都是计算机管理员Administrator和有计算机管理员权限的用户还是有
细微差别的。但是一般情况下使用系统用计算机管理员权限的用户就足够了
posted @
2006-02-13 21:40 java小记 阅读(5287) |
评论 (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小记 阅读(218) |
评论 (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小记 阅读(289) |
评论 (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小记 阅读(276) |
评论 (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小记 阅读(897) |
评论 (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小记 阅读(209) |
评论 (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小记 阅读(232) |
评论 (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小记 阅读(237) |
评论 (0) |
编辑 收藏