﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-learning java-文章分类-java 转帖</title><link>http://www.blogjava.net/lmsun/category/2850.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 20:35:47 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 20:35:47 GMT</pubDate><ttl>60</ttl><item><title>JSP页面查询显示常用模式</title><link>http://www.blogjava.net/lmsun/articles/14127.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Mon, 26 Sep 2005 09:24:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/14127.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/14127.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/14127.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/14127.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/14127.html</trackback:ping><description><![CDATA[原文链接：<A href="http://www.javaresearch.org/article/showarticle.jsp?column=106&amp;thread=8512">http://www.javaresearch.org/article/showarticle.jsp?column=106&amp;thread=8512</A><BR><BR>author:&nbsp;evan<BR><BR>email:&nbsp;<A class=l2 href="maioto:evan_zhao@hotmail.com" target=_blank>evan_zhao@hotmail.com</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR><B>背景</B>：<BR>1．&nbsp;&nbsp;&nbsp;&nbsp;需要将数据库查询结果在JSP中以列表方式显示<BR>2．&nbsp;&nbsp;&nbsp;&nbsp;在一个良好的J2EE模式中数据库查询一般用DAO实现（Data&nbsp;Access&nbsp;Object），&nbsp;JSP仅用于显示数据<BR><BR><B>问题</B>：<BR>&nbsp;&nbsp;&nbsp;&nbsp;通过JDBC&nbsp;ResultSet可获取查询结果（存在于数据库缓冲区内），但在Statement、Connection关闭后ResultSet即不可用。因此需要一种方式取出所有查询结果并传递至JSP页面。<BR><BR><B>解决方法一</B>：<BR>&nbsp;&nbsp;&nbsp;&nbsp;使用Value&nbsp;Object。将每条记录均封装成JavaBean对象，把这些对象装入Collection传送给JSP显示。这种方法的缺点是每一种查询都需要定义一个java&nbsp;class，并且将记录数据封装成java对象时也需要很多额外的代码。<BR>示例代码：<BR><BR>
<DIV class=codeStyle>
<OL>
<LI>
<LI><I><FONT color=#339900>//查询数据代码</FONT></I> 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>Connection</FONT>&nbsp;conn&nbsp;=&nbsp;DBUtil.getConnection(); 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>PreparedStatement</FONT>&nbsp;pst&nbsp;=&nbsp;<B><FONT color=#0000ff>null</FONT></B>; 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>ResultSet</FONT>&nbsp;rs&nbsp;=&nbsp;<B><FONT color=#0000ff>null</FONT></B>; 
<LI>&nbsp;&nbsp;<B><FONT color=#0000ff>try</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target=_blank><FONT class=classLink><U>String</U></FONT></A></B>&nbsp;sql=“select&nbsp;emp_code,&nbsp;real_name&nbsp;from&nbsp;t_employee&nbsp;where&nbsp;organ_id=?”; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;pst&nbsp;=&nbsp;conn.preparedStatement(sql); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;pst.setString(1,&nbsp;“101”); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#ff0000>ResultSet</FONT>&nbsp;rs&nbsp;=&nbsp;pst.executeQuery(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#ff0000>List</FONT>&nbsp;list&nbsp;=&nbsp;<B><FONT color=#0000ff>new</FONT></B>&nbsp;<FONT color=#ff0000>ArrayList</FONT>(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;Employee&nbsp;emp; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>while</FONT></B>&nbsp;(rs.next()){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;emp&nbsp;=&nbsp;<B><FONT color=#0000ff>new</FONT></B>&nbsp;Employee(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;emp.setReakName(rs.getString(“real_name”)); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;emp.setEmpCode(rs.getString(“emp_code”)); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;… 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add(emp); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>return</FONT></B>&nbsp;list; 
<LI>&nbsp;&nbsp;}<B><FONT color=#0000ff>finally</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;DBUtil.close(rs,&nbsp;pst&nbsp;,conn); 
<LI>&nbsp;&nbsp;} 
<LI>
<LI>
<LI><I><FONT color=#339900>//jsp显示部分代码</FONT></I> 
<LI>&lt;% 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>List</FONT>&nbsp;empList&nbsp;=&nbsp;(<FONT color=#ff0000>List</FONT>)request.getAttribute(“empList”); 
<LI>&nbsp;&nbsp;<B><FONT color=#0000ff>if</FONT></B>&nbsp;(empList&nbsp;==&nbsp;<B><FONT color=#0000ff>null</FONT></B>)&nbsp;empList&nbsp;=&nbsp;<FONT color=#ff0000>Collections</FONT>.EMPTY_LIST; 
<LI>%&gt; 
<LI>… 
<LI>&lt;table&nbsp;&nbsp;cellspacing=<FONT color=#ff33ff>"0"</FONT>&nbsp;width=”90%”&gt; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&nbsp;&nbsp;&lt;td&gt;代码&lt;/td&gt;&nbsp;&lt;td&gt;姓名&lt;/td&gt;&nbsp;&nbsp;&lt;/tr&gt; 
<LI>&lt;% 
<LI>&nbsp;&nbsp;Employee&nbsp;emp; 
<LI>&nbsp;&nbsp;<B><FONT color=#0000ff>for</FONT></B>&nbsp;(<B><FONT color=#0000ff>int</FONT></B>&nbsp;i=0;&nbsp;i&lt;&nbsp;empList.size();&nbsp;i++){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;emp&nbsp;=&nbsp;(Employee)&nbsp;empList.get(i); 
<LI>%&gt; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&nbsp;&nbsp; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;%=&nbsp;emp.getEmpCode()%&gt;&lt;/td&gt;&nbsp; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;%=&nbsp;emp.getRealName()%&gt;&lt;/td&gt;&nbsp;&nbsp; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt; 
<LI>&lt;% 
<LI>&nbsp;&nbsp;}<I><FONT color=#339900>//&nbsp;end&nbsp;for</FONT></I> 
<LI>%&gt; 
<LI>&lt;/table&gt; </LI></OL></DIV><BR><BR><B>解决方法二</B>：<BR>&nbsp;&nbsp;&nbsp;&nbsp;遍历ResultSet取出所有数据封装进Collection。<BR>具体做法：<BR>1．&nbsp;&nbsp;&nbsp;&nbsp;生成一个List对象(List&nbsp;list&nbsp;=&nbsp;new&nbsp;ArrayList()&nbsp;)。<BR>2．&nbsp;&nbsp;&nbsp;&nbsp;生成一个Map对象(Map&nbsp;map&nbsp;=&nbsp;new&nbsp;HashMap()&nbsp;)。使用Map封装一行数据，key为各字段名，value为对应的值。(map.put(“USER_NAME”),&nbsp;rs.getString(“USER_NAME”))<BR>3．&nbsp;&nbsp;&nbsp;&nbsp;将第2&nbsp;步生成的Map对象装入第1步的list对象中(list.add(map)&nbsp;)。<BR>4．&nbsp;&nbsp;&nbsp;&nbsp;重复2、3步直到ResultSet遍历完毕<BR>在DBUtil.&nbsp;resultSetToList(ResultSet&nbsp;rs)方法中实现了上述过程（所有列名均使用大写），可参考使用。<BR><BR><B>示例代码</B>：<BR><BR>
<DIV class=codeStyle>
<OL>
<LI>
<LI><I><FONT color=#339900>//查询数据部分代码：</FONT></I> 
<LI>&nbsp;&nbsp;… 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>Connection</FONT>&nbsp;conn&nbsp;=&nbsp;DBUtil.getConnection(); 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>PreparedStatement</FONT>&nbsp;pst&nbsp;=&nbsp;<B><FONT color=#0000ff>null</FONT></B>; 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>ResultSet</FONT>&nbsp;rs&nbsp;=&nbsp;<B><FONT color=#0000ff>null</FONT></B>; 
<LI>&nbsp;&nbsp;<B><FONT color=#0000ff>try</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target=_blank><FONT class=classLink><U>String</U></FONT></A></B>&nbsp;sql=“select&nbsp;emp_code,&nbsp;real_name&nbsp;from&nbsp;t_employee&nbsp;where&nbsp;organ_id=?”; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;pst&nbsp;=&nbsp;conn.preparedStatement(sql); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;pst.setString(1,&nbsp;“101”); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;rs&nbsp;=&nbsp;pst.executeQuery(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#ff0000>List</FONT>&nbsp;list&nbsp;=&nbsp;DBUtil.&nbsp;resultSetToList(<FONT color=#ff0000>ResultSet</FONT>&nbsp;rs); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>return</FONT></B>&nbsp;list; 
<LI>&nbsp;&nbsp;}<B><FONT color=#0000ff>finally</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;DBUtil.close(rs,&nbsp;pst&nbsp;,conn); 
<LI>&nbsp;&nbsp;} 
<LI>
<LI>
<LI>
<LI><I><FONT color=#339900>//JSP显示部分代码</FONT></I> 
<LI>&lt;% 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>List</FONT>&nbsp;empList&nbsp;=&nbsp;(<FONT color=#ff0000>List</FONT>)request.getAttribute(“empList”); 
<LI>&nbsp;&nbsp;<B><FONT color=#0000ff>if</FONT></B>&nbsp;(empList&nbsp;==&nbsp;<B><FONT color=#0000ff>null</FONT></B>)&nbsp;empList&nbsp;=&nbsp;<FONT color=#ff0000>Collections</FONT>.EMPTY_LIST; 
<LI>%&gt; 
<LI>… 
<LI>&lt;table&nbsp;&nbsp;cellspacing=<FONT color=#ff33ff>"0"</FONT>&nbsp;width=”90%”&gt; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&nbsp;&nbsp;&lt;td&gt;代码&lt;/td&gt;&nbsp;&lt;td&gt;姓名&lt;/td&gt;&nbsp;&nbsp;&lt;/tr&gt; 
<LI>&lt;% 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>Map</FONT>&nbsp;colMap; 
<LI>&nbsp;&nbsp;<B><FONT color=#0000ff>for</FONT></B>&nbsp;(<B><FONT color=#0000ff>int</FONT></B>&nbsp;i=0;&nbsp;i&lt;&nbsp;empList.size();&nbsp;i++){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;colMap&nbsp;=&nbsp;(<FONT color=#ff0000>Map</FONT>)&nbsp;empList.get(i); 
<LI>%&gt; 
<LI>&nbsp;&nbsp;&lt;tr&gt;&nbsp;&nbsp; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;%=colMap.get(“EMP_CODE”)%&gt;&lt;/td&gt;&nbsp; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;%=colMap.get(“REAL_NAME”)%&gt;&lt;/td&gt;&nbsp;&nbsp; 
<LI>&nbsp;&nbsp;&lt;/tr&gt; 
<LI>&lt;% 
<LI>&nbsp;&nbsp;}<I><FONT color=#339900>//&nbsp;end&nbsp;for</FONT></I> 
<LI>%&gt; 
<LI>&lt;/table&gt; </LI></OL></DIV><BR><BR><B>解决方法三</B>：<BR>&nbsp;&nbsp;&nbsp;&nbsp;使用RowSet。<BR>RowSet是JDBC2.0中提供的接口,Oracle对该接口有相应实现，其中很有用的是oracle.jdbc.rowset.OracleCachedRowSet。&nbsp;OracleCachedRowSet实现了ResultSet中的所有方法，但与ResultSet不同的是，OracleCachedRowSet中的数据在Connection关闭后仍然有效。<BR><BR>oracle的rowset实现在<A href="http://otn.oracle.com/software/content.html">http://otn.oracle.com/software/content.html</A>的jdbc下载里有，名称是ocrs12.zip<BR><BR><B>示例代码</B>：<BR><BR>
<DIV class=codeStyle>
<OL>
<LI><I><FONT color=#339900>//查询数据部分代码：</FONT></I> 
<LI>&nbsp;&nbsp;<B><FONT color=#0000ff>import</FONT></B>&nbsp;javax.sql.<B><A href="http://www.javaresearch.org/source/jdk142/javax/sql/RowSet.java.html" target=_blank><FONT class=classLink><U>RowSet</U></FONT></A></B>; 
<LI>&nbsp;&nbsp;<B><FONT color=#0000ff>import</FONT></B>&nbsp;oracle.jdbc.rowset.OracleCachedRowSet; 
<LI>&nbsp;&nbsp;… 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>Connection</FONT>&nbsp;conn&nbsp;=&nbsp;DBUtil.getConnection(); 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>PreparedStatement</FONT>&nbsp;pst&nbsp;=&nbsp;<B><FONT color=#0000ff>null</FONT></B>; 
<LI>&nbsp;&nbsp;<FONT color=#ff0000>ResultSet</FONT>&nbsp;rs&nbsp;=&nbsp;<B><FONT color=#0000ff>null</FONT></B>; 
<LI>&nbsp;&nbsp;<B><FONT color=#0000ff>try</FONT></B>{…… 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target=_blank><FONT class=classLink><U>String</U></FONT></A></B>&nbsp;sql=“select&nbsp;emp_code,&nbsp;real_name&nbsp;from&nbsp;t_employee&nbsp;where&nbsp;organ_id=?”; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;pst&nbsp;=&nbsp;conn.preparedStatement(sql); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;pst.setString(1,&nbsp;“101”); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;rs&nbsp;=&nbsp;pst.executeQuery(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;OracleCachedRowSet&nbsp;ors&nbsp;=&nbsp;newOracleCachedRowSet(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<I><FONT color=#339900>//将ResultSet中的数据封装到RowSet中</FONT></I> 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;ors.populate(rs); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>return</FONT></B>&nbsp;ors; 
<LI>&nbsp;&nbsp;}<B><FONT color=#0000ff>finally</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;DBUtil.close(rs,&nbsp;pst,&nbsp;conn); 
<LI>&nbsp;&nbsp;} 
<LI>
<LI>
<LI><I><FONT color=#339900>//JSP显示部分代码</FONT></I> 
<LI>&lt;% 
<LI>&nbsp;&nbsp;javax.sql.<B><A href="http://www.javaresearch.org/source/jdk142/javax/sql/RowSet.java.html" target=_blank><FONT class=classLink><U>RowSet</U></FONT></A></B>&nbsp;empRS&nbsp;=&nbsp;(javax.sql.<B><A href="http://www.javaresearch.org/source/jdk142/javax/sql/RowSet.java.html" target=_blank><FONT class=classLink><U>RowSet</U></FONT></A></B>)&nbsp;request.getAttribute(“empRS”); 
<LI>%&gt; 
<LI>… 
<LI>&lt;table&nbsp;&nbsp;cellspacing=<FONT color=#ff33ff>"0"</FONT>&nbsp;width=”90%”&gt; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&nbsp;&nbsp;&lt;td&gt;代码&lt;/td&gt;&nbsp;&lt;td&gt;姓名&lt;/td&gt;&nbsp;&nbsp;&lt;/tr&gt; 
<LI>&lt;% 
<LI>&nbsp;&nbsp;<B><FONT color=#0000ff>if</FONT></B>&nbsp;(empRS&nbsp;!=&nbsp;<B><FONT color=#0000ff>null</FONT></B>)&nbsp;<B><FONT color=#0000ff>while</FONT></B>&nbsp;(empRS.next()&nbsp;)&nbsp;{ 
<LI>%&gt; 
<LI>&nbsp;&nbsp;&lt;tr&gt;&nbsp;&nbsp; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;%=&nbsp;empRS.get(“EMP_CODE”)%&gt;&lt;/td&gt;&nbsp; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;%=&nbsp;empRS.get(“REAL_NAME”)%&gt;&lt;/td&gt;&nbsp;&nbsp; 
<LI>&nbsp;&nbsp;&lt;/tr&gt; 
<LI>&lt;% 
<LI>&nbsp;&nbsp;}<I><FONT color=#339900>//&nbsp;end&nbsp;while</FONT></I> 
<LI>%&gt; 
<LI>&lt;/table&gt; </LI></OL></DIV><BR><BR><B>适用场合</B>：<BR>&nbsp;&nbsp;方法一使用于定制的查询操作<BR>&nbsp;&nbsp;方法二适用于多条查询语句或需要对查询结果进行处理的情况。<BR>&nbsp;&nbsp;方法三适合于单条查询语句，适用于快速开发。<BR><BR><BR><B>相关链接</B>：<BR>&nbsp;&nbsp;&nbsp;&nbsp;如果需要分页显示请参考：<A class=l2 href="http://www.javaresearch.org/article/showarticle.jsp?column=106&amp;thread=8893" target=_blank>JSP分页技术实现</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;如果查询结果需要生成WORD或者EXCEL，请参考：<A class=l2 href="http://www.javaresearch.org/article/showarticle.jsp?column=106&amp;thread=8444" target=_blank>使用jsp实现word、excel格式报表打印</A><BR><BR>附：<B>DBUtil代码</B>：<BR>
<DIV class=codeStyle>
<OL>
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.util.<B><A href="http://www.javaresearch.org/source/jdk142/java/util/List.java.html" target=_blank><FONT class=classLink><U>List</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.util.<B><A href="http://www.javaresearch.org/source/jdk142/java/util/ArrayList.java.html" target=_blank><FONT class=classLink><U>ArrayList</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.util.<B><A href="http://www.javaresearch.org/source/jdk142/java/util/Map.java.html" target=_blank><FONT class=classLink><U>Map</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.util.<B><A href="http://www.javaresearch.org/source/jdk142/java/util/HashMap.java.html" target=_blank><FONT class=classLink><U>HashMap</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.util.<B><A href="http://www.javaresearch.org/source/jdk142/java/util/Properties.java.html" target=_blank><FONT class=classLink><U>Properties</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.util.<B><A href="http://www.javaresearch.org/source/jdk142/java/util/Collections.java.html" target=_blank><FONT class=classLink><U>Collections</U></FONT></A></B>; 
<LI>
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/Connection.java.html" target=_blank><FONT class=classLink><U>Connection</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/SQLException.java.html" target=_blank><FONT class=classLink><U>SQLException</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/ResultSet.java.html" target=_blank><FONT class=classLink><U>ResultSet</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/ResultSetMetaData.java.html" target=_blank><FONT class=classLink><U>ResultSetMetaData</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/Statement.java.html" target=_blank><FONT class=classLink><U>Statement</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/PreparedStatement.java.html" target=_blank><FONT class=classLink><U>PreparedStatement</U></FONT></A></B>; 
<LI>
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;javax.naming.<B><A href="http://www.javaresearch.org/source/jdk142/javax/naming/Context.java.html" target=_blank><FONT class=classLink><U>Context</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;javax.naming.<B><A href="http://www.javaresearch.org/source/jdk142/javax/naming/InitialContext.java.html" target=_blank><FONT class=classLink><U>InitialContext</U></FONT></A></B>; 
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;javax.naming.<B><A href="http://www.javaresearch.org/source/jdk142/javax/naming/NamingException.java.html" target=_blank><FONT class=classLink><U>NamingException</U></FONT></A></B>; 
<LI>
<LI><B><FONT color=#0000ff>import</FONT></B>&nbsp;javax.sql.<B><A href="http://www.javaresearch.org/source/jdk142/javax/sql/DataSource.java.html" target=_blank><FONT class=classLink><U>DataSource</U></FONT></A></B>; 
<LI>
<LI><B><FONT color=#0000ff>public</FONT></B>&nbsp;<B><FONT color=#0000ff>class</FONT></B>&nbsp;DBUtil{ 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>private</FONT></B>&nbsp;<B><FONT color=#0000ff>static</FONT></B>&nbsp;<B><FONT color=#0000ff>final</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target=_blank><FONT class=classLink><U>String</U></FONT></A></B>&nbsp;JDBC_DATA_SOURCE&nbsp;=&nbsp;<FONT color=#ff33ff>"java:comp/env/jdbc/DataSource"</FONT>; 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<I><FONT color=#339900>/**</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enableLocalDebug:&nbsp;是否在本地调试。&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;值为true时如果查找数据源失败则使用DriverManager与数据库建立连接；</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果为false则只查找数据源建立数据库连接。</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;默认为false。&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可通过系统属性jdbc.enable_local_debug=true设置enableLocalDebug为true，启用本地调试：&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;增加JVM&nbsp;parameter：&nbsp;-Djdbc.enable_local_debug=true</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</FONT></I> 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>private</FONT></B>&nbsp;<B><FONT color=#0000ff>static</FONT></B>&nbsp;<B><FONT color=#0000ff>boolean</FONT></B>&nbsp;enableLocalDebug&nbsp;=&nbsp;<B><FONT color=#0000ff>false</FONT></B>; 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>static</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enableLocalDebug&nbsp;=&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/Boolean.java.html" target=_blank><FONT class=classLink><U>Boolean</U></FONT></A></B>.getBoolean&nbsp;(<FONT color=#ff33ff>"jdbc.enable_local_debug"</FONT>); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>private</FONT></B>&nbsp;<B><FONT color=#0000ff>static</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/javax/naming/Context.java.html" target=_blank><FONT class=classLink><U>Context</U></FONT></A></B>&nbsp;ctx&nbsp;=&nbsp;<B><FONT color=#0000ff>null</FONT></B>; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>private</FONT></B>&nbsp;<B><FONT color=#0000ff>static</FONT></B>&nbsp;javax.sql.<B><A href="http://www.javaresearch.org/source/jdk142/javax/sql/DataSource.java.html" target=_blank><FONT class=classLink><U>DataSource</U></FONT></A></B>&nbsp;ds&nbsp;=&nbsp;<B><FONT color=#0000ff>null</FONT></B>; 
<LI>
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>private</FONT></B>&nbsp;<B><FONT color=#0000ff>static</FONT></B>&nbsp;<B><FONT color=#0000ff>void</FONT></B>&nbsp;initDataSource()&nbsp;<B><FONT color=#0000ff>throws</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/Exception.java.html" target=_blank><FONT class=classLink><U>Exception</U></FONT></A></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<I><FONT color=#339900>//&nbsp;Put&nbsp;connection&nbsp;properties&nbsp;in&nbsp;to&nbsp;a&nbsp;hashtable.</FONT></I> 
<LI>
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>if</FONT></B>&nbsp;(ctx&nbsp;==&nbsp;<B><FONT color=#0000ff>null</FONT></B>)&nbsp;{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctx&nbsp;=&nbsp;<B><FONT color=#0000ff>new</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/javax/naming/InitialContext.java.html" target=_blank><FONT class=classLink><U>InitialContext</U></FONT></A></B>(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>if</FONT></B>&nbsp;(ds&nbsp;==&nbsp;<B><FONT color=#0000ff>null</FONT></B>)&nbsp;{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ds&nbsp;=&nbsp;(javax.sql.<B><A href="http://www.javaresearch.org/source/jdk142/javax/sql/DataSource.java.html" target=_blank><FONT class=classLink><U>DataSource</U></FONT></A></B>)&nbsp;ctx.lookup(JDBC_DATA_SOURCE); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<I><FONT color=#339900>/**</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;查找应用服务器数据源，从数据源中获得数据库连接。&lt;br&gt;&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;在本地调试时如果查找数据源失败并且enableLocalDebug==true</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;则根据系统属性使用java.sql.DriverManager建立连接。&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;本地调试时可配置的系统属性如下：&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&lt;p&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#jdbc驱动程序名&nbsp;&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jdbc.driver=&lt;i&gt;oracle.jdbc.driver.OracleDriver&lt;/i&gt;&nbsp;&lt;br&gt;&nbsp;&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#数据库连接串&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jdbc.url=&lt;i&gt;jdbc:oracle:thin:@10.1.1.1:1521:ocrl&lt;/i&gt;&nbsp;&lt;br&gt;&nbsp;&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#数据库用户名&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jdbc.username=&lt;i&gt;scott&lt;/i&gt;&nbsp;&lt;br&gt;&nbsp;&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#数据库用户密码&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jdbc.password=&lt;i&gt;tiger&lt;/i&gt;&nbsp;&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&lt;/p&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;可通过JVM参数设置上述系统属性：&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;-Djdbc.driver=oracle.jdbc.driver.OracleDriver&nbsp;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;-Djdbc.url=jdbc:oracle:thin:@10.1.1.1:1521:ocrl</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;-Djdbc.username=scott&nbsp;-Djdbc.password=tiger</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;Connection</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@throws&nbsp;NamingException&nbsp;如果数据源查找失败</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@throws&nbsp;SQLException&nbsp;如果建立数据库连接失败</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</FONT></I> 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>public</FONT></B>&nbsp;<B><FONT color=#0000ff>static</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/Connection.java.html" target=_blank><FONT class=classLink><U>Connection</U></FONT></A></B>&nbsp;getConnection()&nbsp;<B><FONT color=#0000ff>throws</FONT></B>&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/SQLException.java.html" target=_blank><FONT class=classLink><U>SQLException</U></FONT></A></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>try</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initDataSource(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>return</FONT></B>&nbsp;ds.getConnection(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<B><FONT color=#0000ff>catch</FONT></B>(<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/SQLException.java.html" target=_blank><FONT class=classLink><U>SQLException</U></FONT></A></B>&nbsp;sqle){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>throw</FONT></B>&nbsp;sqle; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<B><FONT color=#0000ff>catch</FONT></B>(<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/Exception.java.html" target=_blank><FONT class=classLink><U>Exception</U></FONT></A></B>&nbsp;ne){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>if</FONT></B>&nbsp;(enableLocalDebug){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>return</FONT></B>&nbsp;getTestConn(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<B><FONT color=#0000ff>else</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>throw</FONT></B>&nbsp;<B><FONT color=#0000ff>new</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/RuntimeException.java.html" target=_blank><FONT class=classLink><U>RuntimeException</U></FONT></A></B>(ne.toString()); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<I><FONT color=#339900>//通过DriverManager建立本地测试连接</FONT></I> 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>private</FONT></B>&nbsp;<B><FONT color=#0000ff>static</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/Connection.java.html" target=_blank><FONT class=classLink><U>Connection</U></FONT></A></B>&nbsp;getTestConn(){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>try</FONT></B>&nbsp;{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target=_blank><FONT class=classLink><U>String</U></FONT></A></B>&nbsp;driver&nbsp;=&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target=_blank><FONT class=classLink><U>System</U></FONT></A></B>.getProperty(<FONT color=#ff33ff>"jdbc.driver"</FONT>); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target=_blank><FONT class=classLink><U>System</U></FONT></A></B>.out.println(<FONT color=#ff33ff>"jdbc.driver="</FONT>+driver); 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target=_blank><FONT class=classLink><U>String</U></FONT></A></B>&nbsp;url&nbsp;=&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target=_blank><FONT class=classLink><U>System</U></FONT></A></B>.getProperty(<FONT color=#ff33ff>"jdbc.url"</FONT>); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target=_blank><FONT class=classLink><U>System</U></FONT></A></B>.out.println(<FONT color=#ff33ff>"jdbc.url="</FONT>+url); 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target=_blank><FONT class=classLink><U>String</U></FONT></A></B>&nbsp;userName&nbsp;=&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target=_blank><FONT class=classLink><U>System</U></FONT></A></B>.getProperty(<FONT color=#ff33ff>"jdbc.username"</FONT>); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target=_blank><FONT class=classLink><U>System</U></FONT></A></B>.out.println(<FONT color=#ff33ff>"jdbc.username="</FONT>+userName); 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/String.java.html" target=_blank><FONT class=classLink><U>String</U></FONT></A></B>&nbsp;password&nbsp;=&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target=_blank><FONT class=classLink><U>System</U></FONT></A></B>.getProperty(<FONT color=#ff33ff>"jdbc.password"</FONT>); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/System.java.html" target=_blank><FONT class=classLink><U>System</U></FONT></A></B>.out.println(<FONT color=#ff33ff>"jdbc.password="</FONT>+password); 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/Class.java.html" target=_blank><FONT class=classLink><U>Class</U></FONT></A></B>.forName(driver).newInstance(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>return</FONT></B>&nbsp;java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/DriverManager.java.html" target=_blank><FONT class=classLink><U>DriverManager</U></FONT></A></B>.getConnection(url,&nbsp;userName,&nbsp;password); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>catch</FONT></B>&nbsp;(<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/Exception.java.html" target=_blank><FONT class=classLink><U>Exception</U></FONT></A></B>&nbsp;ex)&nbsp;{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>throw</FONT></B>&nbsp;<B><FONT color=#0000ff>new</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/lang/RuntimeException.java.html" target=_blank><FONT class=classLink><U>RuntimeException</U></FONT></A></B>(ex.getMessage()); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<I><FONT color=#339900>/**</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;将查询结果封装成List。&lt;br&gt;</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;List中元素类型为封装一行数据的Map，Map&nbsp;key为字段名（大写），value为相应字段值</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;rs&nbsp;ResultSet</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;List</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@throws&nbsp;java.sql.SQLException</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</FONT></I> 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>public</FONT></B>&nbsp;<B><FONT color=#0000ff>static</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/util/List.java.html" target=_blank><FONT class=classLink><U>List</U></FONT></A></B>&nbsp;resultSetToList(<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/ResultSet.java.html" target=_blank><FONT class=classLink><U>ResultSet</U></FONT></A></B>&nbsp;rs)&nbsp;<B><FONT color=#0000ff>throws</FONT></B>&nbsp;java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/SQLException.java.html" target=_blank><FONT class=classLink><U>SQLException</U></FONT></A></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>if</FONT></B>&nbsp;(rs==<B><FONT color=#0000ff>null</FONT></B>)&nbsp;<B><FONT color=#0000ff>return</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/util/Collections.java.html" target=_blank><FONT class=classLink><U>Collections</U></FONT></A></B>.EMPTY_LIST; 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/ResultSetMetaData.java.html" target=_blank><FONT class=classLink><U>ResultSetMetaData</U></FONT></A></B>&nbsp;md&nbsp;=&nbsp;rs.getMetaData(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>int</FONT></B>&nbsp;columnCount&nbsp;=&nbsp;md.getColumnCount(); 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/util/List.java.html" target=_blank><FONT class=classLink><U>List</U></FONT></A></B>&nbsp;list&nbsp;=&nbsp;<B><FONT color=#0000ff>new</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/util/ArrayList.java.html" target=_blank><FONT class=classLink><U>ArrayList</U></FONT></A></B>(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/util/Map.java.html" target=_blank><FONT class=classLink><U>Map</U></FONT></A></B>&nbsp;rowData; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>while</FONT></B>&nbsp;(rs.next()){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rowData&nbsp;=&nbsp;<B><FONT color=#0000ff>new</FONT></B>&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/util/HashMap.java.html" target=_blank><FONT class=classLink><U>HashMap</U></FONT></A></B>(columnCount); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>for</FONT></B>&nbsp;(<B><FONT color=#0000ff>int</FONT></B>&nbsp;i=1;&nbsp;i&lt;=columnCount;&nbsp;i++){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rowData.put(md.getColumnName(i),rs.getObject(i)); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add(rowData); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>return</FONT></B>&nbsp;list; 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<I><FONT color=#339900>/**</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;关闭ResultSet、Statement和Connection</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;rs&nbsp;ResultSet&nbsp;to&nbsp;be&nbsp;closed</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;stmt&nbsp;Statement&nbsp;or&nbsp;PreparedStatement&nbsp;&nbsp;to&nbsp;be&nbsp;closed</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;conn&nbsp;Connection&nbsp;&nbsp;to&nbsp;be&nbsp;closed</FONT></I> 
<LI><I><FONT color=#339900>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</FONT></I> 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>public</FONT></B>&nbsp;<B><FONT color=#0000ff>static</FONT></B>&nbsp;<B><FONT color=#0000ff>void</FONT></B>&nbsp;close(<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/ResultSet.java.html" target=_blank><FONT class=classLink><U>ResultSet</U></FONT></A></B>&nbsp;rs,&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/Statement.java.html" target=_blank><FONT class=classLink><U>Statement</U></FONT></A></B>&nbsp;stmt,&nbsp;<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/Connection.java.html" target=_blank><FONT class=classLink><U>Connection</U></FONT></A></B>&nbsp;conn){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>if</FONT></B>&nbsp;(rs&nbsp;!=&nbsp;<B><FONT color=#0000ff>null</FONT></B>)&nbsp;<B><FONT color=#0000ff>try</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rs.close(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<B><FONT color=#0000ff>catch</FONT></B>(java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/SQLException.java.html" target=_blank><FONT class=classLink><U>SQLException</U></FONT></A></B>&nbsp;ex){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>if</FONT></B>&nbsp;(stmt&nbsp;!=&nbsp;<B><FONT color=#0000ff>null</FONT></B>)&nbsp;<B><FONT color=#0000ff>try</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stmt.close(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<B><FONT color=#0000ff>catch</FONT></B>(java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/SQLException.java.html" target=_blank><FONT class=classLink><U>SQLException</U></FONT></A></B>&nbsp;ex){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT color=#0000ff>if</FONT></B>&nbsp;(conn&nbsp;!=&nbsp;<B><FONT color=#0000ff>null</FONT></B>)&nbsp;<B><FONT color=#0000ff>try</FONT></B>{ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conn.close(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<B><FONT color=#0000ff>catch</FONT></B>(java.sql.<B><A href="http://www.javaresearch.org/source/jdk142/java/sql/SQLException.java.html" target=_blank><FONT class=classLink><U>SQLException</U></FONT></A></B>&nbsp;ex){ 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace(); 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>&nbsp;&nbsp;&nbsp;&nbsp;} 
<LI>
<LI>}<I><FONT color=#339900>//&nbsp;end&nbsp;of&nbsp;DBUtil</FONT></I> </LI></OL></DIV><img src ="http://www.blogjava.net/lmsun/aggbug/14127.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-09-26 17:24 <a href="http://www.blogjava.net/lmsun/articles/14127.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用JAVA实现一个分页类</title><link>http://www.blogjava.net/lmsun/articles/14123.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Mon, 26 Sep 2005 08:59:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/14123.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/14123.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/14123.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/14123.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/14123.html</trackback:ping><description><![CDATA[<P>这里我以从一个用户表中查询用户信息为例演示其用法：</P>
<P>1.将PageResultSet.java文件编译成class文件，并放入你的Web<BR>应用程序的WEB-INF/classes/com/youngor/util目录下，可以对包名做相应修改。</P>
<P>2.在你的Action类中:<BR>先从业务处理逻辑类中取出数据(ArrayList或Vector格式)<BR>UserBO userBO=new UserBO();<BR>Collection data=userBO.findUsers();//示例方法<BR>再得到当前页curPage和每页记录数pageSize<BR>int curPage = Integer.parseInt(request.getParameter(“cur_page“));<BR>int pageSize=15;<BR>然后生成PageResultSet对象<BR>PageResultSet dataList = new PageResultSet(data, curPage, pageSize);<BR>request.setAttribute("usersList", usersList);</P>
<P>3.在你的JSP页面中:<BR>&nbsp;&nbsp; &lt;%<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PageResultSet pageResultSet=(PageResultSet)request.getAttribute("usersList");<BR>&nbsp;&nbsp; ArrayList usersList=(ArrayList)pageResultSet.getData();<BR>&nbsp;&nbsp; for(int i=0;i&lt;usersList.size();i++)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UserEO userEO=(UserEO)usersList.get(i);%&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;a href="view_user.do?id=&lt;%=userEO.getId()%&gt;"&gt;&lt;%=userEO.getUsername()%&gt;&lt;/a&gt;&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;%=userEO.getName()%&gt;&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;%=userEO.getPhoneNumber()%&gt;&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;%=userEO.getEmailBox()%&gt;&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;%=userEO.getAddress()%&gt;&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;%=userEO.getPostcode()%&gt;&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;%}%&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/table&gt;&lt;/td&gt;<BR>&nbsp; &lt;/tr&gt;<BR>&lt;/table&gt;</P>
<P>&lt;!-- 显示分页工具栏 --&gt;</P>
<P>&lt;%=pageResultSet.getToolBar("list_users.do")%&gt;</P>
<P><BR>注意：<BR>1、如果你觉得分页工具栏不能满足你的要求，可以用PageResultSet类中的公共方法<BR>first()、previous()、next()、last()定制自己的工具栏，并且，你还可以在PageResultSet中定义多个样式的工具栏；<BR>2、getToolBar(String url)方法接受带查询字符串的参数，比如“list_users.do?class_id=1“。</P>
<P>&nbsp;</P>
<P>//PageResultSet.java<BR>package com.youngor.util;</P>
<P>import java.util.*;</P>
<P>/**<BR>&nbsp;* &lt;p&gt;Title: PageResultSet&lt;/p&gt;<BR>&nbsp;*<BR>&nbsp;* &lt;p&gt;Description:分页类 &lt;/p&gt;<BR>&nbsp;*<BR>&nbsp;* &lt;p&gt;Copyright: Copyright (c) 2004&lt;/p&gt;<BR>&nbsp;*<BR>&nbsp;* &lt;p&gt;Company:youngor-studio(<A href="http://www.54youngor.com">http://www.54youngor.com</A>) &lt;/p&gt;<BR>&nbsp;* @author:伍维波<BR>&nbsp;* @version 1.0<BR>&nbsp;*/<BR>public class PageResultSet {<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 分页数据<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; private Collection data = null;<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 当前页<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; private int curPage;<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 每页显示的记录数<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; private int pageSize;<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 记录行数<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; private int rowsCount;<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 页数<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; private int pageCount;</P>
<P>&nbsp;&nbsp;&nbsp; public PageResultSet(Collection data) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.data = data;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.curPage = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.pageSize = 10;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.rowsCount = data.size();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; public PageResultSet(Collection data, int curPage) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.data = data;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.curPage = curPage;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.pageSize = 10;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.rowsCount = data.size();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; public PageResultSet(Collection data, int curPage, int pageSize) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.data = data;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.curPage = curPage;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.pageSize = pageSize;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.rowsCount = data.size();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * getCurPage:返回当前的页数<BR>&nbsp;&nbsp;&nbsp;&nbsp; *<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return int<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public int getCurPage() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return curPage;<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * getPageSize：返回分页大小<BR>&nbsp;&nbsp;&nbsp;&nbsp; *<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return int<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public int getPageSize() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return pageSize;<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * getRowsCount：返回总记录行数<BR>&nbsp;&nbsp;&nbsp;&nbsp; *<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return int<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public int getRowsCount() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return rowsCount;<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * getPageCount：返回总页数<BR>&nbsp;&nbsp;&nbsp;&nbsp; *<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return int<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public int getPageCount() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return pageCount;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 第一页<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return int<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public int first() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 最后一页<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return int<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public int last() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return pageCount;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 上一页<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return int<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public int previous() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (curPage - 1 &lt; 1) ? 1 : curPage - 1;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 下一页<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return int<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public int next() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (curPage + 1 &gt; pageCount) ? pageCount : curPage + 1;<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 第一页<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return boolean<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public boolean isFirst() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (curPage==1)?true:false;<BR>&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 第一页<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return boolean<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public boolean isLast() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (curPage==pageCount)?true:false;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 获取当前页数据<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return Collection<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public Collection getData() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Collection curData = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (data != null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int start = (curPage - 1) * pageSize;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int end = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (start + pageSize &gt; rowsCount)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end = rowsCount;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end = start + pageSize;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList arrayCurData = new ArrayList();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList arrayData = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Vector vectorCurData = new Vector();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Vector vectorData = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean isArray = true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (data instanceof ArrayList) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arrayData = (ArrayList) data;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isArray = true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else if (data instanceof Vector) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vectorData = (Vector) data;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isArray = false;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = start; i &lt; end; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (isArray) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arrayCurData.add(arrayData.get(i));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vectorData.add(vectorData.elementAt(i));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (isArray) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curData = (Collection) arrayCurData;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curData = (Collection) vectorCurData;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return curData;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; /**<BR>&nbsp;&nbsp;&nbsp;&nbsp; * 获取工具条<BR>&nbsp;&nbsp;&nbsp;&nbsp; * @return String<BR>&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp; public String getToolBar(String fileName){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String temp="";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(fileName.indexOf("?")==-1)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp="?";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp="&amp;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String str="&lt;form method='post' name='frmPage' action='"+fileName+"'&gt;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="&lt;p align='center'&gt;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(isFirst())<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="首页 上一页&amp;nbsp;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="&lt;a href='"+fileName+temp+"cur_page=1'&gt;首页&lt;/a&gt;&amp;nbsp;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="&lt;a href='"+fileName+temp+"cur_page="+(curPage-1)+"'&gt;上一页&lt;/a&gt;&amp;nbsp;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(isLast())<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="下一页 尾页&amp;nbsp;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="&lt;a href='"+fileName+temp+"cur_page="+(curPage+1)+"'&gt;下一页&lt;/a&gt;&amp;nbsp;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="&lt;a href='"+fileName+temp+"cur_page="+pageCount+"'&gt;尾页&lt;/a&gt;&amp;nbsp;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="&amp;nbsp;共&lt;b&gt;"+rowsCount+"&lt;/b&gt;条记录&amp;nbsp;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="&amp;nbsp;转到&lt;select name='page' onChange=\"location='"+fileName+temp+"cur_page='+this.options[this.selectedIndex].value\"&gt;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(int i=1;i&lt;=pageCount;i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(i==curPage)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="&lt;option value='"+i+"' selected&gt;第"+i+"页&lt;/option&gt;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="&lt;option value='"+i+"'&gt;第"+i+"页&lt;/option&gt;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str+="&lt;/select&gt;&lt;/p&gt;&lt;/form&gt;";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return str;<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR></P><img src ="http://www.blogjava.net/lmsun/aggbug/14123.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-09-26 16:59 <a href="http://www.blogjava.net/lmsun/articles/14123.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Security for J2EE Applications</title><link>http://www.blogjava.net/lmsun/articles/13618.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Wed, 21 Sep 2005 03:14:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/13618.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/13618.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/13618.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/13618.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/13618.html</trackback:ping><description><![CDATA[<P><FONT face=Arial,Helvetica,Verdana,sans-serif size=3><B>Listing 1</B><BR><BR>&nbsp;&nbsp; &lt;html&gt;<BR>&nbsp;&nbsp; &lt;head&gt;<BR>&nbsp;&nbsp; &lt;/head&gt;<BR>&nbsp;&nbsp; &lt;body onLoad="document.myForm.submit()"&gt;<BR>&nbsp;&nbsp; &lt;form action="https://loginserver.yourcorp.com/webapp/login<BR>&nbsp;&nbsp; servlet" name="myForm" method="POST"&gt;<BR>&nbsp;&nbsp; &lt;input type="hidden" name="key" value="!@#$EncryptedString!@#$"&gt;<BR>&nbsp;&nbsp; &lt;/form&gt;<BR>&nbsp;&nbsp; &lt;/body&gt;<BR>&nbsp;&nbsp; &lt;/html&gt;<BR><BR><BR><B>Listing 2</B><BR><BR>&lt;html&gt;<BR>&lt;head&gt;<BR>&lt;title&gt;Hello World</TITLE><BR>&lt;SCRIPT language="JavaScript" SRC="https://partner1/servlet/LCMMSServlet/login?data=ALKSDFJQWER...JLQKWE"&gt;<BR>&lt;/SCRIPT&gt;<BR>&lt;SCRIPT language="JavaScript"<BR>SRC="https://partner2/servlet/LCMMSServlet/login?data=ALKSDFJQWER...JLQKWE"&gt;<BR>&lt;/SCRIPT&gt;<BR>&lt;SCRIPT language="JavaScript"&gt;<BR>function postForm() {<BR>&nbsp;&nbsp; document.myForm.submit( );<BR>}<BR>&lt;/SCRIPT&gt;<BR>&lt;/head&gt;<BR><BR>&lt;body bgcolor=#FFFFFF onLoad="postForm()"&gt;<BR>&lt;form action="https://myserver/servlet/LCMMSServlet/authenticated" <BR>method="POST" name="myForm"&gt;<BR>&lt;input type="hidden" name="data" value="ALKSDFJQWER...JLQKWE"&gt;<BR>&lt;input type="hidden" name="url" value="/requested/url?param1=val1¶m2=val2"&gt;<BR>&lt;/form&gt;<BR>&lt;/body&gt;<BR>&lt;/html&gt;&nbsp; <BR><BR><BR><BR><B>Listing 3</B><BR><BR>&lt;FRAMESET ROWS="100%,0%,0%" onLoad="submitViewableFrameForm()"&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;FRAME NAME="viewable" SRC="TempFrame.jsp"&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;!--The frame below logs in the browser to partner1 --&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;FRAME NAME="setPartner1cookie"<BR> <BR>SRC="https://partner1:7002/servlet/LCMMSServlet/login?data=ALKSDFJQWER...JLQKWE"&gt; <BR><BR><BR>&nbsp;&nbsp;&nbsp; &lt;!--The frame below logs in the browser to partner2 --&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;FRAME NAME="setPartner2cookie"<BR> <BR>SRC="https://partner2:7002/servlet/LCMMSServlet/login?data=ALKSDFJQWER...JLQKWE"&gt;<BR>&lt;/FRAMESET&gt;<BR><BR><BR><BR><B>Listing 4</B><BR><BR>&lt;html&gt;<BR>&lt;head&gt;<BR>&lt;/head&gt;<BR>&lt;body bgcolor=#FFFFFF&gt;<BR>Put some text here like "Logging in...Please wait."<BR>&lt;form action="https://myserver/servlet/LCMMSServlet/authenticated"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; method="POST" name="myForm" target="_top"&gt;<BR>&lt;input type="hidden" name="data" value="ALKSDFJQWER...JLQKWE"&gt;<BR>&lt;input type="hidden" name="url" value="/requested/url?param1=val1¶m2=val2"&gt;<BR>&lt;/form&gt;<BR>&lt;/body&gt;<BR>&lt;/html&gt;<BR><BR><BR><BR><B>Listing 5</B><BR><BR>CryptTool ct = CryptToolFactory.getCryptTool( ... );<BR>Properties p = new Properties ( );<BR>... //Get user Id<BR>String userId = ...;<BR>p.setProperty ("uid",userId);<BR>p.setProperty("anotherProp", someValue);<BR><BR><BR>//The String returned is a hex encoded ciphertext<BR>String encryptedInfo = ct.encrypt(p);<BR>Cookie c = new Cookie ("SSO",encryptedInfo);<BR>c.setMaxAge(-1);<BR>c.setDomain(".yourDomain.com");<BR>c.setPath("/");<BR><BR><BR>//If this is a login server cookie and cookie has to be sent over SSL<BR>c.setSecure(true);<BR><BR><BR>//Send cookie to client<BR>response.addCookie (c );<BR><BR><BR><BR><B>Listing 6</B><BR><BR>package jdj.sso.test;<BR><BR><BR>import java.net.*;<BR>import java.io.*;<BR><BR><BR>public class HttPSocketClient {<BR><BR><BR>&nbsp;&nbsp; public static void main(String[] args) throws Exception {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String host = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int port = -1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String path = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; args.length; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(args[i]);<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (args.length &lt; 3) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "USAGE: java HttPSocketClient " +<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "host port requestedfilepath");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.exit(-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; host = args[0];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; port = Integer.parseInt(args[1]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; path = args[2];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IllegalArgumentException e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("USAGE: java HttPSocketClient " +<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "host port requestedfilepath");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.exit(-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR><BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Socket socket = new Socket(host,port);<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PrintWriter out = new PrintWriter(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new BufferedWriter(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new OutputStreamWriter(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; socket.getOutputStream())));<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.println("GET " + path + " HTTP/1.1");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.println();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.flush();<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BufferedReader in = new BufferedReader(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new InputStreamReader(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; socket.getInputStream()));<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String inputLine;<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ((inputLine = in.readLine()) != null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(inputLine);<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in.close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; socket.close();<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp; }<BR>}<BR><BR><BR><BR><B>Listing 7</B><BR><BR>grant CodeBase "file:./Login.jar" {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; permission java.security.AllPermission;<BR>};<BR><BR><BR>grant CodeBase "file:./HttpSocketClient.jar",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Principal javax.security.auth.kerberos.KerberosPrincipal<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "your_kerb_username@your_realm" {<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; permission java.net.SocketPermission "*", "connect";<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; permission javax.security.auth.kerberos.ServicePermission<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "krbtgt/your_realm@your_realm",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "initiate";<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; permission javax.security.auth.kerberos.ServicePermission<BR><BR><BR>"server_service_principal@your_realm",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "initiate";<BR>};<BR><BR>&nbsp; <BR></FONT></P><img src ="http://www.blogjava.net/lmsun/aggbug/13618.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-09-21 11:14 <a href="http://www.blogjava.net/lmsun/articles/13618.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>利用 Jsp+Taglib+JavaBean 快速构建 动态数据库查询模板</title><link>http://www.blogjava.net/lmsun/articles/11744.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Thu, 01 Sep 2005 05:39:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/11744.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/11744.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/11744.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/11744.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/11744.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 本文主要介绍如何快速利用 jsp+taglib+javaBean 构建动态数据库查询模板的全过程,已经如和使用,扩展该模板的方法.&nbsp;&nbsp;&nbsp; 本模板所要实现的功能是: 通过非常简单地继承一个模板中的方法产生类,和添加一个含有Tag的简单JSP来实现动态数据库查询. 废话不说了, 现在开始.1 配置 Tomcat5.0 (或以上版本,我使用了JSP2.0版本的一...&nbsp;&nbsp;<a href='http://www.blogjava.net/lmsun/articles/11744.html'>阅读全文</a><img src ="http://www.blogjava.net/lmsun/aggbug/11744.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-09-01 13:39 <a href="http://www.blogjava.net/lmsun/articles/11744.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>安装配置Jboss</title><link>http://www.blogjava.net/lmsun/articles/11609.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Wed, 31 Aug 2005 02:51:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/11609.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/11609.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/11609.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/11609.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/11609.html</trackback:ping><description><![CDATA[<STRONG>1、 Jboss的配置安装<BR>1.1、Jboss的下载</STRONG><BR>Jboss的下载地址为：http://www.jboss.org/。目前的最新版本是：Jboss2.2.1。建议你下载Jboss2.2.1和tomcat3.2.1集成的下载包，这样避免了单个下载后两者之间配置的问题。下载地址是：http://prdownloads.sourceforge.net/jboss/JBoss-2.2.1_Tomcat-3.2.1.zip<BR><BR>下载完成后，解压到一个目录，这个目录名为E:\program files\jb_tom（这个目录名是笔者自定的）。下面有如下子目录:<BR><BR>E:\program files\jb_tom\jboss和E:\jb_tom\tomcat<BR><BR>注意：这里的目录稍微做了改动，建议你将TOMCAT_HOME加到CLASSPATH中，否则E:\program files\jb_tom\jboss里的run_with_tomcat.bat这个文件要做相应改动才能正常运行。<BR><BR><B>1.2、配置运行</B><BR>在启动Jboss前应该先安装好JDK，建议安装JDK1.3以上的版本（目前最新的正式版本是JDK1.3.1），然后将系统的Classpath设置好。Jboss不要任何配置和修改，当然，最好将TOMCAT_HOME加到Classpath中，这样，E:\program files\jb_tom\jboss里的run_with_tomcat.bat这个文件前的TOMCAT_HOME就可以去掉了。<BR><BR>运行E:\program files\jb_tom\jboss里的run_with_tomcat.bat这个文件，这样，Tomcat端口在8080，Jboss为8083，运行http://localhost:8080/将出现tomcat首页，运行http://localhost:8083/将出现无错误的空白页。<BR><BR><FONT size=3><B>2、 测试<A href="http://dev.21tx.com/java/ejb/" target=_blank><FONT color=#3366cc>EJB</FONT></A></B></FONT><BR><B>2.1、启动JBOSS：</B><BR>方法么，这里就不多作描述了，因为前面已经写过了^&amp;^。<BR><BR><B>2.2、下载并配置EJB例程</B><BR>到http://www.wodejia.net/softdownload/java/interestejb.zip下载interestejb.zip，这就是我们用于测试的EJB例程。将压缩包解开，出现如下目录：<BR><BR>interest/com<BR><BR>interest/docs<BR><BR>……<BR><BR>将该目录下所有文件全部copy到jboss安装目录下的examples目录下，如果没有examples目录，就自己建立一个，结构如下：<BR><BR>E:\ program files\jb_tom \jboss\examples\interest...<BR><BR>将文件E:\program files\jb_tom\jboss\examples\interest\interest.jar复制到：E:\program files\jb_tom\jboss\deploy下。<BR><BR>在Classpath中加入如下文件：e:\program files\jb_tom\tomcat\lib\servlet.jar;e:\program files\jb_tom\jboss\client\jboss-client.jar;e:\program files\jb_tom\jboss\client\jnp-client.jar;e:\program files\jb_tom\jboss\lib\ext\ejb.jar;e:\program files\jb_tom\jboss\examples\interest\interest-client.jar<BR><BR>为了测试Client通过servlet调用EJB，必须将： E:\program files\jb_tom\jboss\examples\interest下EJB.class和EJB.java复制到：<BR><BR>E:\program files\jb_tom\tomcat\webapps\ROOT\WEB-INF\classes目录下<BR><BR>将目录E:\jb_tom\jboss\examples\interest\com以及此下的所有文件都复制到E:\program files\jb_tom\tomcat\webapps\ROOT\WEB-INF\classes下。<BR><BR>重新启动JBOSS_TOMCAT。<BR><BR><B>2.3、command下client测试运行：</B><BR>command下，进入目录E:\ program files\jb_tom\jboss\examples\interest<BR><BR>java InterestClient<BR><BR>将出现：<BR><BR>Got context<BR><BR>Got reference <BR><BR>Interest on 1000 units, at 10% per period, compounded over 2 periods is: 210.00000000000023 <BR><BR>Jboss窗口将出现：<BR><BR>[Interest] Someone called `calculateCompoundInterest!` <BR><BR><B>2.4、web下client通过Servlet测试运行：</B><BR>http://localhost:8080/servlet/EJB<BR><BR>将出现：<BR><BR>Interest on 1000 units, at 10% per period, compounded over 2 periods is: 210.00000000000023 <BR><BR>Jboss窗口将出现：<BR><BR>[Interest] Someone called `calculateCompoundInterest!` <BR><BR>到此已经成功了。 <BR><BR>注意：这里将JBoss-2.2.1_Tomcat-3.2.1.zip解压后复制到E:\program files\jb_tom目录下<BR><img src ="http://www.blogjava.net/lmsun/aggbug/11609.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-08-31 10:51 <a href="http://www.blogjava.net/lmsun/articles/11609.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于打印</title><link>http://www.blogjava.net/lmsun/articles/11407.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Mon, 29 Aug 2005 01:51:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/11407.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/11407.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/11407.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/11407.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/11407.html</trackback:ping><description><![CDATA[<P>1<BR>方式：采用css来控制打印，提供一个专门的控制打印的css文件，然后在需要打印的页面的代码中将该css文件引入<BR>下面通过例子来讲解如何实现页面的打印：<BR>1．写控制打印的样式表文件print.css，文件内容如下：<BR>@media print { <BR>header{display:none}<BR>button{display:none}<BR>textarea{display:none} <BR>select{display:none} <BR>.noprint{display:none;<BR>&nbsp;position: relative;<BR>&nbsp;right:0px} <BR>input{position: relative;left:0px;<BR>&nbsp; right:19px;<BR>&nbsp; BORDER-TOP: 0px;<BR>&nbsp; BORDER-LEFT: 0px;<BR>&nbsp; BORDER-RIGHT: 0px;<BR>&nbsp; BORDER-BOTTOM: 0px; <BR>&nbsp; COLOR : #FFFFFF; <BR>&nbsp; display:""}<BR>select{position: relative;left:-50px;<BR>&nbsp;&nbsp; border:0pt;<BR>&nbsp;&nbsp; vertical-align:justify; <BR>&nbsp;&nbsp; display:""}<BR>&nbsp; table{<BR>&nbsp;&nbsp;&nbsp; position: relative;left:-40px;<BR>&nbsp;&nbsp; MARGIN-TOP: 1px; <BR>&nbsp;&nbsp; MARGIN-BOTTOM: 1px; <BR>&nbsp;&nbsp; PADDING-BOTTOM: 1px;<BR>&nbsp;&nbsp; BORDER-TOP: 1px;<BR>&nbsp;&nbsp; BORDER-LEFT: 1px;<BR>&nbsp;&nbsp; BORDER-RIGHT: 1px;<BR>&nbsp;&nbsp; BORDER-BOTTOM: 1px; <BR>&nbsp;&nbsp; COLOR : #FFFFFF;&nbsp;&nbsp; <BR>&nbsp;&nbsp; display:""<BR>&nbsp;&nbsp; }&nbsp;&nbsp; <BR>.print{position: relative;left:-50px;<BR>&nbsp;&nbsp; right:10px;<BR>&nbsp;&nbsp; display:""<BR>&nbsp;&nbsp; }<BR>}<BR>其中，@media print中的print指定了需要使用的设备，就是—打印机（网络打印机）。<BR>.noprint{display:none; position: relative; right:0px} <BR>说明其上面的所有元素都不打印，所以display属性设置为none. <BR>位置position属性设置为relative，表示相对位置。如果不设置为relative，则打印出来的效果会和页面上的位置一模一样，如果确实需要和页面一致的话，当然可以不设置成relative。Right属性以及它的值表示打印出来的位置相对于页面原始位置的偏移方向以及偏移量，当然还可以是left.<BR>根据上面的定义，header,button,textarea和select是不打印的，当然这些都是根据需要可以添加和修改的。<BR>接下来就是需要打印的元素的定义了，其各个属性的含义都是顾名思义的，很容易理解，在此不做赘述，只需要记住将display属性的值这是为空串””就可以了。<BR>2．在需要打印的页面的代码中，将该样式表文件引入：<BR>&lt;head&gt;<BR>&lt;link rel=stylesheet href="print.css"&gt; <BR>………………………………<BR>&lt;/head&gt;<BR>其中，href属性指定的是样式表文件的位置。<BR>3．在页面代码中加入使用打印API的代码，一般来说加在&lt;body&gt;……..&lt;/body&gt;之间即可：<BR>&lt;OBJECT ID="WebBrowser" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"&gt;&lt;/OBJECT&gt;<BR>这段代码是必不可少的，不必了解其确切含义。<BR>4．用js写执行打印动作的函数，一个简单的例子如下：<BR>function doPrint()<BR>{<BR>WebBrowser.ExecWB(6,6);<BR>}<BR>其中的WebBrowser.ExecWB(6,6);语句表示执行打印。还有其他的一些功能，如下：<BR>WebBrowser.ExecWB(1,1) 打开 <BR>WebBrowser.ExecWB(2,1) 关闭现在所有的IE窗口，并打开一个新窗口 <BR>WebBrowser.ExecWB(4,1) 保存网页 <BR>WebBrowser.ExecWB(6,6) 打印 <BR>WebBrowser.ExecWB(7,1) 打印预览 <BR>WebBrowser.ExecWB(8,1) 打印页面设置 <BR>WebBrowser.ExecWB(10,1) 查看页面属性 <BR>WebBrowser.ExecWB(15,1) 好像是撤销，有待确认 <BR>WebBrowser.ExecWB(17,1) 全选 <BR>WebBrowser.ExecWB(22,1) 刷新 <BR>WebBrowser.ExecWB(45,1) 关闭窗体无提示<BR>5．在页面代码中添加打印按钮：<BR>&lt;input type=button value="toPrinter" onclick="doPrint()"&gt;<BR>6．调整页面元素，控制打印细节。具体做法是：如果想打印某个页面元素，只需要将该元素放在&lt;div class=print&gt;…&lt;/div&gt;中；如果不想打印，则将该元素放在&lt;div class=noprint&gt;…&lt;/div&gt;中。</P>
<P>做到以上几点，一个简单的打印功能就实现了！<BR><BR><BR><BR>2<BR><BR>&lt;%@ page contentType="application/msword;charset=GBK" %&gt;</P><img src ="http://www.blogjava.net/lmsun/aggbug/11407.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-08-29 09:51 <a href="http://www.blogjava.net/lmsun/articles/11407.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>快速配置weblogic8.X的应用目录</title><link>http://www.blogjava.net/lmsun/articles/11069.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Thu, 25 Aug 2005 07:45:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/11069.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/11069.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/11069.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/11069.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/11069.html</trackback:ping><description><![CDATA[我们知道在weblogic7.x之前，安装完后weblogic会自动创建默认的应用目录DefaultWebApp。如果没有特别的需要，就可以利用这个默认的应用目录布署Web应用程序或者J2EE系统了。而在weblogic8.x之后版本中，它不会自动创建默认的应用目录。所以我们需要使用Configuration Wizard来创建自己的应用目录，发布应用目录有两种方法，现分别介绍。 
<P>1.weblogic8.x安装 weblogic8.x有几个版本提供使用，并且从这个版本出现了中文版。例子中使用了英文版。weglogic安装很简单，只需要按默认值一路下一步就OK了。 </P>
<P>2.创建weblogic服务 安装成功后，依次点击”开始”-&gt;”BEA WebLogic Platform 8.1”-&gt;”Configuration Wizard”，启动”BEA WebLogic Configuration Wizard”,选择”create a new weblogic configuration”，然后”next”,在”template”选择”base weblogic server domain”,然后”next”,没有特别的需要就不修改默认选择，然后”next”，在这个界面上输入用户名、密码和创建这个服务的描述（请记住这个用户密码它是启动这个服务和进入服务控制台的帐号）,然后”next”，选择jdk的版本,然后”next”，在这个界面你可以修改创建服务的目录和名称，然后按”create”开始创建。 </P>
<P>3.创建应用目录 创建应用目录有两种方式分别介绍如下： </P>
<P>3.1 最简单的应用目录创建 当weblogic服务创建成功后，再次打开“Configuration Wizard”，这次选择”extend and existing weblogic configuration(扩展weblogic配置)”,然后”next”,选择weblogic服务目录，然后”next”,在”Configuration Extensions”中勾选”DefaultWebApp”，然后以下取默认值一路”next”直到”import”就OK了。默认应用目录一般在D:\bea\user_projects\applications\mydomain\DefaultWebApp(注：d:为我PC机上安装weblogic的盘符，读者根据安装目录进行查找)。 </P>
<P>测试：启动weblogic服务,然后输入<A href="http://localhost:7001/">http://localhost:7001</A>就可以看到介绍性页面了。 </P>
<P>3.2使用控制台创建应用目录 当weblogic服务创建成功后,启动weblogic服务,然后在浏览器中输入<A href="http://localhost:7001/console">http://localhost:7001/console</A> 在登录界面输入你创建weblogic服务时的用户密码。 </P>
<P>a.创建一个新目录做为即将发布的应用目录，我使用d:\appweb做为例了目录,在该目录下再创建WEB-INF,在WEB-INF下面创建web.xml文件，目录结构为 </P>
<P>appweb </P>
<P>|__WEB-INF/web.xml </P>
<P>web.xml内容一般是: </P>
<P>&lt;?xml version="1.0" ?&gt; <BR>&lt;!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "<A href="http://java.sun.com/dtd/web-app_2_3.dtd">http://java.sun.com/dtd/web-app_2_3.dtd</A>"&gt; <BR>&lt;web-app&gt; <BR>&lt;welcome-file-list&gt; <BR>&lt;welcome-file&gt;index.html&lt;/welcome-file&gt; <BR>&lt;/web-app&gt; </P>
<P><BR>b.目录创建完成后，在控制台的首页，选择“Web Application Modules”-&gt;”Deploy a new Web Application Module... ”,在”Location”下选择你刚才创建的appweb(注意这个目录一定要有WEB-INF目录，并且在WEB-INF目录下一定要有web.xml文件，否则不能够创建应用目录)。选择后，点击”target module”，返回，可以看到当前weblogic服务中依成功创建的应用目录了。然后再创建一个index.html文档放在appweb目录下，就完成了应用目录的创建。 </P>
<P>测试：启动weblogic服务,然后输入<A href="http://localhost:7001/appweb/index.html">http://localhost:7001/appweb/index.html</A>就可以看到自己设置的页面了。 </P>
<P><BR><BR>2<BR><BR>二、开始部署J2EE web项目</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; Web项目的部署采用Configuration Wizard工具。依次点击“开始”-&gt;“BEA WebLogic Platform<BR>8.1”-&gt;“Configuration Wizard”，启动”BEA WebLogic Configuration Wizard”。</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; 2.1 新建weblogic domain<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 选择”新建weblogic配置”，</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 然后”下一步”,在”模板”选择”base weblogic server domain”,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 然后”下一步”,没有特别的需要就不修改默认选择，然后”下一步”，在这个界面上输入用户名、密码（如填写用户wangnewton，密码wangnewton）和创建这个服务的描述（请记住这个用户密码它是启动这个服务和进入服务控制台的帐号）</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 然后”下一步”，选择jdk的版本,然后”下一步”，在这个界面你可以修改创建服务的目录和配置名称，然后按”创建”开始创建。</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 点击完成结束。</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; 2.2 创建应用目录：创建应用目录有两种方式分别介绍如下</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.2.1方法一：继续采用Configuration Wizard 创建<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这次选择”extend and existing weblogic configuration(扩展weblogic配置)”,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 然后“下一步”,选择前面配置的weblogic配置目录mydomain，然后“下一步”,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在”Configuration Extensions”中勾选”DefaultWebApp”，</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 然后以下取默认值一路”下一步”直到”导入”就OK了。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 默认应用目录一般在D:\bea\user_projects\applications\mydomain\DefaultWebApp(注：d:为我PC机上安装weblogic的盘符，读者根据安装目录进行查找)。 </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 测试：启动weblogic服务,然后输入<A href="http://localhost:7001">http://localhost:7001</A>就可以看到介绍性页面了。</P>
<P><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.2.2方法二：使用控制台创建应用目录<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 使用这个方法最好先要有一个可以发布的web应用，比如大家做过的基于tomcat的项目。没有的话至少要有webapp\WEB-INF\web.xml文件，web.xml文件类似于：<BR>webapp<BR>|__WEB-INF/web.xml </P>
<P>web.xml内容一般是: <BR>&lt;?xml version="1.0" ?&gt;&nbsp; <BR>&lt;!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "<A href="http://java.sun.com/dtd/web-app_2_3.dtd">http://java.sun.com/dtd/web-app_2_3.dtd</A>"&gt; <BR>&lt;web-app&gt; <BR>&lt;welcome-file-list&gt; <BR>&lt;welcome-file&gt;index.html&lt;/welcome-file&gt; <BR>&lt;/web-app&gt;</P>
<P>&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 创建好如上的文件结构和web.xml后，启动weblogic服务,完成后在浏览器中输入<A href="http://127.0.0.1:7001/console">http://127.0.0.1:7001/console</A></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在登录界面输入你创建weblogic服务时的用户密码（wangnewton）。然后sign in，在下面的页面中选择“Web Application Modules”</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 点击后进入该界面，点击Deploy a new Web Application Module...</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在“Location”下选择你刚才创建的appweb(注意这个目录一定要有WEB-INF目录，并且在WEB-INF目录下一定要有web.xml文件，否则不能够创建应用目录)。选择后，点击”target module”，</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 返回，可以看到当前weblogic服务中依成功创建的应用目录了。然后再创建一个index.html文档放在appweb目录下，就完成了应用目录的创建。</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 测试：启动weblogic服务,然后输入<A href="http://127.0.0.1:7001/webapp/index.html">http://127.0.0.1:7001/webapp/index.html</A>就可以看到自己设置的页面了。</P>
<P><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PS：相对来说，方法二是实际项目中使用最多的一种配置方法。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 继续PS：如何设置项目为默认启动路径？<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对于刚才创建的应用目录，总要通过<A href="http://127.0.0.1:7001/webapp/index.html">http://127.0.0.1:7001/webapp/index.html</A>，能否直接设置成通过<A href="http://127.0.0.1:7001/index.html">http://127.0.0.1:7001/index.html</A>可以访问的方式呢？</P>
<P>方法是在webapp\WEB-INF下新建一个weblogic.xml文件，<BR>webapp<BR>|__WEB-INF/weblogic.xml </P>
<P>weblogic.xml内容一般是: <BR>&lt;!DOCTYPE weblogic-web-app PUBLIC "-//BEA Systems, Inc.//DTD Web Application 8.1//EN" "<A href="http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd">http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd</A>"&gt;<BR>&lt;weblogic-web-app&gt;<BR>&lt;context-root&gt;/&lt;/context-root&gt;<BR>&lt;/weblogic-web-app&gt;</P>
<P>&nbsp;</P>
<P>三、weblogic 连接池的配置<BR>&nbsp;&nbsp;&nbsp;&nbsp; 实例背景：SQL-SERVER 2000的数据库 + JDBC3.0<BR>&nbsp;&nbsp;&nbsp;&nbsp; 3.1 配置连接池<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 进入控制台(console) 可以看到该界面 ，然后点击 Connetion Pools</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 点击后进入该界面，点击configure a new JDBC Conneciton Pool </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 点击后显示该界面 因为是使用SQL-SERVER 2000 所以选择MS SQL Server<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 选择后，选择 MicroSoft’s MS SQL Server Driver (type) Version 。。。[倒数第三个^-^]然后点击Continue</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 按页面要求填入相关信息</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 此步骤完成后，进入该页面</P>
<P><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 点击 Test Driver Configuration ，如果是成功显示该页</P>
<P>&nbsp;&nbsp;&nbsp; 点击Create and deploy,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; 3.2 配置数据源<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 进入控制台，点击date source。。。</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 点击后显示该页 点击 configure a new JDBC Data Source</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 点击后进入该页</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 以下步骤都是默认选择后显示</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 由此，数据源就配置成功了，记住这里的JNDIName配置为MyJNDI，稍后将会在程序中被使用。</P>
<P>&nbsp;</P>
<P>四、weblogic 连接池测试程序<BR>&nbsp;&nbsp;&nbsp;&nbsp; 为了方便，采用一个简单jsp程序进行连接池的测试，连接池使用起来不但可以提高系统吞吐量，而且连接程序也是很简单的。一般大家手头上都会有专业的数据库连接组件，把这个组件修改成为weblogic连接池的组件也是很简单的，只需要执行简单几行代码替换就ok了。但作为新手，可以通过这个jsp测试刚才建立的连接池设置。</P>
<P>&lt;%@ page contentType="text/html;charset=GBK" %&gt; <BR>&lt;%@ page import= "java.sql.* " %&gt; <BR>&lt;%@ page import= "javax.sql.* " %&gt; <BR>&lt;%@ page import= "javax.naming.* "%&gt; <BR>&lt;HTML&gt;<BR>&lt;HEAD&gt;<BR>&lt;TITLE&gt; &lt;/TITLE&gt;<BR>&lt;META NAME="Generator" CONTENT="EditPlus2.11"&gt;<BR>&lt;/HEAD&gt;<BR>&lt;BODY&gt;<BR>&lt;%<BR>// 从weblogic 8 文档抄来<BR>Context ctx = null;<BR>Hashtable ht = new Hashtable();<BR>ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");<BR>ht.put(Context.PROVIDER_URL,"t3://127.0.0.1:7001");<BR>// ht.put(Context.SECURITY_PRINCIPAL, "admin");<BR>// ht.put(Context.SECURITY_CREDENTIALS, "11111111"); Connection conn = null;<BR>Statement stmt = null;<BR>ResultSet rs = null;<BR>try {<BR>ctx = new InitialContext(ht);<BR>DataSource ds = (DataSource)ctx.lookup ("MyJNDI"); <BR>conn = ds.getConnection();<BR>stmt = conn.createStatement();<BR>stmt.execute("select * from accounts");<BR>rs = stmt.getResultSet(); <BR>while ( rs.next()){<BR>out.println( rs.getString(1)+"___");<BR>out.println( rs.getString(2)+"___");<BR>out.println( rs.getString(3)+"&lt;br&gt;");<BR>} <BR>stmt.close();<BR>stmt=null;<BR>conn.close(); conn=null; <BR>}catch (Exception e) {<BR>out.println("错误 !! ERR !" );<BR>}<BR>finally { <BR>try {<BR>ctx.close();<BR>} catch (Exception e) {<BR>out.println("ctx ERR !" ); } <BR>try {<BR>if (rs != null) rs.close();<BR>} catch (Exception e) {<BR>out.println("rs ERR !" ); } <BR>try {<BR>if (stmt != null) stmt.close();<BR>} catch (Exception e) {<BR>out.println("stmt ERR !" ); } <BR>try {<BR>if (conn != null) conn.close();<BR>} catch (Exception e) {<BR>out.println("conn ERR !" ); } }<BR>%&gt; <BR>&lt;/BODY&gt;<BR>&lt;/HTML&gt; </P>
<P><BR>&nbsp;</P><img src ="http://www.blogjava.net/lmsun/aggbug/11069.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-08-25 15:45 <a href="http://www.blogjava.net/lmsun/articles/11069.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>log4j的配置与使用</title><link>http://www.blogjava.net/lmsun/articles/10908.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Wed, 24 Aug 2005 05:15:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/10908.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/10908.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/10908.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/10908.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/10908.html</trackback:ping><description><![CDATA[<P>1.把log4j-1.2.8.jar放到你的lib下,設置好classpath<BR>2.添加配置文件.<BR>配置文件取名log4j.properties，直接放到WEB-INF/classes下即可<BR>例如:<BR>log4j.rootLogger=DEBUG, ROOT</P>
<P>log4j.appender.ROOT=org.apache.log4j.RollingFileAppender<BR>log4j.appender.ROOT.File=MMQB_Debug_Info.log<BR>log4j.appender.ROOT.MaxFileSize=200KB<BR>log4j.appender.ROOT.MaxBackupIndex=5<BR>log4j.appender.ROOT.layout=org.apache.log4j.PatternLayout<BR>log4j.appender.ROOT.layout.ConversionPattern=[%d] %c %-5p - %m%n<BR>#log4j.logger.com.webage.ejbs=INFO</P>
<P>3.在程序中<BR>protected Log log = LogFactory.getLog(this.getClass().getName());<BR>log.debug("debug infomation");<BR><BR><BR>首先建立一个servlet<BR>/*<BR>&nbsp;* 创建日期 2005-4-4<BR>&nbsp;*<BR>&nbsp;* TODO 要更改此生成的文件的模板，请转至<BR>&nbsp;* 窗口 － 首选项 － Java － 代码样式 － 代码模板<BR>&nbsp;*/<BR>package cn.wst.common.log4j;</P>
<P><BR>&nbsp;</P>
<P>/*<BR>&nbsp;* 创建日期 2005-3-8<BR>&nbsp;*<BR>&nbsp;* 源文件: MyLog4jInit.java<BR>&nbsp;*/</P>
<P>&nbsp;<BR>import java.io.IOException;<BR>&nbsp;<BR>import javax.servlet.ServletConfig;<BR>import javax.servlet.ServletException;<BR>import javax.servlet.http.HttpServlet;<BR>import javax.servlet.http.HttpServletRequest;<BR>import javax.servlet.http.HttpServletResponse;<BR>&nbsp;<BR>import org.apache.log4j.xml.DOMConfigurator;<BR>import org.apache.log4j.PropertyConfigurator;</P>
<P>/**<BR>&nbsp;* 用于初始化Log4j<BR>&nbsp;*<BR>&nbsp;* @author wst<BR>&nbsp;* @version 1.0<BR>&nbsp;*/<BR>public class MyLog4jInit extends HttpServlet {<BR>&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp; public void init(ServletConfig config) throws ServletException {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super.init(config);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String prefix = getServletContext().getRealPath("/");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String file = getInitParameter("log4j"); // if the log4j-init-file is ot<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("--------&nbsp; Log4J Start [AddressBook] --------- ");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( file != null ) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DOMConfigurator.configure(prefix + file);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp; &nbsp;PropertyConfigurator.configure(prefix+file);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp; protected void doGet(HttpServletRequest request,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpServletResponse response) throws ServletException, IOException {<BR>&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp; <BR>&nbsp;}<BR>&nbsp;<BR>然后在web-inf里面的最开头配置servlet<BR>&nbsp;&lt;servlet&gt;<BR>&nbsp; &nbsp;&nbsp;&lt;servlet-name&gt;MyLog4jInit&lt;/servlet-name&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;servlet-class&gt;cn.wst.common.log4j.MyLog4jInit&lt;/servlet-class&gt;<BR>&nbsp;&nbsp; &lt;init-param&gt;<BR>&nbsp; &nbsp; &lt;param-name&gt;log4j&lt;/param-name&gt;<BR>&nbsp; &lt;param-value&gt;WEB-INF/log4j.xml&lt;/param-value&gt;<BR>&nbsp; &lt;/init-param&gt;<BR>&nbsp;&nbsp; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;&lt;!--log4j的初始化--&gt;<BR>&nbsp;&lt;/servlet&gt;<BR>相应在web-inf下的log4j.xml：<BR>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;<BR>&lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt;<BR>&lt;log4j:configuration xmlns:log4j="<A href="http://jakarta.apache.org/log4j/">http://jakarta.apache.org/log4j/</A>"&gt;<BR>&nbsp;&lt;appender name="cn.wst.file.log"<BR>&nbsp;&nbsp;class="org.apache.log4j.RollingFileAppender"&gt;<BR>&nbsp;&nbsp;&lt;param name="File" value="C:/addrLog.txt" /&gt;<BR>&nbsp;&nbsp;&lt;param name="Append" value="false" /&gt;<BR>&nbsp;&nbsp;&lt;layout class="org.apache.log4j.PatternLayout"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} [%C:%M()]-[%p] %m%n" /&gt;<BR>&nbsp;&nbsp;&lt;/layout&gt;<BR>&nbsp;&lt;/appender&gt;</P>
<P>&nbsp;&lt;appender name="cn.wst.console.log"<BR>&nbsp;&nbsp;class="org.apache.log4j.ConsoleAppender"&gt;<BR>&nbsp;&nbsp;&lt;layout class="org.apache.log4j.PatternLayout"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;param name="ConversionPattern" value="%5p&nbsp; (%F:%L) - %m%n" /&gt;<BR>&nbsp;&nbsp;&lt;/layout&gt;<BR>&nbsp;&lt;/appender&gt;<BR>&nbsp;&lt;!--&nbsp; use this to turn on debug to a rolling file. --&gt;<BR>&nbsp;&lt;root&gt;<BR>&nbsp;&nbsp;&lt;level value="info" /&gt;<BR>&nbsp;&nbsp;&lt;appender-ref ref="cn.wst.console.log" /&gt;<BR>&nbsp;&nbsp;&lt;appender-ref ref="cn.wst.file.log " /&gt;<BR>&nbsp;&lt;/root&gt;<BR>&nbsp;<BR>&lt;/log4j:configuration&gt;</P>
<P>在程序中使用使用：假设类名为Find_hotel_servlet，则<BR>先证明全局变量static Logger logger = Logger.getLogger(Find_hotel_servlet.class.getName());<BR>然后使用只要Find_hotel_servlet.logger.info(＊＊＊＊＊)就可以了<BR></P><img src ="http://www.blogjava.net/lmsun/aggbug/10908.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-08-24 13:15 <a href="http://www.blogjava.net/lmsun/articles/10908.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HttpClient and FileUpload--Jakarta Commons 下的工具</title><link>http://www.blogjava.net/lmsun/articles/10453.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Thu, 18 Aug 2005 09:11:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/10453.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/10453.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/10453.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/10453.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/10453.html</trackback:ping><description><![CDATA[<STRONG>August 2004</STRONG> <BR><BR>
<H3><B><A href="http://www.theserverside.com/news/thread.tss?thread_id=27789">Discuss this Article</A></B></H3><BR>
<P><FONT color=#993300 size=2>Adapted from:<BR><A href="http://www.apress.com/author/authorDisplay.html?aID=217" target=_blank>Pro Jakarta Commons</A>, by Harshad Oak<BR>Publisher: Apress<BR>ISBN: 1590592832</FONT></P>
<P>All communication over the Internet happens using a standard set of protocols, such as File Transfer Protocol (FTP), Simple Mail Transfer Protocol (SMTP), Post Office Protocol (POP), Hypertext Transfer Protocol (HTTP), and so on. HTTP is one of the most popular of these protocols and is integral to the World Wide Web. It is also a protocol that many of today’s applications have to be aware of and use wisely. If a Java application has to interact using HTTP, the Commons HttpClient component can make things a bit easier. Using this component, you do not have to worry about all the technicalities of the protocol but just concern yourself with the various classes and methods provided by the HttpClient component. In this article you will have a look at the capabilities of the HttpClient component and also some hands-on examples.</P>
<P>You will also have a quick look at the FileUpload component, which simplifies file-upload tasks on the server side. Finally, you will work through an example where you use HttpClient and FileUpload together.</P>
<BLOCKQUOTE>
<P>NOTE For all server-based examples in this article, I have used Tomcat version 4.0.6; however, you should not have any problems if you use another server that supports servlets and Java Server Page (JSP) technology.</P></BLOCKQUOTE>
<P>Table 9-1 shows the details for the components covered in this article.</P>
<P>Table 9-1. Component Details</P>
<TABLE cellSpacing=0 cellPadding=5 width=391 border=1>
<TBODY>
<TR>
<TD><STRONG>Name</STRONG></TD>
<TD><STRONG>Version</STRONG></TD>
<TD><STRONG>Package</STRONG></TD></TR>
<TR>
<TD>HttpClient</TD>
<TD>2.0-rc1</TD>
<TD>org.apache.commons.httpclient</TD></TR>
<TR>
<TD>FileUpload</TD>
<TD>1.0</TD>
<TD>org.apache.commons.fileupload</TD></TR></TBODY></TABLE><BR><BR>
<H2>Introducing HttpClient</H2>
<P>HttpClient is an attempt to provide a simple Application Programming Interface (API) that Java developers can use to create code that communicates over HTTP. If you are developing a Web browser or just an application that occasionally needs some data from the Web, HttpClient can help you develop the client code that will communicate over HTTP. As the name suggests, HttpClient is meant only for HTTP client code and cannot be used to, say, develop a server that processes HTTP requests.</P>
<P>I recommend you use HttpClient instead of using the <STRONG>java.net</STRONG> classes because HttpClient is easier to work with, it supports some HTTP features that <STRONG>java.net</STRONG> does not, and it has a vibrant community backing it. Visit <A href="http://www.nogoop.com/product_16.html#compare" target=_blank>http://www.nogoop.com/product_16.html#compare</A> to compare HttpClient, <STRONG>java.net</STRONG>, and a couple of other APIs.</P>
<P>With a number of Commons components, the Javadocs are the only real documentation that exists. However, with HttpClient some good documentation exists beyond the Javadocs. A short tutorial at <A href="http://jakarta.apache.org/commons/httpclient/tutorial.html" target=_blank>http://jakarta.apache.org/commons/httpclient/tutorial.html</A> can get you started with HttpClient.</P>
<P>These are some of the important features of HttpClient:</P>
<UL>
<LI>Implements HTTP versions 1.0 and 1.1 
<LI>Implements all HTTP methods, such as GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE 
<LI>Supports communication using HTTPS and connections through HTTP proxies 
<LI>Supports authentication using Basic, Digest, and NT LAN Manager (NTLM) methods 
<LI>Handles cookies </LI></UL>
<P>You will now see the various elements of the HttpClient component and how they fall into place to get you talking over HTTP.</P>
<H2>Using HttpClient</H2>
<P>HttpClient uses the Commons Logging component, so the only dependency for HttpClient to work properly is that the <STRONG>commons-logging</STRONG> component Java Archive (JAR) file be present. Using HttpClient to handle most requirements is fairly simple. You just need to understand a few key classes and interfaces. The following sections present a simple example of sending a GET request and then explain the classes that play a role in how the example works.</P>
<H3><EM>Using the GET Method</EM></H3>
<P>The GET method is the most common method used to send an HTTP request. Every time you click a hyperlink, you send an HTTP request using the GET method. You will now see an example of sending a GET request using HttpClient based Java code. The code in Listing 9-1 sends a GET request to the URL <A href="http://localhost:8080/validatorStrutsApp/userInfo.do" target=_blank>http://localhost:8080/validatorStrutsApp/userInfo.do</A> and has three request parameters, <STRONG>firstname</STRONG>, <STRONG>lastname</STRONG>, and <STRONG>email</STRONG>.</P><PRE>Listing 9-1. <STRONG>HttpClientTrial</STRONG>
package com.commonsbook.chap9;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;

public class SubmitHttpForm {

    private static String url =
         "http://localhost:8080/validatorStrutsApp/userInfo.do";

    public static void main(String[] args) {

        //Instantiate an HttpClient
        HttpClient client = new HttpClient();

        //Instantiate a GET HTTP method
        HttpMethod method = new GetMethod(url);

        //Define name-value pairs to set into the QueryString
        NameValuePair nvp1= new NameValuePair("firstName","fname");
        NameValuePair nvp2= new NameValuePair("lastName","lname");
        NameValuePair nvp3= new NameValuePair("email","email@email.com");

        method.setQueryString(new NameValuePair[]{nvp1,nvp2, nvp3});

        try{
            int statusCode = client.executeMethod(method);

            System.out.println("QueryString&gt;&gt;&gt; "+method.getQueryString());
            System.out.println("Status Text&gt;&gt;&gt;"
                  +HttpStatus.getStatusText(statusCode));

            //Get data as a String
            System.out.println(method.getResponseBodyAsString());

            //OR as a byte array
            byte [] res  = method.getResponseBody();

            //write to file
            FileOutputStream fos= new FileOutputStream("donepage.html");
            fos.write(res);

            //release connection
            method.releaseConnection();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
}
</PRE>
<P>The output on executing this piece of code will depend on the response you get to your GET request. </P>
<P>The following steps take place in the class <STRONG>SubmitHttpForm</STRONG> to invoke the URL specified, including passing the three parameters as part of the query string, displaying the response, and writing the response to a file:</P>
<OL>
<LI>You first need to instantiate the HttpClient, and because you have specified no parameters to the constructor, by default the org.apache.commons.httpclient.SimpleHttpConnectionManager class is used to create a new HttpClient. To use a different ConnectionManager, you can specify any class implementing the interface org.apache.commons.httpclient.HttpConnectionManager as a parameter to the constructor. You can use the MultiThreadedHttpConnectionManager connection manager if more than one thread is likely to use the HttpClient. The code would then be new HttpClient(new MultiThreadedHttpConnectionManager()). 
<LI>Next you create an instance of HttpMethod. Because HttpClient provides implementations for all HTTP methods, you could very well have chosen an instance of PostMethod instead of GetMethod. Because you are using an HttpMethod reference and not a reference of an implementation class such as GetMethod or PostMethod, you intend to use no special features provided by implementations such as GetMethod or PostMethod. 
<LI>You define name/value pairs and then set an array of those name/value pairs into the query string. 
<LI>Once the groundwork is complete, you execute the method using the HttpClient instance you created in step 1. The response code returned is based on the success or failure of the execution. 
<LI>You get the response body both as a string and as a byte array. The response is printed to the console and also written to a file named donepage.html. 
<P><STRONG>NOTE</STRONG> The class <STRONG>org.apache.commons.httpclient.HttpStatus</STRONG> defines static int variables that map to HTTP status codes.</P></LI></OL>
<P>In this example, you can see how easily you can fire a request and get a response over HTTP using the HttpClient component. You might have noted that writing such code has a lot of potential to enable testing of Web applications quickly and to even load test them. This has led to HttpClient being used in popular testing framework such as Jakarta Cactus, HTMLUnit, and so on. You can find in the documentation a list of popular applications that use HttpClient.</P>
<P>You used the GET method to send name/value pairs as part of a request. However, the GET method cannot always serve your purpose, and in some cases using the POST method is a better option.</P>
<H3><EM>Using the POST Method</EM></H3>
<P>Listing 9-2 shows an example where you enclose an Extensible Markup Language (XML) file within a request and send it using the POST method to a JSP named <STRONG>GetRequest.jsp</STRONG>. The JSP will just print the request headers it receives. These headers will show if the request got across properly.</P><PRE>Listing 9-2. Sending an XML File Using the POST Method
package com.commonsbook.chap9;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class PostAFile {
    private static String url =
         "http://localhost:8080/HttpServerSideApp/GetRequest.jsp";

    public static void main(String[] args) throws IOException {
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);

        client.setConnectionTimeout(8000);

        // Send any XML file as the body of the POST request
        File f = new File("students.xml");
        System.out.println("File Length = " + f.length());

        postMethod.setRequestBody(new FileInputStream(f));
        postMethod.setRequestHeader("Content-type",
            "text/xml; charset=ISO-8859-1");

        int statusCode1 = client.executeMethod(postMethod);

        System.out.println("statusLine&gt;&gt;&gt;" + postMethod.getStatusLine());
        postMethod.releaseConnection();
    }
}
</PRE>
<P>In Listing 9-2, I have stated the URL for <STRONG>GetRequest.jsp</STRONG> using a server I am running locally on port 8080. This URL will vary based on the server where the JSP is being maintained. In this example, you create an instance of the classes <STRONG>HttpClient</STRONG> and <STRONG>PostMethod</STRONG>. You set the connection timeout for the HTTP connection to 3,000 milliseconds and then set an XML file into the request body. I am using a file named students.xml however, the contents of the file are not relevant to the example, and you could very well use any other file. Because you are sending an XML file, you also set the Content-Type header to state the format and the character set. <STRONG>GetRequest.jsp</STRONG> contains only a scriptlet that prints the request headers. The contents of the JSP are as follows:</P><PRE>&lt;%
        java.util.Enumeration e= request.getHeaderNames();
        while (e.hasMoreElements()) {
          String headerName=(String)e.nextElement();
          System.out.println(headerName +" = "+request.getHeader(headerName));
        }
%&gt;
</PRE>
<P>Upon executing the class PostAFile, the JSP gets invoked, and the output displayed on the server console is as follows:</P><PRE>content-type = text/xml; charset=ISO-8859-1
user-agent = Jakarta Commons-HttpClient/2.0rc1
host = localhost:8080
content-length = 279
</PRE>
<P>The output shown on the console where the PostAFile class was executed is as follows:</P><PRE>File Length = 279
statusLine&gt;&gt;&gt;HTTP/1.1 200 OK
</PRE>
<P>Note that the output on the server shows the content length as <STRONG>279</STRONG> (bytes), the same as the length of the file <STRONG>students.xml</STRONG> that is shown on the application console. Because you are not invoking the JSP using any browser, the User-Agent header that normally states the browser specifics shows the HttpClient version being used instead.</P>
<BLOCKQUOTE>
<P><STRONG>NOTE</STRONG> In this example, you sent a single file over HTTP. To upload multiple files, the <STRONG>MultipartPostMethod</STRONG> class is a better alternative. You will look at it later in the “Introducing FileUpload” section.</P></BLOCKQUOTE>
<H3><EM>Managing Cookies</EM></H3>
<P>HttpClient provides cookie management features that can be particularly useful to test the way an application handles cookies. Listing 9-3 shows an example where you use HttpClient to add a cookie to a request and also to list details of cookies set by the JSP you invoke using the HttpClient code.</P>
<P>The <STRONG>HttpState</STRONG> class plays an important role while working with cookies. The <STRONG>HttpState</STRONG> class works as a container for HTTP attributes such as cookies that can persist from one request to another. When you normally surf the Web, the Web browser is what stores the HTTP attributes.</P><PRE>Listing 9-3. <STRONG>CookiesTrial.java</STRONG>
package com.commonsbook.chap9;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;

public class CookiesTrial {

    private static String url =
         "http://127.0.0.1:8080/HttpServerSideApp/CookieMgt.jsp";

    public static void main(String[] args) throws Exception {

        //A new cookie for the domain 127.0.0.1
        //Cookie Name= ABCD   Value=00000   Path=/  MaxAge=-1   Secure=False
        Cookie mycookie = new Cookie("127.0.0.1", "ABCD", "00000", "/", -1, false);

        //Create a new HttpState container
        HttpState initialState = new HttpState();
        initialState.addCookie(mycookie);

        //Set to COMPATIBILITY for it to work in as many cases as possible
        initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY);
        //create new client
        HttpClient httpclient = new HttpClient();
        //set the HttpState for the client
        httpclient.setState(initialState);

        GetMethod getMethod = new GetMethod(url);
        //Execute a GET method
        int result = httpclient.executeMethod(getMethod);

        System.out.println("statusLine&gt;&gt;&gt;"+getMethod.getStatusLine());

        //Get cookies stored in the HttpState for this instance of HttpClient
        Cookie[] cookies = httpclient.getState().getCookies();

        for (int i = 0; i &lt; cookies.length; i++) {
            System.out.println("\nCookieName="+cookies[i].getName());
            System.out.println("Value="+cookies[i].getValue());
            System.out.println("Domain="+cookies[i].getDomain());
        }

        getMethod.releaseConnection();
    }
}
</PRE>
<P>In Listing 9-3, you use the <STRONG>HttpState</STRONG> instance to store a new cookie and then associate this instance with the <STRONG>HttpClient</STRONG> instance. You then invoke <STRONG>CookieMgt.jsp</STRONG>. This JSP is meant to print the cookies it finds in the request and then add a cookie of its own. The JSP code is as follows:</P><PRE>&lt;%
        Cookie[] cookies= request.getCookies();

        for (int i = 0; i &lt; cookies.length; i++) {
          System.out.println(cookies[i].getName() +" = "+cookies[i].getValue());
        }

        //Add a new cookie
        response.addCookie(new Cookie("XYZ","12345"));
%&gt;
</PRE>
<BLOCKQUOTE>
<P><STRONG>CAUTION</STRONG> HttpClient code uses the class <STRONG>org.apache.commons.httpclient.Cookie</STRONG>, and JSP and servlet code uses the class <STRONG>javax.servlet.http.Cookie</STRONG>.</P></BLOCKQUOTE>
<P>The output on the application console upon executing the CookiesTrial class and invoking CookieMgt.jsp is as follows:</P><PRE>statusLine&gt;&gt;&gt;HTTP/1.1 200 OK

CookieName=ABCD
Value=00000
Domain=127.0.0.1

CookieName=XYZ
Value=12345
Domain=127.0.0.1

CookieName=JSESSIONID
Value=C46581331881A84483F0004390F94508
Domain=127.0.0.1
</PRE>
<P>In this output, note that although the cookie named ABCD has been created from CookiesTrial, the other cookie named XYZ is the one inserted by the JSP code. The cookie named JSESSIONID is meant for session tracking and gets created upon invoking the JSP. The output as displayed on the console of the server when the JSP is executed is as follows:</P>
<P>ABCD = 00000</P>
<P>This shows that when <STRONG>CookieMgt.jsp</STRONG> receives the request from the <STRONG>CookiesTrial</STRONG> class, the cookie named <STRONG>ABCD</STRONG> was the only cookie that existed. The sidebar “HTTPS and Proxy Servers” shows how you should handle requests over HTTPS and configure your client to go through a proxy.</P>
<TABLE height=494 cellSpacing=0 cellPadding=5 width=495 align=left border=0>
<TBODY>
<TR>
<TD>
<TABLE borderColor=#666666 cellSpacing=0 cellPadding=10 width=455 align=center border=1>
<TBODY>
<TR>
<TD width=441>
<P align=center><STRONG>HTTPS and Proxy Servers</STRONG></P>
<P>Using HttpClient to try out URLs that involve HTTPS is the same as with ordinary URLs. Just state <STRONG>https://…</STRONG> as your URL, and it should work fine. You only need to have Java Secure Socket Extension (JSSE) running properly on your machine. JSSE ships as a part of Java Software Development Kit (JSDK) 1.4 and higher and does not require any separate download and installation.</P>
<P>If you have to go through a proxy server, introduce the following piece of code. Replace <STRONG>PROXYHOST</STRONG> with the host name and replace <STRONG>9999</STRONG> with the port number for your proxy server:</P><PRE>	  HttpClient client = new HttpClient();
HostConfiguration hConf= client.getHostConfiguration();
hConf.setProxy("PROXYHOST ", 9999);
</PRE>If you also need to specify a username password for the proxy, you can do this using the <STRONG>setProxyCredentials</STRONG> method of the class <STRONG>HttpState</STRONG>. This method takes a <STRONG>Credentials</STRONG> object as a parameter. <STRONG>Credentials</STRONG> is a marker interface that has no methods and has a single implementation <STRONG>UsernamePasswordCredentials</STRONG>. You can use this class to create a <STRONG>Credentials</STRONG> object that holds the username and password required for Basic authentication.</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<P>You will now see the HttpClient component’s capability to use MultipartPostMethod to upload multiple files. You will look at this in tandem with the Commons FileUpload component. This Commons component is specifically meant to handle the server-side tasks associated with file uploads.</P>
<H2>Introducing FileUpload</H2>
<P>The FileUpload component has the capability of simplifying the handling of files uploaded to a server. Note that the FileUpload component is meant for use on the server side; in other words, it handles where the files are being uploaded to—not the client side where the files are uploaded from. Uploading files from an HTML form is pretty simple; however, handling these files when they get to the server is not that simple. If you want to apply any rules and store these files based on those rules, things get more difficult.</P>
<P>The FileUpload component remedies this situation, and in very few lines of code you can easily manage the files uploaded and store them in appropriate locations. You will now see an example where you upload some files first using a standard HTML form and then using HttpClient code.</P>
<H3><EM>Using HTML File Upload</EM></H3>
<P>The commonly used methodology to upload files is to have an HTML form where you define the files you want to upload. A common example of this HTML interface is the Web page you encounter when you want to attach files to an email while using any of the popular Web mail services.</P>
<P>In this example, you will create a simple HTML page where you provide for three files to be uploaded. Listing 9-4 shows the HTML for this page. Note that the <STRONG>enctype</STRONG> attribute for the form has the value <STRONG>multipart/form-data</STRONG>, and the <STRONG>input</STRONG> tag used is of type <STRONG>file</STRONG>. Based on the value of the <STRONG>action</STRONG> attribute, on form submission, the data is sent to <STRONG>ProcessFileUpload.jsp</STRONG>.</P><PRE>Listing 9-4. <STRONG>UploadFiles.html</STRONG>
&lt;HTML&gt;
  &lt;HEAD&gt;
    &lt;META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252"/&gt;
    &lt;TITLE&gt;File Upload Page&lt;/TITLE&gt;
  &lt;/HEAD&gt;
  &lt;BODY&gt;Upload Files
    &lt;FORM name="filesForm" action="ProcessFileUpload.jsp"
    method="post" enctype="multipart/form-data"&gt;
        File 1:&lt;input type="file" name="file1"/&gt;&lt;br/&gt;
        File 2:&lt;input type="file" name="file2"/&gt;&lt;br/&gt;
        File 3:&lt;input type="file" name="file3"/&gt;&lt;br/&gt;
        &lt;input type="submit" name="Submit" value="Upload Files"/&gt;
    &lt;/FORM&gt;
  &lt;/BODY&gt;
&lt;/HTML&gt;
</PRE>
<P>You can use a servlet to handle the file upload. I have used JSP to minimize the code you need to write. The task that the JSP has to accomplish is to pick up the files that are sent as part of the request and store these files on the server. In the JSP, instead of displaying the result of the upload in the Web browser, I have chosen to print messages on the server console so that you can use this same JSP when it is not invoked through an HTML form but by using HttpClient-based code.</P>
<P>Listing 9-5 shows the JSP code. Note the code that checks whether the item is a form field. This check is required because the Submit button contents are also sent as part of the request, and you want to distinguish between this data and the files that are part of the request. You have set the maximum file size to 1,000,000 bytes using the <STRONG>setSizeMax</STRONG> method.</P><PRE>Listing 9-5. <STRONG>ProcessFileUpload.jsp</STRONG>
&lt;%@ page contentType="text/html;charset=windows-1252"%&gt;
&lt;%@ page import="org.apache.commons.fileupload.DiskFileUpload"%&gt;
&lt;%@ page import="org.apache.commons.fileupload.FileItem"%&gt;
&lt;%@ page import="java.util.List"%&gt;
&lt;%@ page import="java.util.Iterator"%&gt;
&lt;%@ page import="java.io.File"%&gt;
html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1252"&gt;
&lt;title&gt;Process File Upload&lt;/title&gt;
&lt;/head&gt;
&lt;%
        System.out.println("Content Type ="+request.getContentType());

        DiskFileUpload fu = new DiskFileUpload();
        // If file size exceeds, a FileUploadException will be thrown
        fu.setSizeMax(1000000);

        List fileItems = fu.parseRequest(request);
        Iterator itr = fileItems.iterator();

        while(itr.hasNext()) {
          FileItem fi = (FileItem)itr.next();

          //Check if not form field so as to only handle the file inputs
          //else condition handles the submit button input
          if(!fi.isFormField()) {
            System.out.println("\nNAME: "+fi.getName());
            System.out.println("SIZE: "+fi.getSize());
            //System.out.println(fi.getOutputStream().toString());
            File fNew= new File(application.getRealPath("/"), fi.getName());

            System.out.println(fNew.getAbsolutePath());
            fi.write(fNew);
          }
          else {
            System.out.println("Field ="+fi.getFieldName());
          }
        }
%&gt;
&lt;body&gt;
Upload Successful!!
&lt;/body&gt;
&lt;/html&gt;
</PRE>
<BLOCKQUOTE>
<P><STRONG>CAUTION</STRONG> With FileUpload 1.0 I found that when the form was submitted using Opera version 7.11, the <STRONG>getName</STRONG> method of the class <STRONG>FileItem</STRONG> returns just the name of the file. However, if the form is submitted using Internet Explorer 5.5, the filename along with its entire path is returned by the same method. This can cause some problems.</P></BLOCKQUOTE>
<P>To run this example, you can use any three files, as the contents of the files are not important. Upon submitting the form using Opera and uploading three random XML files, the output I got on the Tomcat server console was as follows:</P><PRE>Content Type =multipart/form-data; boundary=----------rz7ZNYDVpN1To8L73sZ6OE

NAME: academy.xml
SIZE: 951
D:\javaGizmos\jakarta-tomcat-4.0.1\webapps\HttpServerSideApp\academy.xml

NAME: academyRules.xml
SIZE: 1211
D:\javaGizmos\jakarta-tomcat-4.0.1\webapps\HttpServerSideApp\academyRules.xml

NAME: students.xml
SIZE: 279
D:\javaGizmos\jakarta-tomcat-4.0.1\webapps\HttpServerSideApp\students.xml
Field =Submit
However, when submitting this same form using Internet Explorer 5.5, the output on the server console was as follows:
Content Type =multipart/form-data; boundary=---------------------------7d3bb1de0
2e4

NAME: D:\temp\academy.xml
SIZE: 951
D:\javaGizmos\jakarta-tomcat-4.0.1\webapps\HttpServerSideApp\D:\temp\academy.xml
</PRE>
<P>The browser displayed the following message: “The requested resource (D:\javaGizmos\jakarta-tomcat-4.0.1\webapps\HttpServerSideApp\D:\temp\academy.xml (The filename, directory name, or volume label syntax is incorrect)) is not available.”</P>
<P>This contrasting behavior on different browsers can cause problems. One workaround that I found in an article at <A href="http://www.onjava.com/pub/a/onjava/2003/06/25/commons.html" target=_blank>http://www.onjava.com/pub/a/onjava/2003/06/25/commons.html</A> is to first create a file reference with whatever is supplied by the <STRONG>getName</STRONG> method and then create a new file reference using the name returned by the earlier file reference. Therefore, you can insert the following code to have your code work with both browsers (I wonder who the guilty party is…blaming Microsoft is always the easy way out) </P><PRE>File tempFileRef  = new File(fi.getName());
File fNew = new File(application.getRealPath("/"),tempFileRef.getName());
</PRE>
<P>In this section, you uploaded files using a standard HTML form mechanism. However, often a need arises to be able to upload files from within your Java code, without any browser or form coming into the picture. In the next section, you will look at HttpClient-based file upload.</P>
<H3><EM>Using HttpClient-Based FileUpload</EM></H3>
<P>Earlier in the article you saw some of the capabilities of the HttpClient component. One capability I did not cover was its ability to send multipart requests. In this section, you will use this capability to upload a few files to the same JSP that you used for uploads using HTML.</P>
<P>The class <STRONG>org.apache.commons.httpclient.methods.MultipartPostMethod</STRONG> provides the multipart method capability to send multipart-encoded forms, and the package <STRONG>org.apache.commons.httpclient.methods.multipart</STRONG> has the support classes required. Sending a multipart form using HttpClient is quite simple. In the code in Listing 9-6, you send three files to <STRONG>ProcessFileUpload.jsp</STRONG>.</P><PRE>Listing 9-6. <STRONG>HttpMultiPartFileUpload.java</STRONG>
package com.commonsbook.chap9;
import java.io.File;
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.MultipartPostMethod;

public class HttpMultiPartFileUpload {
    private static String url =
      "http://localhost:8080/HttpServerSideApp/ProcessFileUpload.jsp";

    public static void main(String[] args) throws IOException {
        HttpClient client = new HttpClient();
        MultipartPostMethod mPost = new MultipartPostMethod(url);
        client.setConnectionTimeout(8000);

        // Send any XML file as the body of the POST request
        File f1 = new File("students.xml");
        File f2 = new File("academy.xml");
        File f3 = new File("academyRules.xml");

        System.out.println("File1 Length = " + f1.length());
        System.out.println("File2 Length = " + f2.length());
        System.out.println("File3 Length = " + f3.length());

        mPost.addParameter(f1.getName(), f1);
        mPost.addParameter(f2.getName(), f2);
        mPost.addParameter(f3.getName(), f3);

        int statusCode1 = client.executeMethod(mPost);

        System.out.println("statusLine&gt;&gt;&gt;" + mPost.getStatusLine());
        mPost.releaseConnection();
    }
}
</PRE>
<P>In this code, you just add the files as parameters and execute the method. The <STRONG>ProcessFileUpload.jsp</STRONG> file gets invoked, and the output is as follows:</P><PRE>Content Type =multipart/form-data; boundary=----------------31415926535897932384
6

NAME: students.xml
SIZE: 279
D:\javaGizmos\jakarta-tomcat-4.0.1\webapps\HttpServerSideApp\students.xml

NAME: academy.xml
SIZE: 951
D:\javaGizmos\jakarta-tomcat-4.0.1\webapps\HttpServerSideApp\academy.xml

NAME: academyRules.xml
SIZE: 1211
D:\javaGizmos\jakarta-tomcat-4.0.1\webapps\HttpServerSideApp\academyRules.xml
</PRE>
<P>Thus, file uploads on the server side become quite a simple task if you are using the Commons FileUpload component.</P>
<H2>Summary</H2>
<P>In this article, you saw the HttpClient and FileUpload components. Although HttpClient can be useful in any kind of applications that use HTTP for communication, the FileUpload component has a much more specific scope. One important plus for HttpClient is the existence of a decent user guide and tutorial. The FileUpload component can be just what you are looking for if you are wondering what to use and how to manage files uploaded through your application.<BR></P><img src ="http://www.blogjava.net/lmsun/aggbug/10453.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-08-18 17:11 <a href="http://www.blogjava.net/lmsun/articles/10453.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MD5 算法的Java Bean 源代码</title><link>http://www.blogjava.net/lmsun/articles/10438.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Thu, 18 Aug 2005 07:46:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/10438.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/10438.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/10438.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/10438.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/10438.html</trackback:ping><description><![CDATA[<P><BR>java<BR>/************************************************<BR>MD5 算法的Java Bean<BR>@author:Topcat Tuppin<BR>Last Modified:10,Mar,2001<BR>*************************************************/<BR>package beartool;<BR>import java.lang.reflect.*;<BR>/*************************************************<BR>md5 类实现了RSA Data Security, Inc.在提交给IETF<BR>的RFC1321中的MD5 message-digest 算法。<BR>*************************************************/</P>
<P>public class MD5 {<BR>&nbsp;/* 下面这些S11-S44实际上是一个4*4的矩阵，在原始的C实现中是用#define 实现的，<BR>&nbsp;这里把它们实现成为static final是表示了只读，切能在同一个进程空间内的多个<BR>&nbsp;Instance间共享*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S11 = 7;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S12 = 12;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S13 = 17;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S14 = 22;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S21 = 5;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S22 = 9;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S23 = 14;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S24 = 20;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S31 = 4;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S32 = 11;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S33 = 16;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S34 = 23;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S41 = 6;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S42 = 10;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S43 = 15;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final int S44 = 21;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* 下面的三个成员是MD5计算过程中用到的3个核心数据，在原始的C实现中<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 被定义到MD5_CTX结构中<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private long[] state = new long[4];&nbsp; // state (ABCD)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private long[] count = new long[2];&nbsp; // number of bits, modulo 2^64 (lsb first)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private byte[] buffer = new byte[64]; // input buffer<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;/* digestHexStr是MD5的唯一一个公共成员，是最新一次计算结果的<BR>&nbsp;　 16进制ASCII表示.<BR>&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public String digestHexStr;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* digest,是最新一次计算结果的2进制内部表示，表示128bit的MD5值.<BR>&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private byte[] digest = new byte[16];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;/*<BR>&nbsp;&nbsp; getMD5ofStr是类MD5最主要的公共方法，入口参数是你想要进行MD5变换的字符串<BR>&nbsp;&nbsp; 返回的是变换完的结果，这个结果是从公共成员digestHexStr取得的．<BR>&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public String getMD5ofStr(String inbuf) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Init();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Update(inbuf.getBytes(), inbuf.length());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Final();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digestHexStr = "";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 16; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digestHexStr += byteHEX(digest[i]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return digestHexStr;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 这是MD5这个类的标准构造函数，JavaBean要求有一个public的并且没有参数的构造函数<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public MD5() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Init();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* md5Init是一个初始化函数，初始化核心变量，装入标准的幻数 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void md5Init() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count[0] = 0L;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count[1] = 0L;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ///* Load magic initialization constants.</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state[0] = 0x67452301L;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state[1] = 0xefcdab89L;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state[2] = 0x98badcfeL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state[3] = 0x10325476L;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* F, G, H ,I 是4个基本的MD5函数，在原始的MD5的C实现中，由于它们是<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 简单的位运算，可能出于效率的考虑把它们实现成了宏，在java中，我们把它们<BR>&nbsp;&nbsp;&nbsp;&nbsp; 　　实现成了private方法，名字保持了原来C中的。 */</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private long F(long x, long y, long z) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (x &amp; y) | ((~x) &amp; z);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private long G(long x, long y, long z) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (x &amp; z) | (y &amp; (~z));</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private long H(long x, long y, long z) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return x ^ y ^ z;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private long I(long x, long y, long z) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return y ^ (x | (~z));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FF,GG,HH和II将调用F,G,H,I进行近一步变换<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Rotation is separate from addition to prevent recomputation.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private long FF(long a, long b, long c, long d, long x, long s,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long ac) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a += F (b, c, d) + x + ac;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = ((int) a &lt;&lt; s) | ((int) a &gt;&gt;&gt; (32 - s));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a += b;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private long GG(long a, long b, long c, long d, long x, long s,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long ac) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a += G (b, c, d) + x + ac;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = ((int) a &lt;&lt; s) | ((int) a &gt;&gt;&gt; (32 - s));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a += b;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private long HH(long a, long b, long c, long d, long x, long s,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long ac) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a += H (b, c, d) + x + ac;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = ((int) a &lt;&lt; s) | ((int) a &gt;&gt;&gt; (32 - s));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a += b;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private long II(long a, long b, long c, long d, long x, long s,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long ac) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a += I (b, c, d) + x + ac;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = ((int) a &lt;&lt; s) | ((int) a &gt;&gt;&gt; (32 - s));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a += b;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Update是MD5的主计算过程，inbuf是要变换的字节串，inputlen是长度，这个<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 函数由getMD5ofStr调用，调用之前需要调用md5init，因此把它设计成private的<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void md5Update(byte[] inbuf, int inputLen) {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i, index, partLen;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] block = new byte[64];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; index = (int)(count[0] &gt;&gt;&gt; 3) &amp; 0x3F;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // /* Update number of bits */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((count[0] += (inputLen &lt;&lt; 3)) &lt; (inputLen &lt;&lt; 3))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count[1]++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count[1] += (inputLen &gt;&gt;&gt; 29);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; partLen = 64 - index;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Transform as many times as possible.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (inputLen &gt;= partLen) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Memcpy(buffer, inbuf, index, 0, partLen);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Transform(buffer);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = partLen; i + 63 &lt; inputLen; i += 64) {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Memcpy(block, inbuf, 0, i, 64);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Transform (block);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; index = 0;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = 0;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ///* Buffer remaining input */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Memcpy(buffer, inbuf, index, i, inputLen - i);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Final整理和填写输出结果<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void md5Final () {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] bits = new byte[8];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int index, padLen;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ///* Save number of bits */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Encode (bits, count, 8);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ///* Pad out to 56 mod 64.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; index = (int)(count[0] &gt;&gt;&gt; 3) &amp; 0x3f;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; padLen = (index &lt; 56) ? (56 - index) : (120 - index);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Update (PADDING, padLen);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ///* Append length (before padding) */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Update(bits, 8);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ///* Store state in digest */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Encode (digest, state, 16);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* md5Memcpy是一个内部使用的byte数组的块拷贝函数，从input的inpos开始把len长度的<BR>　　　　　 字节拷贝到output的outpos位置开始 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void md5Memcpy (byte[] output, byte[] input,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int outpos, int inpos, int len)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0; i &lt; len; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output[outpos + i] = input[inpos + i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; md5Transform是MD5核心变换程序，有md5Update调用，block是分块的原始字节<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void md5Transform (byte block[]) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long a = state[0], b = state[1], c = state[2], d = state[3];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long[] x = new long[16];</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Decode (x, block, 64);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Round 1 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = FF (a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = FF (d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = FF (c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = FF (b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = FF (a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = FF (d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = FF (c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = FF (b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = FF (a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = FF (d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = FF (c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = FF (b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = FF (a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = FF (d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = FF (c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = FF (b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Round 2 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = GG (a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = GG (d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = GG (c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = GG (b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = GG (a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = GG (d, a, b, c, x[10], S22, 0x2441453L); /* 22 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = GG (c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = GG (b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = GG (a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = GG (d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = GG (c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = GG (b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = GG (a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = GG (d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = GG (c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = GG (b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Round 3 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = HH (a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = HH (d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = HH (c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = HH (b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = HH (a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = HH (d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = HH (c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = HH (b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = HH (a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = HH (d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = HH (c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = HH (b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = HH (a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = HH (d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = HH (c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = HH (b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Round 4 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = II (a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = II (d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = II (c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = II (b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = II (a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = II (d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = II (c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = II (b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = II (a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = II (d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = II (c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = II (b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = II (a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; d = II (d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c = II (c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = II (b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state[0] += a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state[1] += b;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state[2] += c;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state[3] += d;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*Encode把long数组按顺序拆成byte数组，因为java的long类型是64bit的，<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 只拆低32bit，以适应原始C实现的用途<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void Encode (byte[] output, long[] input, int len) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i, j;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0, j = 0; j &lt; len; i++, j += 4) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output[j] = (byte)(input[i] &amp; 0xffL);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output[j + 1] = (byte)((input[i] &gt;&gt;&gt; 8) &amp; 0xffL);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output[j + 2] = (byte)((input[i] &gt;&gt;&gt; 16) &amp; 0xffL);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output[j + 3] = (byte)((input[i] &gt;&gt;&gt; 24) &amp; 0xffL);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*Decode把byte数组按顺序合成成long数组，因为java的long类型是64bit的，<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 只合成低32bit，高32bit清零，以适应原始C实现的用途<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void Decode (long[] output, byte[] input, int len) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i, j;</P>
<P><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0, j = 0; j &lt; len; i++, j += 4)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output[i] = b2iu(input[j]) |<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (b2iu(input[j + 1]) &lt;&lt; 8) |<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (b2iu(input[j + 2]) &lt;&lt; 16) |<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (b2iu(input[j + 3]) &lt;&lt; 24);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b2iu是我写的一个把byte按照不考虑正负号的原则的＂升位＂程序，因为java没有unsigned运算<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static long b2iu(byte b) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return b &lt; 0 ? b &amp; 0x7F + 128 : b;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;/*byteHEX()，用来把一个byte类型的数转换成十六进制的ASCII表示，<BR>&nbsp;　因为java中的byte的toString无法实现这一点，我们又没有C语言中的<BR>&nbsp;&nbsp; sprintf(outbuf,"%02X",ib)<BR>&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static String byteHEX(byte ib) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char[] Digit = { '0','1','2','3','4','5','6','7','8','9',<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'A','B','C','D','E','F' };<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char [] ob = new char[2];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ob[0] = Digit[(ib &gt;&gt;&gt; 4) &amp; 0X0F];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ob[1] = Digit[ib &amp; 0X0F];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String s = new String(ob);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String args[]) {</P>
<P><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MD5 m = new MD5();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (Array.getLength(args) == 0) {&nbsp;&nbsp; //如果没有参数，执行标准的Test Suite<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println("MD5 Test suite:");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println("MD5(\"\"):"+m.getMD5ofStr(""));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println("MD5(\"a\"):"+m.getMD5ofStr("a"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println("MD5(\"abc\"):"+m.getMD5ofStr("abc"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println("MD5(\"message digest\"):"+m.getMD5ofStr("message digest"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println("MD5(\"abcdefghijklmnopqrstuvwxyz\"):"+<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m.getMD5ofStr("abcdefghijklmnopqrstuvwxyz"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println("MD5(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\"):"+<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;m.getMD5ofStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;System.out.println("MD5(" + args[0] + ")=" + m.getMD5ofStr(args[0]));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>}</P>
<P><BR>login.jsp<BR>&lt;%@ page language='java' %&gt;<BR>&lt;jsp:useBean id='oMD5' scope='request' class='beartool.MD5'/&gt;</P>
<P>&lt;%@ page import='java.util.*'%&gt;<BR>&lt;%@ page import='java.sql.*'%&gt;<BR>&lt;html&gt;<BR>&lt;body&gt;<BR>&lt;%<BR>&nbsp; String userid = request.getParameter("UserID");&nbsp; //获取用户输入UserID<BR>&nbsp; String password = request.getParameter("Password"); //获取用户输入的Password<BR>&nbsp; <BR>&nbsp; String pwdmd5 = oMD5.getMD5ofStr(password);&nbsp; //计算MD5的值<BR>&nbsp; <BR>&nbsp; PrintWriter rp = response.getWriter();<BR>&nbsp; <BR>&nbsp; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<BR>&nbsp; <BR>&nbsp; Connection con = DriverManager.getConnection("jdbc:odbc:community", "", "");<BR>&nbsp;<BR>&nbsp; Statement stmt = con.createStatement();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;<BR>&nbsp; ResultSet rs = stmt.executeQuery("select * from users where userID ='"+userid+"' and pwdmd5= '" + pwdmd5+"'" );</P>
<P>&nbsp; if (rs.next()) <BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rp.print("Login OK");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; else<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rp.print("Login Fail");<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;<BR>&nbsp; stmt.close();<BR>&nbsp; con.close();<BR>&nbsp;<BR>%&gt;</P>
<P>&lt;/body&gt;</P>
<P>&lt;/html&gt;</P><img src ="http://www.blogjava.net/lmsun/aggbug/10438.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-08-18 15:46 <a href="http://www.blogjava.net/lmsun/articles/10438.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MD5的Java Bean实现</title><link>http://www.blogjava.net/lmsun/articles/10437.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Thu, 18 Aug 2005 07:39:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/10437.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/10437.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/10437.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/10437.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/10437.html</trackback:ping><description><![CDATA[2001 年 3 月 
<BLOCKQUOTE><ABSTRACT-EXTENDED>
<P><I xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">编者的话：</I> </P>
<P><I xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">虽然 MD5 签名算法在 jdk 中早已实现（如 MessageDigest类），但作者从 MD5 的原理分析讲述 MD5 具体算法的 Java实现并给出一个完整的示例程序，我想这对我们的读者来说还是会有很多帮助的。</I> </P></ABSTRACT-EXTENDED></BLOCKQUOTE>
<P><A name=1><SPAN class=atitle2>MD5简介</SPAN></A><BR>MD5的全称是Message-Digest Algorithm 5，在90年代初由MIT的计算机科学实验室和RSA Data Security Inc发明，经MD2、MD3和MD4发展而来。</P>
<P>Message-Digest泛指字节串(Message)的Hash变换，就是把一个任意长度的字节串变换成一定长的大整数。请注意我使用了“字节串”而不是“字符串”这个词，是因为这种变换只与字节的值有关，与字符集或编码方式无关。</P>
<P>MD5将任意长度的“字节串”变换成一个128bit的大整数，并且它是一个不可逆的字符串变换算法，换句话说就是，即使你看到源程序和算法描述，也无法将一个MD5的值变换回原始的字符串，从数学原理上说，是因为原始的字符串有无穷多个，这有点象不存在反函数的数学函数。</P>
<P>MD5的典型应用是对一段Message(字节串)产生fingerprint(指纹)，以防止被“篡改”。举个例子，你将一段话写在一个叫readme.txt文件中，并对这个readme.txt产生一个MD5的值并记录在案，然后你可以传播这个文件给别人，别人如果修改了文件中的任何内容，你对这个文件重新计算MD5时就会发现。如果再有一个第三方的认证机构，用MD5还可以防止文件作者的“抵赖”，这就是所谓的数字签名应用。</P>
<P>MD5还广泛用于加密和解密技术上，在很多操作系统中，用户的密码是以MD5值（或类似的其它算法）的方式保存的，用户Login的时候，系统是把用户输入的密码计算成MD5值，然后再去和系统中保存的MD5值进行比较，而系统并不“知道”用户的密码是什么。</P>
<P>一些黑客破获这种密码的方法是一种被称为“跑字典”的方法。有两种方法得到字典，一种是日常搜集的用做密码的字符串表，另一种是用排列组合方法生成的，先用MD5程序计算出这些字典项的MD5值，然后再用目标的MD5值在这个字典中检索。</P>
<P>即使假设密码的最大长度为8，同时密码只能是字母和数字，共26+26+10=62个字符，排列组合出的字典的项数则是P(62,1)+P(62,2)….+P(62,8)，那也已经是一个很天文的数字了，存储这个字典就需要TB级的磁盘组，而且这种方法还有一个前提，就是能获得目标账户的密码MD5值的情况下才可以。</P>
<P>在很多电子商务和社区应用中，管理用户的Account是一种最常用的基本功能，尽管很多Application Server提供了这些基本组件，但很多应用开发者为了管理的更大的灵活性还是喜欢采用关系数据库来管理用户，懒惰的做法是用户的密码往往使用明文或简单的变换后直接保存在数据库中，因此这些用户的密码对软件开发者或系统管理员来说可以说毫无保密可言，本文的目的是介绍MD5的Java Bean的实现，同时给出用MD5来处理用户的Account密码的例子，这种方法使得管理员和程序设计者都无法看到用户的密码，尽管他们可以初始化它们。但重要的一点是对于用户密码设置习惯的保护。</P>
<P>有兴趣的读者可以从这里取得MD5也就是RFC 1321的文本。http://www.ietf.org/rfc/rfc1321.txt </P>
<P><A name=2><SPAN class=atitle2>实现策略</SPAN></A><BR>MD5的算法在RFC1321中实际上已经提供了C的实现，我们其实马上就能想到，至少有两种用Java实现它的方法，第一种是，用Java语言重新写整个算法，或者再说简单点就是把C程序改写成Java程序。第二种是，用JNI(Java Native Interface)来实现，核心算法仍然用这个C程序，用Java类给它包个壳。</P>
<P>但我个人认为，JNI应该是Java为了解决某类问题时的没有办法的办法（比如与操作系统或I/O设备密切相关的应用），同时为了提供和其它语言的互操作性的一个手段。使用JNI带来的最大问题是引入了平台的依赖性，打破了SUN所鼓吹的“一次编写到处运行”的Java好处。因此，我决定采取第一种方法，一来和大家一起尝试一下“一次编写到处运行”的好处，二来检验一下Java 2现在对于比较密集的计算的效率问题。 </P>
<P><A name=3><SPAN class=atitle2>实现过程</SPAN></A><BR>限于这篇文章的篇幅，同时也为了更多的读者能够真正专注于问题本身，我不想就某一种Java集成开发环境来介绍这个Java Bean的制作过程，介绍一个方法时我发现步骤和命令很清晰，我相信有任何一种Java集成环境三天以上经验的读者都会知道如何把这些代码在集成环境中编译和运行。用集成环境讲述问题往往需要配很多屏幕截图，这也是我一直对集成环境很头疼的原因。我使用了一个普通的文本编辑器，同时使用了Sun公司标准的JDK 1.3.0 for Windows NT。</P>
<P>其实把C转换成Java对于一个有一定C语言基础的程序员并不困难，这两个语言的基本语法几乎完全一致．我大概花了一个小时的时间完成了代码的转换工作，我主要作了下面几件事：</P>
<OL xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">
<LI>把必须使用的一些#define的宏定义变成Class中的final static，这样保证在一个进程空间中的多个Instance共享这些数据 
<LI>删去了一些无用的#if define，因为我只关心MD5，这个推荐的C实现同时实现了MD2 MD3和 MD4，而且有些#if define还和C不同编译器有关 
<LI>将一些计算宏转换成final static 成员函数。 
<LI>所有的变量命名与原来C实现中保持一致，在大小写上作一些符合Java习惯的变化，计算过程中的C函数变成了private方法(成员函数)。 
<LI>关键变量的位长调整 
<LI>定义了类和方法 </LI></OL>
<P>需要注意的是，很多早期的C编译器的int类型是16 bit的，MD5使用了unsigned long int，并认为它是32bit的无符号整数。而在Java中int是32 bit的，long是64 bit的。在MD5的C实现中，使用了大量的位操作。这里需要指出的一点是，尽管Java提供了位操作，由于Java没有unsigned类型，对于右移位操作多提供了一个无符号右移：&gt;&gt;&gt;，等价于C中的 &gt;&gt; 对于unsigned 数的处理。</P>
<P>因为Java不提供无符号数的运算，两个大int数相加就会溢出得到一个负数或异常，因此我将一些关键变量在Java中改成了long类型(64bit)。我个人认为这比自己去重新定义一组无符号数的类同时重载那些运算符要方便，同时效率高很多并且代码也易读，OO(Object Oriented)的滥用反而会导致效率低下。</P>
<P>限于篇幅，这里不再给出原始的C代码，有兴趣对照的读者朋友可以去看RFC 1321。 <A href="http://www-128.ibm.com/developerworks/cn/java/javamd5/code.zip" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">MD5.java源代码</A> </P>
<P><A name=4><SPAN class=atitle2>测试</SPAN></A><BR>在RFC 1321中，给出了Test suite用来检验你的实现是否正确：</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
MD5 ("") = d41d8cd98f00b204e9800998ecf8427e

MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661

MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72

MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0

MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b

……
</CODE></PRE></TD></TR></TBODY></TABLE>
<P>这些输出结果的含义是指：空字符串””的MD5值是d41d8cd98f00b204e9800998ecf8427e，字符串”a”的MD5值是 <BR xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">0cc175b9c0f1b6a831c399e269772661…… <BR xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">编译并运行我们的程序： </P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
				
        <SPAN class=boldcode>javac –Cd . MD5.java
java beartool.MD5</SPAN>
			
      </CODE></PRE></TD></TR></TBODY></TABLE>
<P>为了将来不与别人的同名程序冲突，我在我的程序的第一行使用了package beartool;</P>
<P>因此编译命令 <B xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">javac �Cd . MD5.java</B> 命令在我们的工作目录下自动建立了一个 <B xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">beartool</B> 目录，目录下放着编译成功的 MD5.class </P>
<P>我们将得到和Test suite同样的结果。当然还可以继续测试你感兴趣的其它MD5变换，例如：</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
				
        <SPAN class=boldcode>java beartool.MD5　1234</SPAN>
			
      </CODE></PRE></TD></TR></TBODY></TABLE>
<P>将给出1234的MD5值。</P>
<P>可能是我的计算机知识是从Apple II和Z80单板机开始的，我对大写十六进制代码有偏好，如果您想使用小写的Digest String只需要把byteHEX函数中的A、B、C、D、E、F改成a、b、 c、d、e、f就可以了。</P>
<P>MD5据称是一种比较耗时的计算，我们的Java版MD5一闪就算出来了，没遇到什么障碍，而且用肉眼感觉不出来Java版的MD5比C版的慢。</P>
<P>为了测试它的兼容性，我把这个MD5.class文件拷贝到我的另一台Linux+IBM JDK 1.3的机器上，执行后得到同样结果，确实是“一次编写到处运行了”。 </P>
<P><A name=5><SPAN class=atitle2>Java Bean简述</SPAN></A><BR>现在，我们已经完成并简单测试了这个Java Class，我们文章的标题是做一个Java Bean。</P>
<P>其实普通的Java Bean很简单，并不是什么全新的或伟大的概念，就是一个Java的Class，尽管 Sun规定了一些需要实现的方法，但并不是强制的。而EJB(Enterprise Java Bean)无非规定了一些必须实现（非常类似于响应事件）的方法，这些方法是供EJB Container使用(调用)的。</P>
<P>在一个Java Application或Applet里使用这个bean非常简单，最简单的方法是你要使用这个类的源码工作目录下建一个beartool目录，把这个class文件拷贝进去，然后在你的程序中import beartool.MD5就可以了。最后打包成.jar或.war是保持这个相对的目录关系就行了。</P>
<P>Java还有一个小小的好处是你并不需要摘除我们的MD5类中那个main方法，它已经是一个可以工作的Java Bean了。Java有一个非常大的优点是她允许很方便地让多种运行形式在同一组代码中共存，比如，你可以写一个类，它即是一个控制台Application和GUI Application，同时又是一个Applet，同时还是一个Java Bean，这对于测试、维护和发布程序提供了极大的方便，这里的测试方法main还可以放到一个内部类中。 </P>
<P>这里讲述了把测试和示例代码放在一个内部静态类的好处，是一种不错的工程化技巧和途径。 </P>
<P><A name=6><SPAN class=atitle2>把Java Bean装到JSP里</SPAN></A><BR>正如我们在本文开头讲述的那样，我们对这个MD5 Bean的应用是基于一个用户管理，这里我们假设了一个虚拟社区的用户login过程，用户的信息保存在数据库的个名为users的表中。这个表有两个字段和我们的这个例子有关，userid :char(20)和pwdmd5 :char(32)，userid是这个表的Primary Key，pwdmd5保存密码的MD5串，MD5值是一个128bit的大整数，表示成16进制的ASCII需要32个字符。</P>
<P>这里给出两个文件， <A href="http://www-128.ibm.com/developerworks/cn/java/javamd5/login.html" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">login.html</A>是用来接受用户输入的form， <A href="http://www-128.ibm.com/developerworks/cn/java/javamd5/jsp.html" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">login.jsp</A> 用来模拟使用MD5 Bean的login过程。 </P>
<P>为了使我们的测试环境简单起见，我们在JSP中使用了JDK内置的JDBC-ODBC Bridge Driver，community是ODBC的DSN的名字，如果你使用其它的JDBC Driver，替换掉login.jsp中的 
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>Connection con= DriverManager.getConnection("jdbc:odbc:community", "", "");</CODE></PRE></TD></TR></TBODY></TABLE>即可。 </P>
<P>login.jsp的工作原理很简单，通过post接收用户输入的UserID和Password，然后将Password变换成MD5串，然后在users表中寻找UserID和pwdmd5，因为UserID是users表的Primary Key，如果变换后的pwdmd5与表中的记录不符，那么SQL查询会得到一个空的结果集。</P>
<P>这里需要简单介绍的是，使用这个Bean只需要在你的JSP应用程序的WEB-INF/classes下建立一个beartool目录，然后将MD5.class拷贝到那个目录下就可以了。如果你使用一些集成开发环境，请参考它们的deploy工具的说明。在JSP使用一个java Bean关键的一句声明是程序中的第2行：</P>
<P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD><PRE><CODE>&lt;jsp:useBean id='oMD5' scope='request' class='beartool.MD5'/&gt;</CODE></PRE></TD></TR></TBODY></TABLE>这是所有JSP规范要求JSP容器开发者必须提供的标准Tag。 </P>
<P>id=实际上是指示JSP Container创建Bean的实例时用的实例变量名。在后面的&lt;%和%&gt;之间的Java程序中，你可以引用它。在程序中可以看到，通过 <B xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">pwdmd5=oMD5.getMD5ofStr (password)</B> 引用了我们的MD5 Java Bean提供的唯一一个公共方法: <B xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">getMD5ofStr。</B> </P>
<P>Java Application Server执行.JSP的过程是先把它预编译成.java（那些Tag在预编译时会成为java语句），然后再编译成.class。这些都是系统自动完成和维护的，那个.class也称为Servlet。当然，如果你愿意，你也可以帮助Java Application Server去干本该它干的事情，自己直接去写Servlet，但用Servlet去输出HTML那简直是回到了用C写CGI程序的恶梦时代。</P>
<P>如果你的输出是一个复杂的表格，比较方便的方法我想还是用一个你所熟悉的HTML编辑器编写一个“模板”，然后在把JSP代码“嵌入”进去。尽管这种JSP代码被有些专家指责为“空心粉”，它的确有个缺点是代码比较难管理和重复使用，但是程序设计永远需要的就是这样的权衡。我个人认为，对于中、小型项目，比较理想的结构是把数据表示（或不严格地称作WEB界面相关）的部分用JSP写，和界面不相关的放在Bean里面，一般情况下是不需要直接写Servlet的。</P>
<P>如果你觉得这种方法不是非常的OO(Object Oriented)，你可以继承（extends）它一把，再写一个bean把用户管理的功能包进去。 </P>
<P><A name=7><SPAN class=atitle2>到底能不能兼容？</SPAN></A><BR>我测试了三种Java应用服务器环境，Resin 1.2.3、Sun J2EE 1.2、IBM WebSphere 3.5，所幸的是这个Java Bean都没有任何问题，原因其实是因为它仅仅是个计算程序，不涉及操作系统，I/O设备。其实用其它语言也能简单地实现它的兼容性的，Java的唯一优点是，你只需提供一个形态的运行码就可以了。请注意“形态”二字，现在很多计算结构和操作系统除了语言本身之外都定义了大量的代码形态，很简单的一段C语言核心代码，转换成不同形态要考虑很多问题，使用很多工具，同时受很多限制，有时候学习一种新的“形态”所花费的精力可能比解决问题本身还多。比如光Windows就有EXE、Service、的普通DLL、COM DLL以前还有OCX等等等等，在Unix上虽说要简单一些，但要也要提供一个.h定义一大堆宏，还要考虑不同平台编译器版本的位长度问题。我想这是Java对我来说的一个非常重要的魅力吧。 </P>
<P><A name=resources><SPAN class=atitle2>参考资料 </SPAN></A>
<P>IETF RFC 1321 <A href="http://%20http://www.ietf.org/rfc/rfc1321.txt" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">http:// http://www.ietf.org/rfc/rfc1321.txt</A> 这是关于MD5最原始和权威的文档，同时包含了完整的C语言实现。这份文档是1992年提交的，它的C程序还不是ANSI标准语法的，有些老Unix的亲切感。在Windows下编译这个C程序要小小改动一下。 </P>
<P>J2EE教程 <A href="http://java.sun.com/j2ee/tutorial/doc/information/resources.html" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">http://java.sun.com/j2ee/tutorial/doc/information/resources.html</A> SUN的J2EE教程编得很不错，言简意赅，为全面了解Java Bean和JSP背后的体系结构提供了一个不错的起点或索引。 </P>
<P>Bruce Eckel《Think in Java 2 <SUP xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dw="http://www.ibm.com/developerworks/">nd</SUP>Edition》 这也是Developer Works推荐下载的一本不错的Java教科书，我个人认为这本书是写给至少有一种程序设计语言经验的程序员的。 </P>
<P></P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><A name=author1></A><SPAN class=atitle2>关于作者</SPAN><BR>作者本人于1991年毕业于武汉测绘科技大学计算机科学与工程系，毕业后被分配到中国计算机软件与技术服务总公司，从事国产Unix系统开发和金融系统应用，三年后辞职创业，但至今未成大器。自中学时代开始接触Apple II，经历了中国软件业到目前为止的完整发展历程，在十多年的软件工作生涯中，主要从事银行、零售业系统的开发、体系架构设计和项目管理，曾经成功地主导开发和实施过多个大中型POS系统项目、银行业务系统和企业MIS系统，现为自由职业者，主要为若干固定客户从事软件项目的技术咨询工作，同时作为自由撰稿人为一些网站和电子媒体介绍一些新的企业计算技术。此外，自己建立了一个小型的个人新技术实验室，有若干台装有不同平台的电脑并且连成一个小型的Intranet。最大的爱好就是不断地去弄明白层出不穷的软件技术和概念。 </TD></TR></TBODY></TABLE><BR clear=all><img src ="http://www.blogjava.net/lmsun/aggbug/10437.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-08-18 15:39 <a href="http://www.blogjava.net/lmsun/articles/10437.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JAVA 的MD5加密算法源代码</title><link>http://www.blogjava.net/lmsun/articles/10435.html</link><dc:creator>my java</dc:creator><author>my java</author><pubDate>Thu, 18 Aug 2005 07:35:00 GMT</pubDate><guid>http://www.blogjava.net/lmsun/articles/10435.html</guid><wfw:comment>http://www.blogjava.net/lmsun/comments/10435.html</wfw:comment><comments>http://www.blogjava.net/lmsun/articles/10435.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lmsun/comments/commentRss/10435.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lmsun/services/trackbacks/10435.html</trackback:ping><description><![CDATA[<DIV class=postText>
<P><SPAN id=ArticleContent1_ArticleContent1_lblContent><SPAN id=ArticleContent1_ArticleContent1_lblContent>import java.security.*;<BR>import java.security.spec.*; </P>
<P>class MD5_Test{</P>
<P>&nbsp;&nbsp; public final static String MD5(String s){<BR>&nbsp;&nbsp;&nbsp;&nbsp; char hexDigits[] = {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'e', 'f'};<BR>&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] strTemp = s.getBytes();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MessageDigest mdTemp = MessageDigest.getInstance("MD5");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mdTemp.update(strTemp);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] md = mdTemp.digest();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int j = md.length;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char str[] = new char[j * 2];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int k = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; j; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte byte0 = md[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str[k++] = hexDigits[byte0 &gt;&gt;&gt; 4 &amp; 0xf];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str[k++] = hexDigits[byte0 &amp; 0xf];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new String(str);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception e){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>}<BR>&nbsp;public static void main(String[] args){<BR>&nbsp;//MD5_Test aa = new MD5_Test();</P>
<P>&nbsp;System.out.print(MD5_Test.MD5("XX"));<BR>&nbsp;}<FONT color=#800080><U><BR></FONT></U></P></SPAN></SPAN></DIV>}<A id=viewpost1_TitleUrl HREF="/relax/archive/2005/02/02/923.html"></A><img src ="http://www.blogjava.net/lmsun/aggbug/10435.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lmsun/" target="_blank">my java</a> 2005-08-18 15:35 <a href="http://www.blogjava.net/lmsun/articles/10435.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>