??xml version="1.0" encoding="utf-8" standalone="yes"?>91嫩草亚洲精品,亚洲福利精品电影在线观看,亚洲av午夜精品一区二区三区http://www.tkk7.com/xixidabao/category/10244.htmlGROW WITH JAVAzh-cnFri, 14 Dec 2007 10:03:51 GMTFri, 14 Dec 2007 10:03:51 GMT60Tomcat+MySql+Struts中文问题l妙的解x?/title><link>http://www.tkk7.com/xixidabao/articles/167382.html</link><dc:creator>JAVA之\</dc:creator><author>JAVA之\</author><pubDate>Thu, 13 Dec 2007 01:16:00 GMT</pubDate><guid>http://www.tkk7.com/xixidabao/articles/167382.html</guid><description><![CDATA[<p>    开发Web应用E序Ӟ无论是用什么样的框架技术来开发,一C数据库存取涉及到中文的数据,p面对中文q或者是各种~码方式不匹配的异常Q今天晚上终于搞定了Tomcat+MySql+Struts的中文问题,用到了很单的Ҏ(gu)Q很快就能搞定?/p> <p>    在做以下工作之前Q?font size="1">所有的HTML/JSP的charset都设为charset=gb2312?/font></p> <p>    W一个要解决的是表单提交q问题。在使用Struts提供的ActionFormq程中,无论表单采用的是Struts标签q是Html标签Q都可以用ActionForm的Get/Set来获取和讄表单的元素|它们的作用效果与request.getParameter()Ҏ(gu)一PQ但提取出来的数据不l过处理的话是qQ主要的原因?.Tomcat的J2EE实现对表单提交即PostҎ(gu)提交Ӟ处理参数采用默认的ISO8859_1来处?.Tomcat对GetҎ(gu)提交的请求在query-string处理旉用了和PostҎ(gu)不一L(fng)处理方式。所以如果要正确地显C和获取中文数据采用的解x案:Q?Q对于PostҎ(gu)提交的表单通过~写一个过滤器QfilerQ的Ҏ(gu)解决Q过滤器在用h交的数据被处理之前被调用Q可以通过q个Java代码改变参数的编码方式(目标~码方式可以通过Web.xml文g里面的参数指定)。过滤器的代码如下:</p> <p>import java.io.IOException;<br /> import javax.servlet.Filter;<br /> import javax.servlet.FilterChain;<br /> import javax.servlet.FilterConfig;<br /> import javax.servlet.ServletException;<br /> import javax.servlet.ServletRequest;<br /> import javax.servlet.ServletResponse;<br /> import javax.servlet.UnavailableException;</p> <p><br /> /**<br />  * <p>Example filter that sets the character encoding to be used in parsing the<br />  * incoming request, either unconditionally or only if the client did not<br />  * specify a character encoding.  Configuration of this filter is based on<br />  * the following initialization parameters:</p><br />  * <ul><br />  * <li><strong>encoding</strong> - The character encoding to be configured<br />  *     for this request, either conditionally or unconditionally based on<br />  *     the <code>ignore</code> initialization parameter.  This parameter<br />  *     is required, so there is no default.</li><br />  * <li><strong>ignore</strong> - If set to "true", any character encoding<br />  *     specified by the client is ignored, and the value returned by the<br />  *     <code>selectEncoding()</code> method is set.  If set to "false,<br />  *     <code>selectEncoding()</code> is called <strong>only</strong> if the<br />  *     client has not already specified an encoding.  By default, this<br />  *     parameter is set to "true".</li><br />  * </ul><br />  *<br />  * <p>Although this filter can be used unchanged, it is also easy to<br />  * subclass it and make the <code>selectEncoding()</code> method more<br />  * intelligent about what encoding to choose, based on characteristics of<br />  * the incoming request (such as the values of the <code>Accept-Language</code><br />  * and <code>User-Agent</code> headers, or a value stashed in the current<br />  * user's session.</p><br />  *<br />  * @author Craig McClanahan<br />  * @version $Revision: 1.2 $ $Date: 2004/03/18 16:40:33 $<br />  */</p> <p>public class SetCharacterEncodingFilter implements Filter {</p> <p><br />     // ----------------------------------------------------- Instance Variables</p> <p><br />     /**<br />      * The default character encoding to set for requests that pass through<br />      * this filter.<br />      */<br />     protected String encoding = null;</p> <p><br />     /**<br />      * The filter configuration object we are associated with.  If this value<br />      * is null, this filter instance is not currently configured.<br />      */<br />     protected FilterConfig filterConfig = null;</p> <p><br />     /**<br />      * Should a character encoding specified by the client be ignored?<br />      */<br />     protected boolean ignore = true;</p> <p><br />     // --------------------------------------------------------- Public Methods</p> <p><br />     /**<br />      * Take this filter out of service.<br />      */<br />     public void destroy() {</p> <p>        this.encoding = null;<br />         this.filterConfig = null;</p> <p>    }</p> <p><br />     /**<br />      * Select and set (if specified) the character encoding to be used to<br />      * interpret request parameters for this request.<br />      *<br />      * @param request The servlet request we are processing<br />      * @param result The servlet response we are creating<br />      * @param chain The filter chain we are processing<br />      *<br />      * @exception IOException if an input/output error occurs<br />      * @exception ServletException if a servlet error occurs<br />      */<br />     public void doFilter(ServletRequest request, ServletResponse response,<br />                          FilterChain chain)<br />  throws IOException, ServletException {</p> <p>        // Conditionally select and set the character encoding to be used<br />         if (ignore || (request.getCharacterEncoding() == null)) {<br />             String encoding = selectEncoding(request);<br />             if (encoding != null)<br />                 request.setCharacterEncoding(encoding);<br />         }</p> <p> // Pass control on to the next filter<br />         chain.doFilter(request, response);</p> <p>    }</p> <p><br />     /**<br />      * Place this filter into service.<br />      *<br />      * @param filterConfig The filter configuration object<br />      */<br />     public void init(FilterConfig filterConfig) throws ServletException {</p> <p> this.filterConfig = filterConfig;<br />         this.encoding = filterConfig.getInitParameter("encoding");<br />         String value = filterConfig.getInitParameter("ignore");<br />         if (value == null)<br />             this.ignore = true;<br />         else if (value.equalsIgnoreCase("true"))<br />             this.ignore = true;<br />         else if (value.equalsIgnoreCase("yes"))<br />             this.ignore = true;<br />         else<br />             this.ignore = false;</p> <p>    }</p> <p><br />     // ------------------------------------------------------ Protected Methods</p> <p><br />     /**<br />      * Select an appropriate character encoding to be used, based on the<br />      * characteristics of the current request and/or filter initialization<br />      * parameters.  If no character encoding should be set, return<br />      * <code>null</code>.<br />      * <p><br />      * The default implementation unconditionally returns the value configured<br />      * by the <strong>encoding</strong> initialization parameter for this<br />      * filter.<br />      *<br />      * @param request The servlet request we are processing<br />      */<br />     protected String selectEncoding(ServletRequest request) {</p> <p>        return (this.encoding);</p> <p>    }</p> <p>}</p> <p>~绎后把class文g攑֜classes目录下,q在Web应用的web.xml文g中添加如下代码:</p> <p><filter><br />   <filter-name>Set Character Encoding</filter-name><br />   <filter-class>com.neusoft.equipment.controller.SetCharacterEncodingFilter</filter-class><br />   <init-param><br />    <param-name>encoding</param-name><br />    <param-value>gbk</param-value><br />   </init-param><br />  </filter><br />  <filter-mapping><br />   <filter-name>Set Character Encoding</filter-name><br />   <url-pattern>/*</url-pattern><br />  </filter-mapping>只要是gb2312,gbk,utf8{支持多字节~码的字W集都可以储存汉字,当然Qgb2312中的汉字数量q少于gbkQ而gb2312,gbk{都可在utf8下编码,q里指定目标~码方式是gbkQ重新启动Tomcat后就可以了?br /> Q?Q对GetҎ(gu)提交的表单,׃参数是紧跟在用户的URLh后面QTomcat对其的处理方法与PostҎ(gu)不一栗所以上面设|的qo器对GetҎ(gu)没有作用Q它需要在其他地方讄。找到Tomca的server.xml配置文gQ找到对80Q或者是8080{别的,q个是自׃改后的)的Connectorlg的设|部分,l这个Connectorlgd一个属性:URIEncodingQ?GBK"。修改后的Connectorlg是这L(fng)Q?/p> <p> <!-- Define a non-SSL HTTP/1.1 Connector on port 80--><br />     <Connector port="80" maxHttpHeaderSize="8192"<br />                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"<br />                enableLookups="false" redirectPort="8443" acceptCount="100"<br />                connectionTimeout="20000" disableUploadTimeout="true"  URIEncodingQ?GBK"/>q样修改后,重启Tomcat可以正处理GETҎ(gu)提交的表单数据了?/p> <p>    W二个要解决的是数据库存取数据出现的q{情c对于不同的数据库往往支持不同的编码,造成了应用时比较混ؕQ不同的数据库的解决Ҏ(gu)往往是不同的Q针对MySqlQ网上也有各U各L(fng)解决Ҏ(gu)Q但个h觉得那些太繁了,现在有一个极其简单的解决办法Q修改MySql的配|文Ӟ打开MySql安装后的根目录,扑ֈmy.init文gQ把[mysqld]区的如下语句Qdefault-character-set=latin1修改为:default-character-set=gbkQ然后在[client]区增加:default-character-set=gbkQ修改后记得做一件事情,到Widows控制面板的管理工具下的服务程序,把Mysql服务停止了重新启动,q样根本解决了MySql的数据库q问题Q很单~~~?/p> <img src ="http://www.tkk7.com/xixidabao/aggbug/167382.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/xixidabao/" target="_blank">JAVA之\</a> 2007-12-13 09:16 <a href="http://www.tkk7.com/xixidabao/articles/167382.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>解决jsp,tomcat,MYSQL下中文ؕ码问?/title><link>http://www.tkk7.com/xixidabao/articles/167375.html</link><dc:creator>JAVA之\</dc:creator><author>JAVA之\</author><pubDate>Thu, 13 Dec 2007 01:02:00 GMT</pubDate><guid>http://www.tkk7.com/xixidabao/articles/167375.html</guid><description><![CDATA[<h1>解决jsp,tomcat,MYSQL下中文ؕ码问?/h1> <div class="664ck6o" id="cczoom"><a ><img alt="查看tomcat服务器标{专? src="http://www.xclass.cn/logo/tomcat.gif" /></a> <p style="font-size: 9pt; margin: 0in; color: black; font-family: Arial">        q些天开发一个项目,服务器是tomcat,操作pȝ是xp,采用的是MVC架构Q模式是采用Facade模式QL出现qQ通过单的讄面字符集,ȝ可以正确昄中文Q可是没惛_表单里提交的数据里的中文q是有ؕ码,我狂晕,没想到JSP里的q问题比ASP里严重多了,自己也解决了好多天,同事也帮忙解冻I也参考了|上众多|友的文章和意见Qȝ是搞定。但是好记性不如烂W杆Q所以特意记下,以防止自己遗忘,同时也给那些遇到同样问题的h提供一个好的参考途径Q?/p> <p style="font-size: 9pt; margin: 0in; color: black; font-family: Arial">        以下内容参考了|上的方?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">(一)    JSP设计面上是中文Q但q行时看到的是ؕ码:</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">解决的办法就是在JSP面的编码的地方<%@ page language="java" contentType="text/html;charset=GBK" %>Q因为Jsp转成Java文g时的~码问题Q默认的话有的服务器是ISO-8859-1Q如果一个JSP中直接输入了中文QJsp把它当作ISO8859-1来处理是肯定有问题的Q这一点,我们可以通过查看Jasper所生成的Java中间文g来确?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">(?    当用Request对象获取客户提交的汉字代码的时候,会出Cؕ码,比如表单里:</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">解决的办法是Q要配置一个filter,也就是一个Servelet的过滤器Q代码如下:</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">import java.io.IOException;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">import javax.servlet.Filter;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">import javax.servlet.FilterChain;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">import javax.servlet.FilterConfig;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">import javax.servlet.ServletException;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">import javax.servlet.ServletRequest;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">import javax.servlet.ServletResponse;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">import javax.servlet.UnavailableException;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">public class SetCharacterEncodingFilter implements Filter {</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">    public void destroy() </p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black"><span style="font-family: Verdana">     </span><span style="font-family: Arial">{</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">    }</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">    public void doFilter(ServletRequest request, ServletResponse response,</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black"><span style="font-family: Arial">    </span><span style="font-family: Verdana">                              </span><span style="font-family: Arial">FilterChain chain)throws IOException, ServletException </span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black"><span style="font-family: Verdana">     </span><span style="font-family: Arial">{</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">    request.setCharacterEncoding("GBK");</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">    // 传递控制到下一个过滤器</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">    chain.doFilter(request, response);</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">    }</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">    public void init(FilterConfig filterConfig) throws ServletException </p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black"><span style="font-family: Verdana">     </span><span style="font-family: Arial">{</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">    }</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">}</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">配置web.xml</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"><filter></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"><filter-name>Set Character Encoding</filter-name></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"><filter-class>com.SetCharacterEncodingFilter</filter-class></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"></filter></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"><filter-mapping></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"><filter-name>Set Character Encoding</filter-name></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"><url-pattern>/*</url-pattern></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"></filter-mapping></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">如果你的q是出现q种情况的话你就往下看看是不是你出CW四中情况,你的Form提交的数据是不是用get提交的,一般来说用post提交的话是没有问题的Q如果是的话Q你q看第四中解决的办法?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">q有是对含有汉字字W的信息q行处理Q处理的代码是:</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">package dbJavaBean;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">public class CodingConvert</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">{   </p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">public CodingConvert()</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">{</p> <p style="margin: 0in 0in 0in 1in; color: black"><span style="font-size: 9.75pt; font-family: Arial">  </span><span style="font-size: 9.75pt; font-family: Verdana">       </span><span style="font-size: 9.75pt; font-family: Arial">//</span><span style="font-size: 9pt; font-family: Verdana">process</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">}</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">public String toGb(String uniStr)</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">{</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     String gbStr = "";</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     if(uniStr == null)</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Verdana">       </span><span style="font-family: Arial">{</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Verdana">          </span><span style="font-family: Arial">   uniStr = "";</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     }</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     try</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Verdana">       </span><span style="font-family: Arial">{</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1.5in; color: black; font-family: Arial">   byte[] tempByte = uniStr.getBytes("ISO8859_1");</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1.5in; color: black; font-family: Arial">   gbStr = new String(tempByte,"GB2312");</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     }</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Arial">  </span><span style="font-family: Verdana">    </span><span style="font-family: Arial">catch(Exception ex)</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Verdana">      </span><span style="font-family: Arial">{</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Verdana">              // exception process</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Arial">    </span><span style="font-family: Arial">}</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     return gbStr;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial"> }</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial"> public String toUni(String gbStr)</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Verdana"> </span><span style="font-family: Arial">{</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     String uniStr = "";</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     if(gbStr == null)</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Verdana">       </span><span style="font-family: Arial">{</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Arial">   </span><span style="font-family: Verdana">         </span><span style="font-family: Arial">gbStr = "";</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     }</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     try</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Verdana">       </span><span style="font-family: Arial">{</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1.5in; color: black; font-family: Arial">   byte[] tempByte = gbStr.getBytes("GB2312");</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1.5in; color: black; font-family: Arial">   uniStr = new String(tempByte,"ISO8859_1");</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">     }</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Verdana">       </span><span style="font-family: Arial">catch(Exception ex)</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Verdana">      </span><span style="font-family: Arial">{</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Arial">    </span><span style="font-family: Arial">}</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black"><span style="font-family: Arial">    </span><span style="font-family: Verdana"> </span><span style="font-family: Arial">return uniStr;</span></p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 1in; color: black; font-family: Arial">}</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">}</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">你也可以在直接的转换Q首先你获取的字符串用ISO-8859-1q行~码Q然后将q个~码存放C个字节数l中Q然后将q个数组转化成字W串对象可以了Q例如:</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">String str=request.getParameter(“girl”);</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">Byte B[]=str.getBytes(“ISO-8859-1”);</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial">Str=new String(B);</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">通过上述转换的话Q提交的M信息都能正确的显C?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">(?    在Formgeth在服务端用request. getParameter(“name”)时返回的是ؕ码;按tomcat的做法设|Filter也没有用或者用request.setCharacterEncoding("GBK");也不用问题是出在处理参C递的Ҏ(gu)上:如果在servlet中用doGet(HttpServletRequest request, HttpServletResponse response)Ҏ(gu)q行处理的话前面即是写了:</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">request.setCharacterEncoding("GBK");</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">response.setContentType("text/html;charset=GBK");</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">也是不v作用的,q回的中文还是ؕ码!Q!如果把这个函数改成doPost(HttpServletRequest request, HttpServletResponse response)一切就O(jin)K了?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">同样Q在用两个JSP面处理表单输入之所以能昄中文是因为用的是postҎ(gu)传递的Q改成getҎ(gu)依旧不行?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">由此可见在servlet中用doGet()Ҏ(gu)或是在JSP中用getҎ(gu)q行处理要注意。这毕竟涉及到要通过览器传递参C息,很有可能引v常用字符集的冲突或是不匹配?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">解决的办法是Q?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">1) 打开tomcat的server.xml文gQ找到区块,加入如下一行: </p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">URIEncoding=”GBK” </p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">完整的应如下Q?nbsp;</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"><Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"> enableLookups="false" redirectPort="8443" acceptCount="100" debug="0"</p> <p style="font-size: 9.75pt; margin: 0in 0in 0in 0.5in; color: black; font-family: Arial"> connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK"/></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">2)重启tomcat,一切OK?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">需要加入的原因大家可以ȝI?nbsp;$TOMCAT_HOME/webapps/tomcat-docs/config/http.html下的q个文g可以知道原因了。需要注意的是:q个地方如果你要是用UTF-8的时候在传递的q程中在Tomcat中也是要出现q的情况,如果不行的话换别的字符集?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">(?    JSP面上有中文Q按钮上面也有中文,但是通过服务器查看页面的时候出Cؕ码:</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">     解决的办法是Q首先在JSP文g中不应该直接包含本地化的消息文本Q而是应该通过<bean:message>标签从Resource Bundle中获得文本。应该把你的中文文本攑ֈApplication.properties文g中,q个文g攑֜WEB-INF/classes/*下,例如我在面里有姓名Q年龄两个label,我首先就是要Z个Application.propertiesQ里面的内容应该是name=”姓名” age=”q龄”,然后我把q个文g攑ֈWEB-INF/classes/properties/下,接下来根据Application.properties文gQ对他进行编码{化,创徏一个中文资源文Ӟ假定名字是Application_cn.properties。在JDK中提供了native2ascii命oQ他能够实现字符~码的{换。在DOS环境中找C攄Application.properties的这个文件的目录Q在DOS环境中执行一下命令,生成按GBK~码的中文资源文件Application_cn.propertiesQnative2ascii ?encoding gbk Application.properties Application_cn.properties执行以上命o以后生成如下内容的Application_cn.properties文gQname="u59d3"u540d age="u5e74"u9f84,在Struts-config.xml中配|:<message-resources parameter="properties.Application_cn"/>。到q一步,基本上完成了一大半Q接着你就要在JSP面上写<%@ page language="java" contentType="text/html;charset=GBK" %>,到名字的那个label是要?lt;bean:message key=”name”>,q样的化在页面上出现的时候就会出C文的姓名Q年龄这个也是一P按钮上汉字的处理也是同样的?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">(?    写入到数据库是ؕ码:</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">解决的方法:要配|一个filter,也就是一个Servelet的过滤器Q代码如同第二种时候一栗?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">如果你是通过JDBC直接链接数据库的时候,配置的代码如下:jdbc:mysql://localhost:3306/workshopdb?useUnicode=true&characterEncoding=GBKQ这样保证到数据库中的代码是不是q?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">如果你是通过数据源链接的化你不能按照q样的写法了Q首先你p写在配置文g中,在tomcat 5.0.19中配|数据源的地Ҏ(gu)?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">C:"Tomcat 5.0"conf"Catalina"localhostq个下面Q我建立的工E是workshopQ放|的目录是webapp下面,workshop.xml的配|文件如下:</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial"><!-- insert this Context element into server.xml --></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial"><Context path="/workshop" docBase="workshop" debug="0"</p> <p style="font-size: 9.75pt; margin: 0in; color: black"><span style="font-family: Verdana">           </span><span style="font-family: Arial">reloadable="true" ></span></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">  <Resource name="jdbc/WorkshopDB"</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">               auth="Container"</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">               type="javax.sql.DataSource" /></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">  <ResourceParams name="jdbc/WorkshopDB"></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    <parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">      <name>factory</name></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    </parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    <parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">      <name>maxActive</name></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">      <value>100</value></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    </parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    <parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">      <name>maxIdle</name></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">      <value>30</value></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    </parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    <parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">      <name>maxWait</name></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">      <value>10000</value></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    </parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">      <parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">     <name>username</name></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">     <value>root</value></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    </parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    <parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">     <name>password</name></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">     <value></value></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    </parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    <!-- Class name for mm.mysql JDBC driver --></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    <parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">       <name>driverClassName</name></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">       <value>com.mysql.jdbc.Driver</value></p> <p style="font-size: 9.75pt; margin: 0in; color: black"><span style="font-family: Verdana">     </span><span style="font-family: Arial"></parameter></span></p> <p style="font-size: 9.75pt; margin: 0in; color: black"><span style="font-family: Arial">   </span><span style="font-family: Arial"><parameter></span></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">      <name>url</name></p> <p style="font-size: 9.75pt; margin: 0in; color: black"><span style="font-family: Verdana">        </span><span style="font-family: Arial"><value></span></p> <p style="font-size: 9.75pt; margin: 0in; color: black"><span style="font-family: Verdana">           </span><span style="font-family: Arial"><![CDATA[jdbc:mysql://localhost:3306/workshopdb?useUnicode=true<span style="color: #ff0000"><font face="Courier New">&amp;</font></span>characterEncoding=GBK]]></span></p> <p style="font-size: 9.75pt; margin: 0in; color: black"><span style="font-family: Verdana">        </span><span style="font-family: Arial"></value></span></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">    </parameter></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">  </ResourceParams></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial"></Context></p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">_体的地方要特别的注意,和JDBC直接链接的时候是有区别的Q如果你是配|正的化,当你输入中文的时候到数据库中是中文了,有一点要注意的是你在昄数据的页面也是要?lt;%@ page language="java" contentType="text/html;charset=GBK" %>q行代码的。需要注意的是有的前台的人员在写代码的是后用Dreamver写的Q写了一个Form的时候把他改成了一个jspQ这h一个地方要注意了,那就是在Dreamver中Action的提交方式是request的,你需要把他该q来Q因为在jsp的提交的q程中紧紧就是POST和GET两种方式Q但是这两种方式提交的代码在~码斚wq是有很大不同的Q这个在后面的地方进行说明?</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">以上是我在开发系l中解决中文的问题,不知道能不能解决大家的问题,旉匆忙Q没有及时完善,文笔也不是很好,有些地方估计是词不达意。大家可以给我意见,希望能共同进步?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">其它上按以上的方法就可以解决的?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">W(二)U方法里Q这个过滤器比较单,如果字符集不同的话,那就要手动修改这个过滤器Q下面介l一个功能强的过滤器Q?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial"><br /> package com.manage.filter;</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">import javax.servlet.*;<br /> import java.io.IOException;</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial"><br /> public class SetCharacterEncodingFilter implements Filter {<br /> protected String encoding = null;<br /> protected FilterConfig filterConfig = null;<br /> protected boolean ignore = true;<br /> public void destroy() {</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">this.encoding = null;<br /> this.filterConfig = null;</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">}</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial"><br /> public void doFilter(ServletRequest request, ServletResponse response,<br /> FilterChain chain)<br /> throws IOException, ServletException {<br /> if (ignore || (request.getCharacterEncoding() == null)) {<br /> String encoding = selectEncoding(request);<br /> if (encoding != null)<br /> request.setCharacterEncoding(encoding);<br /> }<br /> chain.doFilter(request, response);</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">}</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial"><br /> public void init(FilterConfig filterConfig) throws ServletException {</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">this.filterConfig = filterConfig;<br /> this.encoding = filterConfig.getInitParameter("encoding");<br /> String value = filterConfig.getInitParameter("ignore");<br /> if (value == null)<br /> this.ignore = true;<br /> else if (value.equalsIgnoreCase("true"))<br /> this.ignore = true;<br /> else if (value.equalsIgnoreCase("yes"))<br /> this.ignore = true;<br /> else<br /> this.ignore = false;</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">}<br /> protected String selectEncoding(ServletRequest request) {</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">return (this.encoding);</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">}</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">}//EOC</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">/**在web.xml里这栯|?br />   <filter><br />   <filter-name>Set Character Encoding</filter-name><br />   <filter-class><br />    com.manage.filter.SetCharacterEncodingFilter<br />   </filter-class><br />   <init-param><br />    <param-name>encoding</param-name><br />    <param-value>UTF-8</param-value><br />   </init-param><br />   <init-param><br />    <param-name>ignore</param-name><br />    <param-value>true</param-value><br />   </init-param><br />  </filter><br />  <filter-mapping><br />   <filter-name>Set Character Encoding</filter-name><br />   <servlet-name>action</servlet-name><br />  </filter-mapping> <br /> */</p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">针对W(二)U方法,q有一个很单的Ҏ(gu)Q就是在每个面里都加上以下代码Q?lt;%request.setCharacterEncoding("gb2312");%> <br /> <%response.setCharacterEncoding("gb2312");%> <br /> q样听说行,不过我试了没有效?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">        针对MYSQL数据库的中文q问题Q我已经ȝ了一专门的解决Ҏ(gu)Q?/p> <p style="font-size: 9.75pt; margin: 0in; color: black; font-family: Arial">请参?a >http://www.busfly.cn/post/58.html</a> <a >再谈q问题Q如何解决MYSQL数据中文q问题</a></p> </div> <img src ="http://www.tkk7.com/xixidabao/aggbug/167375.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/xixidabao/" target="_blank">JAVA之\</a> 2007-12-13 09:02 <a href="http://www.tkk7.com/xixidabao/articles/167375.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>体验Struts(4)---用ajax实现三下拉列表 http://www.tkk7.com/xixidabao/articles/48315.htmlJAVA之\JAVA之\Fri, 26 May 2006 05:53:00 GMThttp://www.tkk7.com/xixidabao/articles/48315.html接着上次的话题,下面的就是学生注册时需要的学院Q专业,班Q三层列表, 学院Q? 专业Q? 班Q? 学院是上来就应该有的Q我们把他放CLabelValueBean? public Vector getInstitutes() { Connection connection = null; PreparedStatement pstmt = null; ResultSet rs = null; try { connection = getConnection(); pstmt = connection.prepareStatement( "select * from institute" ); rs = pstmt.executeQuery(); Vector institutes = new Vector(); institutes.add( new LabelValueBean( "请选择所在学?, "" ) ); while ( rs.next() ) { institutes.add( new LabelValueBean( rs.getString( "institute" ), rs.getString( "id" ) ) ); } return institutes; } catch ( Exception e ) { e.printStackTrace(); } finally { close( rs ); close( pstmt ); close( connection ); } return null; } 而当它选择了一个学院后Q相应的getDepartments(this.value)的js脚本p工作了,q是四步 var xmlHttp; function createXMLHttpRequest() { if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } 发出h function getDepartments(institute) { createXMLHttpRequest() var url = "ajax.do?institute="+institute+"&method=getDepartments" xmlHttp.open("GET",url, true) xmlHttp.onreadystatechange = departments xmlHttp.send(null) } 处理响应 function departments() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { resText = xmlHttp.responseText each = resText.split("|") buildSelect( each, document.getElementById("departmentId"), "请选择所在专?); } } } function buildSelect(str,sel,label) { sel.options.length=0; sel.options[sel.options.length]=new Option(label,"") for(var i=0;i

