﻿<?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-孤灯野火-文章分类-Java</title><link>http://www.blogjava.net/liudawei/category/39773.html</link><description>畅想的天空</description><language>zh-cn</language><lastBuildDate>Tue, 05 Aug 2014 04:11:29 GMT</lastBuildDate><pubDate>Tue, 05 Aug 2014 04:11:29 GMT</pubDate><ttl>60</ttl><item><title>java发送http的get、post请求</title><link>http://www.blogjava.net/liudawei/articles/416574.html</link><dc:creator>孤飞燕</dc:creator><author>孤飞燕</author><pubDate>Tue, 05 Aug 2014 02:59:00 GMT</pubDate><guid>http://www.blogjava.net/liudawei/articles/416574.html</guid><wfw:comment>http://www.blogjava.net/liudawei/comments/416574.html</wfw:comment><comments>http://www.blogjava.net/liudawei/articles/416574.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liudawei/comments/commentRss/416574.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liudawei/services/trackbacks/416574.html</trackback:ping><description><![CDATA[<div>package wzh.Http;</div><div></div><div>import java.io.BufferedReader;</div><div>import java.io.IOException;</div><div>import java.io.InputStreamReader;</div><div>import java.io.PrintWriter;</div><div>import java.net.URL;</div><div>import java.net.URLConnection;</div><div>import java.util.List;</div><div>import java.util.Map;</div><div></div><div>public class HttpRequest {</div><div>&nbsp; &nbsp; /**</div><div>&nbsp; &nbsp; &nbsp;* 向指定URL发送GET方法的请求</div><div>&nbsp; &nbsp; &nbsp;*&nbsp;</div><div>&nbsp; &nbsp; &nbsp;* @param url</div><div>&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;发送请求的URL</div><div>&nbsp; &nbsp; &nbsp;* @param param</div><div>&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;请求参数，请求参数应该是 name1=value1&amp;name2=value2 的形式。</div><div>&nbsp; &nbsp; &nbsp;* @return URL 所代表远程资源的响应结果</div><div>&nbsp; &nbsp; &nbsp;*/</div><div>&nbsp; &nbsp; public static String sendGet(String url, String param) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; String result = "";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; BufferedReader in = null;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; try {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String urlNameString = url + "?" + param;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URL realUrl = new URL(urlNameString);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 打开和URL之间的连接</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URLConnection connection = realUrl.openConnection();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 设置通用的请求属性</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection.setRequestProperty("accept", "*/*");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection.setRequestProperty("connection", "Keep-Alive");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection.setRequestProperty("user-agent",</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 建立实际的连接</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection.connect();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 获取所有响应头字段</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map&lt;String, List&lt;String&gt;&gt; map = connection.getHeaderFields();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 遍历所有的响应头字段</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String key : map.keySet()) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(key + "---&gt;" + map.get(key));</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 定义 BufferedReader输入流来读取URL的响应</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in = new BufferedReader(new InputStreamReader(</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection.getInputStream()));</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = in.readLine()) != null) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += line;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("发送GET请求出现异常！" + e);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // 使用finally块来关闭输入流</div><div>&nbsp; &nbsp; &nbsp; &nbsp; finally {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (in != null) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in.close();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e2) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e2.printStackTrace();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return result;</div><div>&nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; /**</div><div>&nbsp; &nbsp; &nbsp;* 向指定 URL 发送POST方法的请求</div><div>&nbsp; &nbsp; &nbsp;*&nbsp;</div><div>&nbsp; &nbsp; &nbsp;* @param url</div><div>&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;发送请求的 URL</div><div>&nbsp; &nbsp; &nbsp;* @param param</div><div>&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;请求参数，请求参数应该是 name1=value1&amp;name2=value2 的形式。</div><div>&nbsp; &nbsp; &nbsp;* @return 所代表远程资源的响应结果</div><div>&nbsp; &nbsp; &nbsp;*/</div><div>&nbsp; &nbsp; public static String sendPost(String url, String param) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; PrintWriter out = null;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; BufferedReader in = null;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; String result = "";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; try {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URL realUrl = new URL(url);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 打开和URL之间的连接</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URLConnection conn = realUrl.openConnection();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 设置通用的请求属性</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.setRequestProperty("accept", "*/*");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.setRequestProperty("connection", "Keep-Alive");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.setRequestProperty("user-agent",</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 发送POST请求必须设置如下两行</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.setDoOutput(true);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.setDoInput(true);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 获取URLConnection对象对应的输出流</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = new PrintWriter(conn.getOutputStream());</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 发送请求参数</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.print(param);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // flush输出流的缓冲</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.flush();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 定义BufferedReader输入流来读取URL的响应</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in = new BufferedReader(</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new InputStreamReader(conn.getInputStream()));</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = in.readLine()) != null) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += line;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("发送 POST 请求出现异常！"+e);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; //使用finally块来关闭输出流、输入流</div><div>&nbsp; &nbsp; &nbsp; &nbsp; finally{</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(out!=null){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.close();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(in!=null){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in.close();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch(IOException ex){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return result;</div><div>&nbsp; &nbsp; } &nbsp; &nbsp;</div><div>}<br /><br />public static void main(String[] args) {<div>&nbsp; &nbsp; &nbsp; &nbsp; //发送 GET 请求</div><div>&nbsp; &nbsp; &nbsp; &nbsp; String s=HttpRequest.sendGet("http://localhost:6144/Home/RequestString", "key=123&amp;v=456");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(s);</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; //发送 POST 请求</div><div>&nbsp; &nbsp; &nbsp; &nbsp; String sr=HttpRequest.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&amp;v=456");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sr);</div><div>&nbsp; &nbsp; }</div><br /></div><img src ="http://www.blogjava.net/liudawei/aggbug/416574.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liudawei/" target="_blank">孤飞燕</a> 2014-08-05 10:59 <a href="http://www.blogjava.net/liudawei/articles/416574.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java时区的转换</title><link>http://www.blogjava.net/liudawei/articles/387891.html</link><dc:creator>孤飞燕</dc:creator><author>孤飞燕</author><pubDate>Mon, 17 Sep 2012 06:15:00 GMT</pubDate><guid>http://www.blogjava.net/liudawei/articles/387891.html</guid><wfw:comment>http://www.blogjava.net/liudawei/comments/387891.html</wfw:comment><comments>http://www.blogjava.net/liudawei/articles/387891.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liudawei/comments/commentRss/387891.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liudawei/services/trackbacks/387891.html</trackback:ping><description><![CDATA[<p class="MsoNormal"><span lang="EN-US">Java</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">时区的转换<br /></span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">逻辑如下：<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span lang="EN-US">1.</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">明确你要转的当前时间所在的时区</span></p><p style="text-indent: 21pt;" class="MsoNormal"><span lang="EN-US">2.</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">明确你要转向时间所在的时区</span></p><p style="text-indent: 21pt;" class="MsoNormal"><span lang="EN-US">3.</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">获取当前时间所在时区相对</span><span lang="EN-US">GMT</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">的偏移量</span></p><p style="text-indent: 21pt;" class="MsoNormal"><span lang="EN-US">4.</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">当前时间</span><span lang="EN-US">-</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">相对</span><span lang="EN-US">GMT</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">的偏移量来获得当前时间的</span><span lang="EN-US">GMT</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">的值</span></p><p style="text-indent: 21pt;" class="MsoNormal"><span lang="EN-US">5.GMT</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">的值</span><span lang="EN-US">+</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">转向时区相对</span><span lang="EN-US">GMT</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">的偏移量获取转向时区的时间值</span></p><p class="MsoNormal"><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">例如：你要从</span><span lang="EN-US">GMT+8</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">时间转向</span><span lang="EN-US">GMT+7</span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin;">时间<br />sourceTimeZone 为<span lang="EN-US">GMT+8</span></span></p>targetTimeZone为GMT+7<br /><p>/**<br />&nbsp;* <br />&nbsp;*/<br />package test;</p><p>import java.text.ParseException;<br />import java.text.SimpleDateFormat;<br />import java.util.Date;<br />import java.util.TimeZone;</p><p><br />public class DateTimeUtil {<br />&nbsp;<br />&nbsp;static String DEFAULT_TIMEZONE = "GMT+8";<br />&nbsp;static String DEFAULT_FORMAT = "d-MMM-yyyy HH:mm (z)";</p><p>&nbsp;/**<br />&nbsp; * 转换时间时区<br />&nbsp; * @param convertString&nbsp; 需要转的时间字符串<br />&nbsp; * @param format&nbsp; 格式话字符串 例如d-MMM-yyyy HH:mm (z)<br />&nbsp; * @param sourceTimeZone 源时间时区<br />&nbsp; * @param targetTimeZone 目标时间时区<br />&nbsp; * @return<br />&nbsp; * @throws ParseException<br />&nbsp; */<br />&nbsp;public static Date ConverDateGMT(String convertString,String format,String sourceTimeZone,String targetTimeZone) throws ParseException<br />&nbsp;{<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;Date date=null;<br />&nbsp;&nbsp;</p><p>&nbsp;&nbsp;if(isEmpty(sourceTimeZone)){<br />&nbsp;&nbsp;&nbsp;sourceTimeZone = DEFAULT_TIMEZONE;<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;if(isEmpty(targetTimeZone)){<br />&nbsp;&nbsp;&nbsp;targetTimeZone = DEFAULT_TIMEZONE;<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;if(isEmpty(format)){<br />&nbsp;&nbsp;&nbsp;format = DEFAULT_FORMAT;<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;SimpleDateFormat sdf = new SimpleDateFormat(format);<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;//获取传入的时间值<br />&nbsp;&nbsp;Long time = new Date(sdf.parse(convertString).getTime()).getTime();<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;//获取源时区时间相对的GMT时间<br />&nbsp;&nbsp;Long sourceRelativelyGMT=time-TimeZone.getTimeZone(sourceTimeZone).getRawOffset();<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;//GMT时间+目标时间时区的偏移量获取目标时间<br />&nbsp;&nbsp;Long targetTime=sourceRelativelyGMT+TimeZone.getTimeZone(targetTimeZone).getRawOffset();<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;date= new Date(targetTime);<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;return date;<br />&nbsp;&nbsp;<br />&nbsp;}<br />&nbsp;<br />&nbsp;<br />&nbsp;/**<br />&nbsp; * Check empty string<br />&nbsp; * &lt;pre&gt;<br />&nbsp; *&nbsp;&nbsp; null: true<br />&nbsp; *&nbsp;&nbsp; "": true<br />&nbsp; *&nbsp;&nbsp; " ":true<br />&nbsp; * &lt;/&gt;<br />&nbsp; * <br />&nbsp; * @param value<br />&nbsp; * @return<br />&nbsp; */<br />&nbsp;public static boolean isEmpty(String value) {<br />&nbsp;&nbsp;boolean emptyFlg = false;<br />&nbsp;&nbsp;if (null == value || value.trim().length() &lt;= 0) {<br />&nbsp;&nbsp;&nbsp;emptyFlg = true;<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;return emptyFlg;<br />&nbsp;}<br />}<br />&nbsp;<br />&nbsp;<br /></p><br /><br />  
 <img src ="http://www.blogjava.net/liudawei/aggbug/387891.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liudawei/" target="_blank">孤飞燕</a> 2012-09-17 14:15 <a href="http://www.blogjava.net/liudawei/articles/387891.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java 页面url传值中文编码&amp;解码</title><link>http://www.blogjava.net/liudawei/articles/349990.html</link><dc:creator>孤飞燕</dc:creator><author>孤飞燕</author><pubDate>Wed, 11 May 2011 02:05:00 GMT</pubDate><guid>http://www.blogjava.net/liudawei/articles/349990.html</guid><wfw:comment>http://www.blogjava.net/liudawei/comments/349990.html</wfw:comment><comments>http://www.blogjava.net/liudawei/articles/349990.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liudawei/comments/commentRss/349990.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liudawei/services/trackbacks/349990.html</trackback:ping><description><![CDATA[<p style="text-indent: 2em">URL参数中有中文值，传到服务端，在用request.getParameter()方法，得到的常常会是乱码。</p>