JAVA之\ 2006-05-26 13:53 发表评论
]]>
体验Struts(3)---加上点ajax http://www.tkk7.com/xixidabao/articles/48314.htmlJAVA之\JAVA之\Fri, 26 May 2006 05:52:00 GMThttp://www.tkk7.com/xixidabao/articles/48314.html你问我什么叫ajaxQ我也不太了解,我了解的是那支培M无数荷兰球_֍的AjaxQ谁知道怎么有h用几个单词的头字母也能凑个单词来Q不q感觉用它来做东西,应该会挺有意思的 比如当用户在注册的时候,用户点一个按U不用刷新界面就可以获得一句提C,是有qhq是没有qh啊?q次我尝试了用ajax技术来做一个三U关键的下拉列表Q而这是我要讲的关键? 其实现在一般的ajax都是向Servlet发出hQ之后服务器响应Q再h的把l果传给它,之后昄出来Q而换到StrutsQ有Z发甍Q也一PAction是ServletQDispatchAction也是Q只要把代码往q里写,让它往.do那里hp了? 在接下来我就向大家介l我是怎样实现上述功能? 因ؓ大学里面的结构是q里? 学院-专业-班-学生 在学生注册的时候他是依赖于上述对象的,所以用h册就需要一个三U的下拉选择 而ajaxp象变术一P从服务器那里h弄来(zhn)需要的列表 下面我先l大家展CZ下第一个功能是怎么实现的吧Q? 当用户在注册的时候,点一个按U,之后会弹Z个alert来告诉你q个用户是否有h用了Q下面就让我们来看看q个功能是怎么实现的吧Q? q里定义了按U,用来试老师是否已经存在? 大体的ajax的JS代码都上面这四部分, 先是创徏XMLHttpRequestQ? var xmlHttp; function createXMLHttpRequest() { if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } 之后是客户响应部分的代码 function teacherCheck() { var f = document.TeacherRegisterForm 从表单里dD? var user = f.user.value if(user=="") {    window.alert("用户名不能ؓI!") f.user.focus()    return false } else { createXMLHttpRequest() q里都是_֍? var url = "ajax.do?method=checkUserIsExist&user="+user 定义响应地址 xmlHttp.open("GET",url, true) 发出响应 xmlHttp.onreadystatechange = checkUser 把从服务器得到的响应再传l另个函? xmlHttp.send(null) } } function checkUser() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { alert(xmlHttp.responseText) q里是对响应l果的操作,在这里我们是滩出对话框,q把服务器发来的信息昄出来 } } } 我把所有ؕ七八p的操作都放C一个DispatchAction里,所以它也不例外的在q个DA中了 public ActionForward checkUserIsExist( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ) throws Exception { Service service = getService(); res.getWriter().write(service.checkUserIsExistForAjax( req.getParameter( "user" ) ) ); return null; } 它仅仅是把业务逻辑部分的结果发送回去,而真正的判断是在业务逻辑那里实现的, public String checkUserIsExistForAjax( String user )把结果弄成String的Ş式传回去 { Connection connection = null; PreparedStatement pstmt1 = null; ResultSet rs = null; try { connection = getConnection(); pstmt1 = connection .prepareStatement( "select * from user where user=?" ); pstmt1.setString( 1, user ); rs = pstmt1.executeQuery(); rs.last(); if ( rs.getRow() > 0 ) { return ID.M_EXIST; 用户存在 } } catch ( Exception e ) { e.printStackTrace(); } finally { close( rs ); close( pstmt1 ); close( connection ); } return ID.M_NOEXIST;用户不存? }

JAVA之\ 2006-05-26 13:52 发表评论
]]>
体验Struts(2)---整体l构http://www.tkk7.com/xixidabao/articles/48313.htmlJAVA之\JAVA之\Fri, 26 May 2006 05:51:00 GMThttp://www.tkk7.com/xixidabao/articles/48313.htmlZ让大家更方便的了解我q个设计Q我先把我的一些整体的规划都说出来吧,׃我是初学Q难免会参照本书c来看,我买的是那本孙某女的书《精通:*****》,看了看她前面的介l,我一看了不得Q能ZQ写的还都不错,q女的可不得了,渐渐疑惑的地斚w常多Q比如例子里面注释都拽上了英语,搞不懂,而当我从|上下到?sh)子盗版jakarta strutsQ我已安下栽说明要求的那样在24时后删除了Q这本书的时候我才恍然大(zhn),原来是抄袭啊Q至于是谁抄的谁Q口说无凭,不能p谤,不过大家心里都该有杆Uͼ 下面是代码了: package com.boya.subject.model; public interface Person { public Long getId(); public void setId( Long id ); public String getName(); public void setName( String name ); public String getPassword(); public void setPassword( String password ); public String getTelphone(); public void setTelphone( String telphone ); public String getUser(); public void setUser( String user ); public String getType(); } package com.boya.subject.model; public abstract class User implements Person { private Long id;数据库id private String user;用户? private String password;密码 private String name;姓名 private String telphone;?sh)? public Long getId() { return id; } public void setId( Long id ) { this.id = id; } public String getName() { return name; } public void setName( String name ) { this.name = name; } public String getPassword() { return password; } public void setPassword( String password ) { this.password = password; } public String getTelphone() { return telphone; } public void setTelphone( String telphone ) { this.telphone = telphone; } public String getUser() { return user; } public void setUser( String user ) { this.user = user; } } package com.boya.subject.model; public class Admin extends User { private String grade = null; 理员权? public String getGrade() { return grade; } public void setGrade( String grade ) { this.grade = grade; } public String getType() { return "admin"; } } package com.boya.subject.model; public class Teacher extends User { private String level; 教师职称 public String getLevel() { return level; } public void setLevel( String level ) { this.level = level; } public String getType() { return "teacher"; } } package com.boya.subject.model; public class Student extends User { private String sn;学生学号 private SchoolClass schoolClass; 班 public SchoolClass getSchoolClass() { return schoolClass; } public void setSchoolClass( SchoolClass schoolClass ) { this.schoolClass = schoolClass; } public String getSn() { return sn; } public void setSn( String sn ) { this.sn = sn; } public String getType() { return "student"; } } 而对于Action我分别做了一个抽象类Q之后别的从q里l承 先是Action? package com.boya.subject.controller; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.boya.subject.frame.ID; import com.boya.subject.frame.ServiceFactory; import com.boya.subject.model.Person; import com.boya.subject.service.Service; import com.boya.subject.util.HtmlUtil; public abstract class BaseAction extends Action { /** * 由服务工厂方法创建服? * @return 数据库操作的服务 * 2006-5-16 18:10:04 */ public Service getService() { ServiceFactory factory = (ServiceFactory) getAppObject( ID.SF ); Service service = null; try { service = factory.createService(); } catch ( Exception e ) { } return service; } /** * 判断用户是否合法登陆 * @param req * @return 用户是否登陆 * 2006-5-16 18:11:26 */ public boolean isLogin( HttpServletRequest req ) { if ( getPerson( req ) != null ) return true; else return false; } /** * 抽象Ҏ(gu),子类实现 * @param mapping * @param form * @param req * @param res * @return * @throws Exception * 2006-5-16 18:12:54 */ protected abstract ActionForward executeAction( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ) throws Exception; /** * 获取session范围的用? * @param req * @return 当前用户 * 2006-5-16 18:13:35 */ public abstract Person getPerson( HttpServletRequest req ); /** * 父类的执行Action * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ) throws Exception { if ( !isLogin( req ) ) { HtmlUtil.callParentGo( res.getWriter(), ID.M_UNLOGIN, ID.P_INDEX ); return null; } return executeAction( mapping, form, req, res ); } /** * 删除session中属性ؓattribute的对? * @param req * @param attribute 对象属? * 2006-5-16 18:16:59 */ public void removeSessionObject( HttpServletRequest req, String attribute ) { HttpSession session = req.getSession(); session.removeAttribute( attribute ); } /** * 讄session中属性ؓattribute的对? * @param req * @param attribute 讄属? * @param o 讄对象 * 2006-5-16 18:17:50 */ public void setSessionObject( HttpServletRequest req, String attribute, Object o ) { req.getSession().setAttribute( attribute, o ); } /** * 讄a(chn)pplication中属性ؓattribute的对? * @param req * @param attribute 讄属? * @param o 讄对象 * 2006-5-16 18:17:50 */ public void setAppObject( String attribute, Object o ) { servlet.getServletContext().setAttribute( attribute, o ); } public Object getSessionObject( HttpServletRequest req, String attribute ) { Object obj = null; HttpSession session = req.getSession( false ); if ( session != null ) obj = session.getAttribute( attribute ); return obj; } public Object getAppObject( String attribute ) { return servlet.getServletContext().getAttribute( attribute ); } public void callParentGo( HttpServletResponse res, String msg, String url ) throws IOException { HtmlUtil.callParentGo( res.getWriter(), msg, url ); } public void callMeGo( HttpServletResponse res, String msg, String url ) throws IOException { HtmlUtil.callMeGo( res.getWriter(), msg, url ); } public void callBack( HttpServletResponse res, String msg ) throws IOException { HtmlUtil.callBack( res.getWriter(), msg ); } public void callMeConfirm( HttpServletResponse res, String msg, String ok, String no ) throws IOException { HtmlUtil.callMeConfirm( res.getWriter(), msg, ok, no ); } } 再是DispatchAction? package com.boya.subject.controller; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.boya.subject.frame.ID; import com.boya.subject.frame.ServiceFactory; import com.boya.subject.model.Person; import com.boya.subject.service.Service; import com.boya.subject.util.HtmlUtil; public abstract class BaseDispatchAction extends DispatchAction { /** * 由服务工厂方法创建服? * @return 数据库操作的服务 * 2006-5-16 18:10:04 */ public Service getService() { ServiceFactory factory = (ServiceFactory) getAppObject( ID.SF ); Service service = null; try { service = factory.createService(); } catch ( Exception e ) { } return service; } /** * 判断用户是否合法登陆 * @param req * @return 用户是否登陆 * 2006-5-16 18:11:26 */ public boolean isLogin( HttpServletRequest req ) { if ( getPerson( req ) != null ) return true; else return false; } /** * 获取session范围的用? * @param req * @return 当前用户 * 2006-5-16 18:13:35 */ public abstract Person getPerson( HttpServletRequest req ); /** * 父类的执行DispatchAction * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ) throws Exception { try { if ( !isLogin( req ) ) { callParentGo( res, ID.M_UNLOGIN, ID.P_INDEX ); return null; } return super.execute( mapping, form, req, res ); } catch ( NoSuchMethodException e ) { callBack( res, ID.M_NOMETHOD ); return null; } } /** * 删除session中属性ؓattribute的对? * @param req * @param attribute 对象属? * 2006-5-16 18:16:59 */ public void removeSessionObject( HttpServletRequest req, String attribute ) { HttpSession session = req.getSession(); session.removeAttribute( attribute ); } /** * 讄session中属性ؓattribute的对? * @param req * @param attribute 讄属? * @param o 讄对象 * 2006-5-16 18:17:50 */ public void setSessionObject( HttpServletRequest req, String attribute, Object o ) { req.getSession().setAttribute( attribute, o ); } /** * 讄a(chn)pplication中属性ؓattribute的对? * @param req * @param attribute 讄属? * @param o 讄对象 * 2006-5-16 18:17:50 */ public void setAppObject( String attribute, Object o ) { servlet.getServletContext().setAttribute( attribute, o ); } public Object getSessionObject( HttpServletRequest req, String attribute ) { Object obj = null; HttpSession session = req.getSession( false ); if ( session != null ) obj = session.getAttribute( attribute ); return obj; } public Object getAppObject( String attribute ) { return servlet.getServletContext().getAttribute( attribute ); } public void callParentGo( HttpServletResponse res, String msg, String url ) throws IOException { HtmlUtil.callParentGo( res.getWriter(), msg, url ); } public void callMeGo( HttpServletResponse res, String msg, String url ) throws IOException { HtmlUtil.callMeGo( res.getWriter(), msg, url ); } public void callBack( HttpServletResponse res, String msg ) throws IOException { HtmlUtil.callBack( res.getWriter(), msg ); } public void callMeConfirm( HttpServletResponse res, String msg, String ok, String no ) throws IOException { HtmlUtil.callMeConfirm( res.getWriter(), msg, ok, no ); } } 对于E序中的一些提CZ息,我比较喜Ƣ用JS来写Q所以我把这些都攑ֈ了一个类? import java.io.IOException; import java.io.Writer; public class HtmlUtil { public static void callParentGo( Writer out, String msg, String url ) throws IOException { out.write( "" ); } public static void callMeGo( Writer out, String msg, String url ) throws IOException { out.write( "" ); } public static void callMeConfirm( Writer out, String msg ,String ok, String no ) throws IOException { out.write( "" ); } public static void callBack( Writer out, String msg ) throws IOException { out.write( "" ); } }