<p style="text-indent: 2em">这将涉及到字符解码操作，我们在应用中常常会用new String(fieldType.getBytes("iso-8859-1"), "UTF-8");等类似的方法去解码。但这种方式受具体应用环境限制，往往在应用部署环境发生改变时，还会出现中文乱码。</p>
<p style="text-indent: 2em">在这里介绍一种解决方法，可以在任何应用部署环境下通用。此方法分两步：</p>
<p style="text-indent: 2em">1、在客户端用escape(encodeURIComponent(fieldValue))方法编码，例如：</p>
<p style="text-indent: 2em">title=escape(encodeURIComponent(title)); //这是js里的函数</p>
<p style="text-indent: 2em">&nbsp;url="&lt;%=request.getContextPath()%&gt;/print/printList!printTable.act<wbr>ion?title="+title;</p>
<p style="text-indent: 2em">2、在服务端用java.net.URLDecoder.decode(getRequest().getParameter("title"),"UTF-8")，进行解码。</p>
<p style="text-indent: 2em">&nbsp;</p>
<p style="text-indent: 2em">-----------------------------------------------------------------------------</p>
<p style="text-indent: 2em">parent.window.location.href 和 iframe中src的乱码问题。 </p>
<p style="text-indent: 2em">要在这两个url地址中传中文，必须加编码，然后再解码。 </p>
<p style="text-indent: 2em">编码：encodeURI(encodeURI("包含中文的串")) </p>
<p style="text-indent: 2em">解码：java.net.URLDecoder.decode("需要解码的串","utf-8"); </p>
<p>&nbsp;</p>
<p>encodeURI方法是正确的，只是需要使用两次encodeURI方法，例如encodeURI(encodeURI("中文"));第一次是把中文编码成%xy的格式，第二次是对%xy中的%进行编码,%编码成%25。整个传参过程大体应该是：提交页面使用encodeURI(encodeURI("中文"))编码,把最后的编码结果%25xy传递给处理页面的过程中，浏览器获取URL地址（注意openModelDialog方法，浏览器获取不到参数编码）后解码成%xy，然后把%xy传递给处理页面,处理页面使用URLDecoder.decode(request.getParameter("参数名"),"UTF-8");完成解码。<br />
总结：<br />
1、汉字出现在URL路径部分的时候不需要编码解码;<br />
2、使用encodeURI进行2次编码;<br />
3、在openModelDialog()打开的模式窗体里没办法用request.getParameter正确获取参数;<br />
</p>
 <img src ="http://www.blogjava.net/liudawei/aggbug/349990.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liudawei/" target="_blank">孤飞燕</a> 2011-05-11 10:05 <a href="http://www.blogjava.net/liudawei/articles/349990.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>利用Java形成的随机抽取</title><link>http://www.blogjava.net/liudawei/articles/340982.html</link><dc:creator>孤飞燕</dc:creator><author>孤飞燕</author><pubDate>Fri, 17 Dec 2010 06:45:00 GMT</pubDate><guid>http://www.blogjava.net/liudawei/articles/340982.html</guid><wfw:comment>http://www.blogjava.net/liudawei/comments/340982.html</wfw:comment><comments>http://www.blogjava.net/liudawei/articles/340982.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liudawei/comments/commentRss/340982.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liudawei/services/trackbacks/340982.html</trackback:ping><description><![CDATA[<p>//抽取随机数逻辑方法<br />