JAVA之\ 2006-05-26 13:51 发表评论
]]>
体验Struts(1)---用户登陆的实?http://www.tkk7.com/xixidabao/articles/48311.htmlJAVA之\JAVA之\Fri, 26 May 2006 05:50:00 GMThttp://www.tkk7.com/xixidabao/articles/48311.html看到题目Q?zhn)一定觉得很土,Struts早已风靡Q而关于Stuts的文章也早已遍地都是Q如果你觉得土那你就别看了,我只是把我这D|间学到的一些比较肤知识在q里记录一下,如果(zhn)真在这些连载文章中获得了?zhn)惌的知识,那么我就会很ƣ慰了? q不快毕业了吗?我选的题目和Struts有关Q做一个关于学校的毕业设计选题pȝQ就是B/Sl构Q访问数据库的一些俗套的东西Qؓ了m固我q段旉学习StrutsQ我把这个系l竟往N做,q样Ҏ(gu)q个动手能力差的人,实际工作l验的人来_会有点帮助吧Q? 当初是q样想的Q所以就开始了我的Struts之旅? 那我׃我的W一讲起吧Q当然第一一般都是登陆,至于怎么配置StrutsQ?zhn)q是参考一些别人的文章吧,我觉得写q些够土的了,写怎么配置Q怎么实现更土! <%@ page contentType="text/html; charset=gb2312"%> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> q句是生成验证登陆表单所需要的js代码 用户名: 密码Q? 把控制格式的HTML删除掉,应该剩下q些是d了,对于q个毕业设计选题pȝQ有三种角色Q管理员(Admin)Q教?Teacher)Q学?Student)而我把他们的登陆都做C一P在后台这三种角色也是都放在了一个表中,对于他们q三U对象,都是l承于Person的类Q所以在登陆时可以忽视他们的具体角色Q用多态来实现登陆? action="/ajax.do?method=login" Q将一些关于登陆啊Q注册的一些ؕ七八p的操作我都攑ֈ了一个DispatchActionQ之后可以用method的不同来分别调用不同的功能? onsubmit="return validateLoginForm(this)"Q这个是用来实现Struts自带的validate验证 Q是用来昄在登陆时的错误信? 在这里需要的Struts相关配置会有如下的几个方面: 首先是要寚w|文件进行配|我们登陆时需要的FormBean和Action (1)struts-config.xmlQ? 对于登陆p|Q我们准备返回到q里 (2)validation.xml: user ^[0-9a-zA-Z]*$ q里是常量配|,因ؓ我们q会需要到用户名的验证Q所以把他设|ؓ了常? 下面是对q个bean的具体严整手D了Q按字段field分别来写他们所依赖depaends的检验手D,常用的有必须填requiredQ正则表辑ּ验证maskQ最大maxlength和最minlength
从application.properties里读取input.user.mask 从application.properties里读取input.user 以上三部分构成了js的一条错误提C,以下是具体的严整规则? mask ${user} minlength 1 maxlength 16 mask ${password} minlength 1 maxlength 16
对于我们需要的FormBean是这样写的: package com.boya.subject.view; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*; public class LoginForm extends ActionForm { private static final long serialVersionUID = 1L; private String user = null; private String password = null; public String getPassword() { return password; } public void setPassword( String password ) { this.password = password; } public String getUser() { return user; } public void setUser( String user ) { this.user = user; } public void reset(ActionMapping mapping,HttpServletRequest request) { this.password = null;q里很重要,当用戯入有错时Q需要返回登陆界面给用户Qؓ了用户填写方便我们可以设|返回给用户的哪部分信息讄为空 } } 我用来实现登陆的DispatchAction代码如下Q? public ActionForward login( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ) throws Exception { Service service = getService();调用业务逻辑 LoginForm loginForm = (LoginForm) form;获取formbean String user = loginForm.getUser();提取用户? Person person = service.getUser( user );从业务逻辑中查扄? ActionMessages messages = new ActionMessages(); ActionMessage am; if ( person == null )如果用户不存在,我们p? { am = new ActionMessage( "index.jsp.fail.user", user );参数的意义:W一个是MQ而后面的作ؓarg数组 messages.add( "user", am );把错误信息放到errors 属性ؓuser那里LC? saveErrors( req, messages ); form.reset( mapping, req );如果出现错误Q调用formbean的重|功? return mapping.findForward( ID.FAIL ); } if ( !person.getPassword().equals( loginForm.getPassword() ) )如果密码不一? { am = new ActionMessage( "index.jsp.fail.password", user ); messages.add( "password", am ); saveErrors( req, messages ); form.reset( mapping, req ); return mapping.findForward( ID.FAIL ); } setSessionObject( req, person.getType(), person );把用h到session? return new ActionForward( person.getType() + ".do", true );我在每个cd用户的类中加入了一个getType来在q里调用Q之后动态的d应的admin.do,student.do,teacher.do的主面Qƈ且这里实现的不是h转发Q而是h从定? }

JAVA之\ 2006-05-26 13:50 发表评论
]]>
体验Struts(5)---从分体会MVC http://www.tkk7.com/xixidabao/articles/48310.htmlJAVA之\JAVA之\Fri, 26 May 2006 05:48:00 GMThttp://www.tkk7.com/xixidabao/articles/48310.html大家都知道Struts是一U基于MVC的结构,而这个MVC又怎么L(fng)解呢Q书上阐q的一般都很详l,而我的理解很直白Q我们可以把业务逻辑攑ֈ每个JSP面中,当你讉K一个JSP面的时候,可以看C务逻辑得到的结果,而把q些业务逻辑与HTML代码Ҏ(gu)C一P一定会造成一些不必要的麻烦,可以不可以不让我们的业务逻辑和那些HTML代码Ҏ(gu)C起呢Q多得搀杂一些,那干脆,量的吧,于是我们可以试着把业务逻辑的运过E放C个Action里,我们讉Kq个ActionQ之后Action执行业务逻辑Q最后把业务逻辑的结果放到request中,q将面h转发l一个用于显C结果的jsp面Q这Pq个面可以少d多的业务逻辑Q而只是单U的LCZ些业务逻辑计算l果的页面而已。这时的ActionUCؓ控制器,JSP可以叫做视图了Q而控制器操作的业务对象,无非应该叫模型了! 从上面的话,我们来分析一下当我们要做一个分|所需要的部分Q而在q之前,我们先看看他们的执行q程吧,首先我们W一ơ请求访问一个页面,它会把所有记录的前N条显C给我们Q之后计是否有下一,{类似的信息Q当我们点下一늚时候,p取下一늚信息Q我们还可以d一个搜索,比如我们用于昄学生的,可以安学生姓名查找,学号查找Q班U查找。而对于显C的对象Q我们一般也都会装为javabeanQ所以用于放|查询结果的容器是不定的Q而这Ӟ我们需要用泛型来提升我们的代码效率Q? 首先我们写一个用于分|C的javabeanQ? package com.boya.subject.model; import java.util.Vector; public class Page { private int current = 1; //当前? private int total = 0; //总记录数 private int pages = 0; //总页? private int each = 5; //每页昄 private int start = 0; //每页昄的开始记录数 private int end = 0; //每页昄的结束记录数 private boolean next = false; //是否有下一? private boolean previous = false; //是否有上一? private Vector v = null; //存放查询l果的容? public Page( Vector v ,int per) { this.v = v; each = per; total = v.size(); //容器的大就是ȝ记录? if ( total % each == 0 ) pages = total / each; //计算总页? else pages = total / each + 1; if ( current >= pages ) { next = false; } else { next = true; } if ( total < each ) { start = 0; end = total; } else { start = 0; end = each; } } public int getCurrent() { return current; } public void setCurrent( int current ) { this.current = current; } public int getEach() { return each; } public void setEach( int each ) { this.each = each; } public boolean isNext() { return next; } public void setNext( boolean next ) { this.next = next; } public boolean isPrevious() { return previous; } public void setPrevious( boolean previous ) { this.previous = previous; } public int getEnd() { return end; } public int getPages() { return pages; } public int getStart() { return start; } public int getTotal() { return total; } //获取下一늚对象? public Vector getNextPage() { current = current + 1; if ( (current - 1) > 0 ) { previous = true; } else { previous = false; } if ( current >= pages ) { next = false; } else { next = true; } Vector os = gets(); return os; } //获取上一? public Vector getPreviouspage() { current = current - 1; if ( current == 0 ) { current = 1; } if ( current >= pages ) { next = false; } else { next = true; } if ( (current - 1) > 0 ) { previous = true; } else { previous = false; } Vector os = gets(); return os; } //一开始获取的 public Vector gets() { if ( current * each < total ) { end = current * each; start = end - each; } else { end = total; start = each * (pages - 1); } Vector gets = new Vector(); for ( int i = start; i < end; i++ ) { E o = v.get( i ); gets.add( o ); } return gets; } } 而对于按不同搜烦Q我们需要一个FormBeanQ一般的搜烦Q都是模p搜索,搜烦个大概,而且输入的信息中文的比重也会很大Q所以,我把对中文字W的转换攑ֈ了这个BEAN里,在进行select * from * where likeq样的查询时Q如果是like ''q样可以得到所有的记录了,我便用这个来对付没有输入查询关键字的情况Q而like '%*%'可以匚w关键字,?%也在q里d上了Q? package com.boya.subject.view; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; public class SearchForm extends ActionForm { private static final long serialVersionUID = 1L; private String key; private String from; public String getFrom() { return from; } public void setFrom( String from ) { this.from = from; } public void reset( ActionMapping mapping, HttpServletRequest req ) { this.key = null; } public String getKey() { return key; } public void setKey( String key ) { try { key = new String( key.getBytes( "iso-8859-1" ), "gb2312" ); } catch ( UnsupportedEncodingException e ) { e.printStackTrace(); } this.key = "%" + key + "%"; } public String getAny(){ return "%%"; } } 前期都做好了Q我现在p开始访问这个Action了,可是q个控制器还没写呢!q里是代? public class AdminUserAction extends AdminAction { private Vector ss; //用来装结果的容器 private Page ps; //分页昄的PAGE对象 protected ActionForward executeAction( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ) throws Exception { if ( !isSupper( req ) ) { return notSupper( res );//如果不是񔽎理员怎么办? } Service service = getService();//获取业务逻辑 SearchForm sf = (SearchForm) form;//获取搜烦FORM String op = req.getParameter( "op" );//获取用户寚w面的操作 String search = req.getParameter( "search" );//是否执行了搜? Vector temp = null; //用于存放临时反馈l用L(fng)l果容器 if ( op == null )//如果用户没有执行?下一늚操作 { if ( search != null )//用户如果执行了搜? { if ( sf.getFrom().equalsIgnoreCase( "class" ) )//如果是按班查找 { ss = service.getAllStudentBySchoolClassForAdmin( sf .getKey() );//获取from的关键字 } else if ( sf.getFrom().equalsIgnoreCase( "name" ) )//如果是按姓名查找 { ss = service.getAllStudentByNameForAdmin( sf .getKey() ); } else if ( sf.getFrom().equalsIgnoreCase( "user" ) )//如果是按用户名查? { ss = service.getAllStudentByUserForAdmin( sf .getKey() ); } else { ss = service.getAllStudentBySnForAdmin( sf.getKey() );//按学h? } form.reset( mapping, req );//重置搜烦表单 } else { ss = service.getAllStudentForAdmin( sf.getAny() ); //用户未执行查扑ְ昄全部Q? } if ( ss != null && ss.size() != 0 )//如果查找不ؓI,有记录,那就创徏一个分对? { ps = new Page( ss, 10 );//查询结果和每页昄记录C为参数构件对? temp = ps.gets();//q获取第一? } } else//如果用户执行了操? { if ( op.equals( "next" ) )//操作是下一? { temp = ps.getNextPage(); } if ( op.equals( "previous" ) )//操作是上一? { temp = ps.getPreviouspage(); } } req.setAttribute( "search", SelectUtil.studentSearch() );//把搜索用到的表单攑ֈrequest? req.setAttribute( "students", temp );//该页昄的学? req.setAttribute( "page", ps );//分页对象 return mapping.findForward( "student" );//h转发 } } 用到SelectUtil中的代码如下Q? /** * 获取学生查找cd的select * @return 学生查找cd * 2006-5-17 9:06:12 */ public static Vector studentSearch() { Vector s = new Vector(); s.add( new LabelValueBean( "按学h?, "sn" ) ); s.add( new LabelValueBean( "按班U查?, "class" ) ); s.add( new LabelValueBean( "按姓名查?, "name" ) ); s.add( new LabelValueBean( "按用h?, "user" ) ); return s; } 在看面视图前先让我们看看Model吧, public class Student extends User { private String sn; private SchoolClass schoolClass; //q里的班U做Z一U对象,我们在视图显C的时候就有了一层嵌? public SchoolClass getSchoolClass() { return schoolClass; } public void setSchoolClass( SchoolClass schoolClass ) { this.schoolClass = schoolClass; } public String getSn() { return sn; } public void setSn( String sn ) { this.sn = sn; } public String getType() { return "student"; } } 在了解了model后,q是看看视图吧, 先放个查询表单: ׃模型中有嵌套Q那么我们就用到Nested标签Q其实没有嵌套也可以使用q个标签Q下面的是用于显CZ息的Q用q迨器进行遍历request范围的studentsQ你不安排范_他会自动扑ֈ的,q把每次遍历的对象v名叫studentQƈ作ؓ层次的根元素Q? //L了student的schoolClass属性对象的schoolClass嵌套 //student的名? 删除 q里是显C分对象的Q? W?bean:write name="page" property="current" />? ?bean:write name="page" property="pages" />? //上一|否存? 上一?/font>    上一?nbsp;  //下一|否存? 下一?/font>    下一?nbsp;  共有条数? 到这里不知道(zhn)看明白了多,在我的这个JSP里几乎没有M的业务逻辑Q这L(fng)设计比把HTML和JAVA搀杂在一起好了很多?