&nbsp;&nbsp;&nbsp;int maxSize = listZxsl.size();//listZxsl 假设为已经得到的list值 想从中随机抽取几个<br />
&nbsp;&nbsp;&nbsp;HashSet&lt;Integer&gt; set = new HashSet&lt;Integer&gt;();<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;xysl=5;//假设需要抽取的数量为5个<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;//产生的个数<br />
&nbsp;&nbsp;&nbsp;inttempMaxSize=null;<br />
&nbsp;&nbsp;&nbsp;if(xysl&gt;maxSize)<br />
&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;tempMaxSize=maxSize ;<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;else<br />
&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;tempMaxSize=xysl;<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;while (true) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;//产生的索引值<br />
&nbsp;&nbsp;&nbsp;&nbsp;int randNumber = (int) (Math.random() * maxSize + 1) - 1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;set.add(randNumber);<br />
&nbsp;&nbsp;&nbsp;&nbsp;if (set.size() &gt;= tempMaxSize) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;for(int number:set)<br />
&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;templist.add(listZxsl.get(number));//templist为返回出去的<br />
&nbsp;&nbsp;&nbsp;}</p>
<img src ="http://www.blogjava.net/liudawei/aggbug/340982.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liudawei/" target="_blank">孤飞燕</a> 2010-12-17 14:45 <a href="http://www.blogjava.net/liudawei/articles/340982.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C3po连接池</title><link>http://www.blogjava.net/liudawei/articles/305652.html</link><dc:creator>孤飞燕</dc:creator><author>孤飞燕</author><pubDate>Fri, 11 Dec 2009 14:33:00 GMT</pubDate><guid>http://www.blogjava.net/liudawei/articles/305652.html</guid><wfw:comment>http://www.blogjava.net/liudawei/comments/305652.html</wfw:comment><comments>http://www.blogjava.net/liudawei/articles/305652.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liudawei/comments/commentRss/305652.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liudawei/services/trackbacks/305652.html</trackback:ping><description><![CDATA[&nbsp;
<p><span style="color: maroon">jdbc.properties</span><span style="color: maroon; font-family: 宋体">配置文件</span><span style="font-family: 宋体">：</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">jdbc.driverClassName=</span><u><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">com</span></u><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">.<u>ibm</u>.db2.<u>jcc</u>.DB2Driver</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: #3f7f5f; font-family: 'Courier New'">#---------------------------------------------------</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">DEVELOP </span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">DATABASE</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">jdbc.url=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">jdbc</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">:</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">db2://10.10.0.163:50000/MACRODB</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: #3f7f5f; font-family: 'Courier New'">#<u>jdbc</u>.<u>url</u>=jdbc:db2://10.10.0.154:50000/SAMPLE</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: #3f7f5f; font-family: 'Courier New'">#---------------------------------------------------</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: #3f7f5f; font-family: 'Courier New'">#TEST DATABASE</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: #3f7f5f; font-family: 'Courier New'">#<u>jdbc</u>.<u>url</u>=jdbc:oracle:thin:@192.168.1.100:1521:<u>orcl</u></span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: #3f7f5f; font-family: 'Courier New'">#---------------------------------------------------</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: #3f7f5f; font-family: 'Courier New'">#LOCALHOST DATABASE</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: #3f7f5f; font-family: 'Courier New'">#<u>jdbc</u>.<u>url</u>=jdbc:oracle:thin:@127.0.0.1:1521:<u>orcl</u></span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: #3f7f5f; font-family: 'Courier New'">#<u>jdbc</u>.<u>username</u>=db2inst1</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: #3f7f5f; font-family: 'Courier New'">#<u>jdbc</u>.password=db2inst1</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">jdbc.username=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">db2inst1</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">jdbc.password=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">123456</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">c3p0.acquireIncrement=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">3</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">c3p0.initialPoolSize=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">3</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">c3p0.minPoolSize=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">10</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">c3p0.maxPoolSize=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">15</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">c3p0.maxIdleTime=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">30</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">c3p0.idleConnectionTestPeriod=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">30</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">c3p0.maxStatements=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">100</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">c3p0.numHelperThreads=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">50</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">c3p0.checkoutTimeout=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">0</span></p>
<p style="margin-left: 10.5pt; text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">c3p0.validate=</span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">true</span></p>
<p><span style="color: maroon; font-family: 宋体">读取配置文件：</span></p>
<p style="text-align: left" align="left"><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">package</span></strong><span style="font-size: 10pt; color: black; font-family: 'Courier New'"> com.nci.macrodb.core.sql;</span></p>
<p style="text-align: left" align="left"><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">import</span></strong><span style="font-size: 10pt; color: black; font-family: 'Courier New'"> java.util.ResourceBundle;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">/**</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">*</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 宋体">取得资源文件</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">*</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">*</span><strong><span style="font-size: 10pt; color: #7f9fbf; font-family: 'Courier New'">@author</span></strong><u><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">ldw</span></u></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">*</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">*/</span></p>
<p style="text-align: left" align="left"><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">public</span></strong><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">class</span></strong><span style="font-size: 10pt; color: black; font-family: 'Courier New'"> C3P0SystemConfig {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">static</span></strong><span style="font-size: 10pt; color: black; font-family: 'Courier New'"> String </span><em><span style="font-size: 10pt; color: #0000c0; font-family: 'Courier New'">configFile</span></em><span style="font-size: 10pt; color: black; font-family: 'Courier New'"> = </span><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">"spring/jdbc"</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">;//</span><span style="font-size: 10pt; color: black; font-family: 宋体">根据具体配置文件名称配置</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp; </span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">/**</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp; &nbsp;</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">*</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 宋体">根据属性名得到资源属性</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp; &nbsp;</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">*</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp; &nbsp;</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">*</span><strong><span style="font-size: 10pt; color: #7f9fbf; font-family: 'Courier New'">@param</span></strong><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">itemIndex</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp; &nbsp;</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">*</span><strong><span style="font-size: 10pt; color: #7f9fbf; font-family: 'Courier New'">@return</span></strong></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp; &nbsp;</span><span style="font-size: 10pt; color: #3f5fbf; font-family: 'Courier New'">*/</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">public</span></strong><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">static</span></strong><span style="font-size: 10pt; color: black; font-family: 'Courier New'"> String getConfigInfomation(String itemIndex) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">try</span></strong><span style="font-size: 10pt; color: black; font-family: 'Courier New'"> {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ResourceBundle resource = ResourceBundle.<em>getBundle</em>(</span><em><span style="font-size: 10pt; color: #0000c0; font-family: 'Courier New'">configFile</span></em><span style="font-size: 10pt; color: black; font-family: 'Courier New'">);</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">return</span></strong><span style="font-size: 10pt; color: black; font-family: 'Courier New'"> resource.getString(itemIndex);</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </span><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">catch</span></strong><span style="font-size: 10pt; color: black; font-family: 'Courier New'"> (Exception e) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 10pt; color: #7f0055; font-family: 'Courier New'">return</span></strong><span style="font-size: 10pt; color: #2a00ff; font-family: 'Courier New'">""</span><span style="font-size: 10pt; color: black; font-family: 'Courier New'">;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 10pt; color: black; font-family: 'Courier New'">}<br />
<br />
<br />
<br />
获得连接：<br />
</p>
<p>package com.nci.macrodb.core.sql;</p>
<p>import java.sql.Connection;<br />
import java.sql.SQLException;</p>
<p>import com.mchange.v2.c3p0.ComboPooledDataSource;</p>
<p>/**<br />
&nbsp;* 编程调用c3p0<br />
&nbsp;* <br />
&nbsp;* @author xuhua<br />
&nbsp;* <br />
&nbsp;*/<br />
public class C3P0DBConnectionManager {<br />
&nbsp;private static ComboPooledDataSource cpds = null;</p>
<p>&nbsp;/**<br />
&nbsp; * 初始化<br />
&nbsp; */<br />
&nbsp;public static void init() {<br />
&nbsp;&nbsp;// 建立数据库连接池<br />
&nbsp;&nbsp;String DRIVER_NAME = C3P0SystemConfig<br />
&nbsp;&nbsp;&nbsp;&nbsp;.getConfigInfomation("jdbc.driverClassName"); // 驱动器<br />
&nbsp;&nbsp;String DATABASE_URL = C3P0SystemConfig.getConfigInfomation("jdbc.url"); // 数据库连接url<br />
&nbsp;&nbsp;String DATABASE_USER = C3P0SystemConfig<br />
&nbsp;&nbsp;&nbsp;&nbsp;.getConfigInfomation("jdbc.username"); // 数据库用户名<br />
&nbsp;&nbsp;String DATABASE_PASSWORD = C3P0SystemConfig<br />
&nbsp;&nbsp;&nbsp;&nbsp;.getConfigInfomation("jdbc.password"); // 数据库密码<br />
&nbsp;&nbsp;int Min_PoolSize = 5;<br />
&nbsp;&nbsp;int Max_PoolSize = 50;<br />
&nbsp;&nbsp;int Acquire_Increment = 5;<br />
&nbsp;&nbsp;int Initial_PoolSize = 10;<br />
&nbsp;&nbsp;// 每隔3000s测试连接是否可以正常使用<br />
&nbsp;&nbsp;int Idle_Test_Period = 3000;<br />
&nbsp;&nbsp;// 每次连接验证连接是否可用<br />
&nbsp;&nbsp;String Validate = C3P0SystemConfig.getConfigInfomation("c3p0.validate");<br />
&nbsp;&nbsp;if (Validate.equals("")) {<br />
&nbsp;&nbsp;&nbsp;Validate = "false";<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;// 最小连接数<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;Min_PoolSize = Integer.parseInt(C3P0SystemConfig<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getConfigInfomation("c3p0.minPoolSize"));<br />
&nbsp;&nbsp;} catch (Exception ex) {<br />
&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;// 增量条数<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;Acquire_Increment = Integer.parseInt(C3P0SystemConfig<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getConfigInfomation("c3p0.acquireIncrement"));<br />
&nbsp;&nbsp;} catch (Exception ex) {<br />
&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;// 最大连接数<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;Max_PoolSize = Integer.parseInt(C3P0SystemConfig<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getConfigInfomation("c3p0.maxPoolSize"));<br />
&nbsp;&nbsp;} catch (Exception ex) {<br />
&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;// 初始化连接数<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;Initial_PoolSize = Integer.parseInt(C3P0SystemConfig<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getConfigInfomation("c3p0.initialPoolSize"));<br />
&nbsp;&nbsp;} catch (Exception ex) {<br />
&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;// 每隔Idle_Test_Period s测试连接是否可以正常使用<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;Idle_Test_Period = Integer.parseInt(C3P0SystemConfig<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getConfigInfomation("c3p0.idleConnectionTestPeriod"));<br />
&nbsp;&nbsp;} catch (Exception ex) {<br />
&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br />
&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;cpds = new ComboPooledDataSource();<br />
&nbsp;&nbsp;&nbsp;cpds.setDriverClass(DRIVER_NAME); // 驱动器<br />
&nbsp;&nbsp;&nbsp;cpds.setJdbcUrl(DATABASE_URL); // 数据库url<br />
&nbsp;&nbsp;&nbsp;cpds.setUser(DATABASE_USER); // 用户名<br />
&nbsp;&nbsp;&nbsp;cpds.setPassword(DATABASE_PASSWORD); // 密码<br />
&nbsp;&nbsp;&nbsp;cpds.setInitialPoolSize(Initial_PoolSize); // 初始化连接池大小<br />
&nbsp;&nbsp;&nbsp;cpds.setMinPoolSize(Min_PoolSize); // 最少连接数<br />
&nbsp;&nbsp;&nbsp;cpds.setMaxPoolSize(Max_PoolSize); // 最大连接数<br />
&nbsp;&nbsp;&nbsp;cpds.setAcquireIncrement(Acquire_Increment); // 连接数的增量<br />
&nbsp;&nbsp;&nbsp;cpds.setIdleConnectionTestPeriod(Idle_Test_Period); // 测连接有效的时间间隔<br />
&nbsp;&nbsp;&nbsp;cpds.setTestConnectionOnCheckout(Boolean.getBoolean(Validate)); // 每次连接验证连接是否可用<br />
&nbsp;&nbsp;} catch (Exception ex) {<br />
&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;}</p>
<p>&nbsp;/**<br />
&nbsp; * 取得链接<br />
&nbsp; * <br />
&nbsp; * @return<br />
&nbsp; */<br />
&nbsp;public static Connection getConnection() {<br />
&nbsp;&nbsp;Connection connection = null;<br />
&nbsp;&nbsp;try {// 保证只进行一次初始化<br />
&nbsp;&nbsp;&nbsp;if (cpds == null) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;init();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;// 取得connection<br />
&nbsp;&nbsp;&nbsp;connection = cpds.getConnection();<br />
&nbsp;&nbsp;} catch (SQLException ex) {<br />
&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;return connection;<br />
&nbsp;}</p>
<p>&nbsp;/**<br />
&nbsp; * 释放连接<br />
&nbsp; */<br />
&nbsp;public static void release() {<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;if (cpds != null) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;cpds.close();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;} catch (Exception ex) {<br />
&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;}</p>
<p>}<br />
<br />
<br />
</p>
</span>
<img src ="http://www.blogjava.net/liudawei/aggbug/305652.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liudawei/" target="_blank">孤飞燕</a> 2009-12-11 22:33 <a href="http://www.blogjava.net/liudawei/articles/305652.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>J2EE路径的通用解决方案</title><link>http://www.blogjava.net/liudawei/articles/283657.html</link><dc:creator>孤飞燕</dc:creator><author>孤飞燕</author><pubDate>Mon, 22 Jun 2009 15:49:00 GMT</pubDate><guid>http://www.blogjava.net/liudawei/articles/283657.html</guid><wfw:comment>http://www.blogjava.net/liudawei/comments/283657.html</wfw:comment><comments>http://www.blogjava.net/liudawei/articles/283657.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liudawei/comments/commentRss/283657.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liudawei/services/trackbacks/283657.html</trackback:ping><description><![CDATA[<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="ProgId" content="Word.Document" />
<meta name="Generator" content="Microsoft Word 11" />
<meta name="Originator" content="Microsoft Word 11" />
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cldw%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" /><!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:PunctuationKerning/>
<w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:SpaceForUL/>
<w:BalanceSingleByteDoubleByteWidth/>
<w:DoNotLeaveBackslashAlone/>
<w:ULTrailSpace/>
<w:DoNotExpandShiftReturn/>
<w:AdjustLineHeightInTable/>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:UseFELayout/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles deflockedstate="false" latentstylecount="156">
</w:LatentStyles>
</xml><![endif]--><style>
<!-- /* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->
</style><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
<p class="MsoNormal"><span lang="EN-US">Web.xml</span><span style="font-family: 宋体;">配置：</span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">servlet</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">servlet-name</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">MainServlet</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;/</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">servlet-name</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">servlet-class</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">com.wes.controller.MainServlet</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;/</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">servlet-class</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">init-param</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">param-name</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">param1</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;/</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">param-name</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">param-value</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">avalible in servlet init()</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;/</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">param-value</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;/</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">init-param</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">load-on-startup</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">0</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;/</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">load-on-startup</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&lt;/</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(63, 127, 127);" lang="EN-US">servlet</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: teal;" lang="EN-US">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"> <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 宋体; color: black;">配置一个</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">Servlet </span><span style="font-size: 10pt; font-family: 宋体; color: black;">名称为：</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">MainServlet<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">MainServlet</span><span style="font-size: 10pt; font-family: 宋体; color: black;">：</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span></span><strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(127, 0, 85);" lang="EN-US">package</span></strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"> com.wes.controller;<span>&nbsp;&nbsp;
</span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(127, 0, 85);" lang="EN-US">import</span></strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"> javax.servlet.ServletException;<span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(127, 0, 85);" lang="EN-US">import</span></strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"> javax.servlet.http.HttpServlet;<span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;</span></span><strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(127, 0, 85);" lang="EN-US">public</span></strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"> </span><strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(127, 0, 85);" lang="EN-US">class</span></strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"> MainServlet </span><strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(127, 0, 85);" lang="EN-US">extends</span></strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"> HttpServlet{<span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp; </span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp;&nbsp; </span></span><strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(127, 0, 85);" lang="EN-US">public</span></strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"> </span><strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(127, 0, 85);" lang="EN-US">void</span></strong><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"> init(){<span>&nbsp;&nbsp;
</span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span><span> </span><span>&nbsp;</span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span><span> </span><span>&nbsp;</span>String webAppRootKey = getServletContext().getRealPath(</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(42, 0, 255);" lang="EN-US">"/"</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">); </span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span><span> </span><span>&nbsp;</span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span><span> </span><span>&nbsp;</span>System.<em>setProperty</em>(</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(42, 0, 255);" lang="EN-US">"webapp.root"</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"> , webAppRootKey);</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span><span> </span><span>&nbsp;</span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<span>&nbsp;
</span></span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">} <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 宋体;">已经配置完毕</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 宋体;">这样可以在</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;" lang="EN-US">java</span><span style="font-size: 10pt; font-family: 宋体;">普通类或者其他地方可以</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black;" lang="EN-US">System.getProperty("webapp.root"),</span><span style="font-size: 10pt; font-family: 宋体; color: black;">得到项目的根路径</span></p>
<br />
<img src ="http://www.blogjava.net/liudawei/aggbug/283657.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liudawei/" target="_blank">孤飞燕</a> 2009-06-22 23:49 <a href="http://www.blogjava.net/liudawei/articles/283657.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>