JAVA之\ 2006-05-26 13:48 发表评论
]]>
实现一?Servlet qo?/title><link>http://www.tkk7.com/xixidabao/articles/41951.html</link><dc:creator>JAVA之\</dc:creator><author>JAVA之\</author><pubDate>Wed, 19 Apr 2006 08:48:00 GMT</pubDate><guid>http://www.tkk7.com/xixidabao/articles/41951.html</guid><description><![CDATA[ <p> <em> <strong> <font color="#000000" size="5">实现一?Servlet qo?/font> </strong> </em> </p> <p> <font color="#000000" size="4"> <em> <strong>1. ~写实现cȝE序</strong> </em> </font> </p> <p> <font color="#000000">qo?API 包含 3 个简单的接口Q又是数?3Q)Q它们整z地嵌套?javax.servlet 包中。那 3 个接口分别是 Filter ?FilterChain ?FilterConfig 。从~程的角度看Q过滤器cd实现 Filter 接口Q然后用这个过滤器cM?FilterChain ?FilterConfig 接口。该qo器类的一个引用将传递给 FilterChain 对象Q以允许qo器把控制权传递给链中的下一个资源?FilterConfig 对象由容器提供l过滤器Q以允许讉K该过滤器的初始化数据?</font> </p> <p> <font color="#000000">Z与我们的三步模式保持一_qo器必运用三个方法,以便完全实现 Filter 接口Q?</font> </p> <p> <font color="#000000">init() Q这个方法在容器实例化过滤器时被调用Q它主要设计用于使过滤器为处理做准备。该Ҏ(gu)接受一?FilterConfig cd的对象作入?</font> </p> <p> <font color="#000000">doFilter() Q与 servlet 拥有一?service() Ҏ(gu)Q这个方法又调用 doPost() 或?doGet() Q来处理h一Pqo器拥有单个用于处理请求和响应的方法―?doFilter() 。这个方法接受三个输入参敎ͼ一?ServletRequest ?response 和一?FilterChain 对象?</font> </p> <p> <font color="#000000">destroy() Q正如?zhn)惛_的那Pq个Ҏ(gu)执行M清理操作Q这些操作可能需要在自动垃圾攉之前q行?<br />清单 1 展示了一个非常简单的qo器,它跟t满一个客h?Web h所q大致旉?/font> </p> <p> <br /> <font color="#000000" size="4"> <em> <strong>清单 1. 一个过滤器cd?/strong> </em> </font> </p> <p> <br /> <font color="#000000">import javax.servlet.*;<br />import java.util.*;<br />import java.io.*;</font> </p> <p> <font color="#000000">public class TimeTrackFilter implements Filter {<br />     private FilterConfig filterConfig = null;</font> </p> <p> <font color="#000000">     public void init(FilterConfig filterConfig)<br />        throws ServletException {</font> </p> <p> <font color="#000000">        this.filterConfig = filterConfig;<br />     }</font> </p> <p> <font color="#000000">     public void destroy() {</font> </p> <p> <font color="#000000">        this.filterConfig = null;<br />     }</font> </p> <p> <font color="#000000">     public void doFilter( ServletRequest request,<br />        ServletResponse response, FilterChain chain )<br />        throws IOException, ServletException {</font> </p> <p> <font color="#000000">        Date startTime, endTime;<br />        double totalTime;</font> </p> <p> <font color="#000000">        startTime = new Date();</font> </p> <p> <font color="#000000">        // Forward the request to the next resource in the chain<br />        chain.doFilter(request, wrapper);</font> </p> <p> <font color="#000000">        // -- Process the response -- \\</font> </p> <p> <font color="#000000">        // Calculate the difference between the start time and end time<br />        endTime = new Date();<br />        totalTime = endTime.getTime() - startTime.getTime();<br />        totalTime = totalTime / 1000; //Convert from milliseconds to seconds</font> </p> <p> <font color="#000000">        StringWriter sw = new StringWriter();<br />        PrintWriter writer = new PrintWriter(sw);</font> </p> <p> <font color="#000000">        writer.println();<br />        writer.println("===============");<br />        writer.println("Total elapsed time is: " + totalTime + " seconds." );<br />        writer.println("===============");</font> </p> <p> <font color="#000000">        // Log the resulting string<br />        writer.flush();<br />        filterConfig.getServletContext().<br />           log(sw.getBuffer().toString());</font> </p> <p> <font color="#000000">     }<br />}</font> </p> <img src ="http://www.tkk7.com/xixidabao/aggbug/41951.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/xixidabao/" target="_blank">JAVA之\</a> 2006-04-19 16:48 <a href="http://www.tkk7.com/xixidabao/articles/41951.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> վ֩ģ壺 <a href="http://vip98888.com" target="_blank">avպavվ</a>| <a href="http://222mp3.com" target="_blank">߲ѲavƬ</a>| <a href="http://jxxitutu.com" target="_blank">AV˵ù</a>| <a href="http://aplus178.com" target="_blank">AVŮһ</a>| <a href="http://szhszszy.com" target="_blank">ƬѹۿĻ</a>| <a href="http://0769wwt.com" target="_blank">»ɫվ</a>| <a href="http://changfafangzhi.com" target="_blank">Ļѹۿ</a>| <a href="http://ywgj50225.com" target="_blank">޹ŮƷþþ</a>| <a href="http://wwwzs88.com" target="_blank">þþƷƷëƬ</a>| <a href="http://www5g9.com" target="_blank">ղĻ</a>| <a href="http://628669.com" target="_blank">޳aһ</a>| <a href="http://hbqueena.com" target="_blank">ŮvƬ</a>| <a href="http://codecampo.com" target="_blank">jjizzȫѿƬ</a>| <a href="http://taixin668.com" target="_blank">ҰһƵ</a>| <a href="http://zbvip888.com" target="_blank">뾫Ʒһ</a>| <a href="http://zjdoukai.com" target="_blank">ۺϾþþƷɫ</a>| <a href="http://gdbobo.com" target="_blank">޾Ʒҹþþ</a>| <a href="http://ynkaishan.com" target="_blank">þþƷձľϣ</a>| <a href="http://bet06966.com" target="_blank">޵һ߹ۿ</a>| <a href="http://wkk3.com" target="_blank">ƵԿ120</a>| <a href="http://dangyuming.com" target="_blank">100ëƬȫ</a>| <a href="http://69xjk.com" target="_blank">Ұ߹ۿƵ</a>| <a href="http://lijieedu.com" target="_blank">͵Ƶ߹ۿ99</a>| <a href="http://www431234.com" target="_blank">˳˳ۺ</a>| <a href="http://740740740.com" target="_blank">Ů˳Ժ˾޸</a>| <a href="http://baoyutv777.com" target="_blank">ۺһ</a>| <a href="http://www026qqcom.com" target="_blank">AVպƷþþþ </a>| <a href="http://wua72.com" target="_blank">һavҩ߳</a>| <a href="http://phlinhng.com" target="_blank">ۺһ</a>| <a href="http://www-741.com" target="_blank">һ</a>| <a href="http://cjfuli.com" target="_blank">þƷ</a>| <a href="http://0917xzb.com" target="_blank">ҹƷ</a>| <a href="http://jte-sh.com" target="_blank">輤һ</a>| <a href="http://8xcb.com" target="_blank">պþ</a>| <a href="http://wx-jn.com" target="_blank">޾ƷƷ벻99</a>| <a href="http://ziniurj.com" target="_blank">3pһ</a>| <a href="http://zmtme.com" target="_blank">av뾫Ʒվ</a>| <a href="http://34007c.com" target="_blank">˶վ</a>| <a href="http://4438xa48.com" target="_blank">޻ɫѵӰ</a>| <a href="http://avqvod.com" target="_blank">ass**ëpics</a>| <a href="http://gengyufood.com" target="_blank">ɫݺվ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>