<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>windflee</title>
    <description></description>
    <link>http://windflee.javaeye.com</link>
    <language>UTF-8</language>
    <copyright>Copyright 2003-2008, JavaEye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>JavaEye - 做最棒的软件开发交流社区</generator>
      <item>
        <title>gwt 和spring结合的登录实例</title>
        <author>windflee</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://windflee.javaeye.com">windflee</a>&nbsp;
          链接：<a href="http://windflee.javaeye.com/blog/153323" style="color:red;">http://windflee.javaeye.com/blog/153323</a>&nbsp;
          发表时间: 2008年01月05日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>把GWT和SPRING结合使用，其中参考了yongyuan.jiang的例子，也参考网上其他人的例子，代码如下：</p><p>/**<br /><br /> * <br /><br /> */<br /><br />package org.fungchoi.server;<br /><br /><br /><br />import java.io.ByteArrayOutputStream;<br /><br />import java.io.IOException;<br /><br />import java.io.InputStream;<br /><br />import java.net.MalformedURLException;<br /><br />import java.net.URL;<br /><br />import java.text.ParseException;<br /><br />import java.util.HashMap;<br /><br />import java.util.Map;<br /><br />import java.util.zip.GZIPOutputStream;<br /><br /><br /><br />import javax.servlet.ServletConfig;<br /><br />import javax.servlet.ServletContext;<br /><br />import javax.servlet.ServletException;<br /><br />import javax.servlet.http.HttpServletRequest;<br /><br />import javax.servlet.http.HttpServletResponse;<br /><br /><br /><br />import org.springframework.web.context.WebApplicationContext;<br /><br /><br /><br />import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;<br /><br />import com.google.gwt.user.client.rpc.RemoteService;<br /><br />import com.google.gwt.user.client.rpc.SerializationException;<br /><br />import com.google.gwt.user.server.rpc.RPC;<br /><br />import com.google.gwt.user.server.rpc.RPCRequest;<br /><br />import com.google.gwt.user.server.rpc.RemoteServiceServlet;<br /><br />import com.google.gwt.user.server.rpc.SerializationPolicy;<br /><br />import com.google.gwt.user.server.rpc.SerializationPolicyLoader;<br /><br /><br /><br />/**<br /><br /> * @author dolang<br /><br /> *<br /><br /> */<br /><br />public class MyGWTServer extends RemoteServiceServlet {<br /><br />			private  final String ACCEPT_ENCODING = &quot;Accept-Encoding&quot;;<br />	private  final String CHARSET_UTF8 = &quot;UTF-8&quot;;<br />	private  final String CONTENT_ENCODING = &quot;Content-Encoding&quot;;<br />	private  final String CONTENT_ENCODING_GZIP = &quot;gzip&quot;;<br />	private  final String CONTENT_TYPE_TEXT_PLAIN_UTF8 = &quot;text/plain; charset=utf-8&quot;;<br />	private  final String GENERIC_FAILURE_MSG = &quot;The call failed on the server; see server log for details&quot;;<br />		private final ThreadLocal perThreadRequest = new ThreadLocal();<br />	private final ThreadLocal perThreadResponse = new ThreadLocal();<br />	  	private WebApplicationContext springContext; <br />   	   	@Override<br />    public void init(ServletConfig Config) throws ServletException {<br />        super.init(Config);<br />        springContext = (WebApplicationContext) Config<br />        .getServletContext()<br />        .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); <br />                                	           if (springContext == null) { <br />   			throw new RuntimeException( <br />                   &quot;Check Your Web.Xml Setting, No Spring Context Configured&quot;);<br />        }<br />             } <br />   	    	@Override<br />	protected SerializationPolicy doGetSerializationPolicy(HttpServletRequest request, <br />			       String moduleBaseURL, String strongName) {<br />		System.out.print(&quot;begin doGetSerializationPolicy procedure ...\n&quot;);<br />		System.out.print(&quot;moduleBaseURL&quot; + moduleBaseURL + &quot;...\n&quot;);<br />		System.out.print(&quot;strongName&quot; + strongName + &quot;...\n&quot;);<br />		if (request == null) {<br />		   System.out.print(&quot;request is null...&quot;);<br />		} <br />  		return super.doGetSerializationPolicy((HttpServletRequest) perThreadRequest.get(),<br />			moduleBaseURL, strongName);	<br />						   	}<br />	   	@Override <br />  	protected void service(HttpServletRequest req, HttpServletResponse resp) <br />   	            throws ServletException, IOException { <br />   		try {<br />			System.out.print(&quot;Begin service procedure...&quot;);<br />			perThreadRequest.set(req); <br />   	        perThreadResponse.set(resp); <br />   	   /*<br />	        if (req == null){<br />	        		System.out.print(&quot;request is null&quot;);<br />	        }<br />	            //String pathInfo = req.getPathInfo();<br />	        	//System.out.print(&quot;request path is :&quot; + pathInfo + &quot;........&quot;);<br />	            System.out.print(&quot;request URL is :&quot; + req.getRequestURL() + &quot;........&quot;);<br />	    */ <br />       	        int i = req.getRequestURL().indexOf(&quot;/login&quot;);<br />	        String pathInfo = req.getRequestURL().substring(i);<br />	        //System.out.print(&quot; the pathInfo is: &quot; + pathInfo + &quot;...&quot;);<br />	            	        while (pathInfo.startsWith(&quot;/&quot;)) { <br />   	            pathInfo = pathInfo.substring(1); <br />   	        } <br />   	   	        RemoteService service = (RemoteService) springContext.getBean(pathInfo); <br />   	   /*<br />	        if (service == null) {<br />	           	System.out.print(&quot;get service bean fail!.....&quot;);<br />	        } else {<br />	           	System.out.print(&quot;get service bean ok!......\n&quot;);<br />	        }<br />	        */<br />	        String requestPayload = readPayloadAsUtf8(req); <br />   	   	    //    System.out.print(requestPayload + &quot;....\n&quot;);<br />	            // Let subclasses see the serialized request. <br />   	            // <br />   	        onBeforeRequestDeserialized(requestPayload); <br />   	   	            // Invoke the core dispatching logic, which returns the serialized <br />   	            // result. <br />   	            // <br />   	        String responsePayload = processCall(service, requestPayload); <br />   	   	            // Let subclasses see the serialized response. <br />   	            // <br />   	        onAfterResponseSerialized(responsePayload); <br />   	   	            // Write the response. <br />   	            // <br />   	        writeResponse(req, resp, responsePayload); <br />   	    } catch (Throwable e) { <br />   	            // Give a subclass a chance to either handle the exception or <br />   	            // rethrow it <br />   	            // <br />   	            doUnexpectedFailure(e); <br />   	    } finally { <br />   	          //  HttpRequestContext.ThreadLocalHttpRequestContext.remove();<br />			  perThreadRequest.set(null);<br />	          perThreadResponse.set(null);	<br />           	        } <br />   	    } <br />   	   	    /** <br />  	     * rewrite processCall <br />  	     * <br />   	     * @param bean <br />  	     * @param payload <br />  	     * @return <br />  	     * @throws SerializationException <br />  	     */ <br />  	public String processCall(RemoteService bean, String payload) <br />   	            throws SerializationException { <br />   	        try { <br />   	        	System.out.print(&quot;begin processCall procedure ...\n&quot;);<br />	            RPCRequest rpcRequest = RPC.decodeRequest(payload, bean.getClass(), <br />   	                    this); <br />   	            return RPC.invokeAndEncodeResponse(bean, rpcRequest.getMethod(), <br />   	                    rpcRequest.getParameters(), rpcRequest <br />   	                            .getSerializationPolicy()); <br />   	        } catch (IncompatibleRemoteServiceException ex) { <br />   	            getServletContext() <br />   	                    .log(&quot;An IncompatibleRemoteServiceException was thrown while processing this call.&quot;, <br />   	                            ex); <br />   	            return RPC.encodeResponseForFailure(null, ex); <br />   	        } <br />   	 }<br />	    	 private String readPayloadAsUtf8(HttpServletRequest request)<br />	      throws IOException, ServletException {<br />	    int contentLength = request.getContentLength();<br />	    if (contentLength == -1) {<br />	      // Content length must be known.<br />	      throw new ServletException(&quot;Content-Length must be specified&quot;);<br />	    }<br /><br />	    String contentType = request.getContentType();<br />	    boolean contentTypeIsOkay = false;<br />	    // Content-Type must be specified.<br />	    if (contentType != null) {<br />	      // The type must be plain text.<br />	      if (contentType.startsWith(&quot;text/plain&quot;)) {<br />	        // And it must be UTF-8 encoded (or unspecified, in which case we assume<br />	        // that it's either UTF-8 or ASCII).<br />	        if (contentType.indexOf(&quot;charset=&quot;) == -1) {<br />	          contentTypeIsOkay = true;<br />	        } else if (contentType.indexOf(&quot;charset=utf-8&quot;) != -1) {<br />	          contentTypeIsOkay = true;<br />	        }<br />	      }<br />	    }<br />	    if (!contentTypeIsOkay) {<br />	      throw new ServletException(<br />	          &quot;Content-Type must be 'text/plain' with 'charset=utf-8' (or unspecified charset)&quot;);<br />	    }<br />	    InputStream in = request.getInputStream();<br />	    try {<br />	      byte[] payload = new byte[contentLength];<br />	      int offset = 0;<br />	      int len = contentLength;<br />	      int byteCount;<br />	      while (offset &lt; contentLength) {<br />	        byteCount = in.read(payload, offset, len);<br />	        if (byteCount == -1) {<br />	          throw new ServletException(&quot;Client did not send &quot; + contentLength<br />	              + &quot; bytes as expected&quot;);<br />	        }<br />	        offset += byteCount;<br />	        len -= byteCount;<br />	      }<br />	      return new String(payload, &quot;UTF-8&quot;);<br />	    } finally {<br />	      if (in != null) {<br />	        in.close();<br />	      }<br />	    }<br />	  }<br />	 	private void writeResponse(HttpServletRequest request,<br />	    	      HttpServletResponse response, String responsePayload) throws IOException {<br />		byte[] reply = responsePayload.getBytes(CHARSET_UTF8);<br />	    String contentType = CONTENT_TYPE_TEXT_PLAIN_UTF8;<br /><br />	    if (acceptsGzipEncoding(request)<br />	          &amp;&amp; shouldCompressResponse(request, response, responsePayload)) {<br />	    	  // Compress the reply and adjust headers.<br />	    	  //<br />	          ByteArrayOutputStream output = null;<br />	          GZIPOutputStream gzipOutputStream = null;<br />	          Throwable caught = null;<br />	          try {<br />	            output = new ByteArrayOutputStream(reply.length);<br />	            gzipOutputStream = new GZIPOutputStream(output);<br />	            gzipOutputStream.write(reply);<br />	            gzipOutputStream.finish();<br />	            gzipOutputStream.flush();<br />	            response.setHeader(CONTENT_ENCODING, CONTENT_ENCODING_GZIP);<br />	            reply = output.toByteArray();<br />	          } catch (IOException e) {<br />	            caught = e;<br />	          } finally {<br />	            if (null != gzipOutputStream) {<br />	              gzipOutputStream.close();<br />	            }<br />	            if (null != output) {<br />	              output.close();<br />	            }<br />	          }<br /><br />	          if (caught != null) {<br />	            getServletContext().log(&quot;Unable to compress response&quot;, caught);<br />	            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);<br />	            return;<br />	          }<br />	        }<br /><br />	        // Send the reply.<br />	        //<br />	        response.setContentLength(reply.length);<br />	        response.setContentType(contentType);<br />	        response.setStatus(HttpServletResponse.SC_OK);<br />	        response.getOutputStream().write(reply);<br />	}	<br /><br />	private boolean acceptsGzipEncoding(HttpServletRequest request) {<br />	        assert (request != null);<br /><br />        String acceptEncoding = request.getHeader(ACCEPT_ENCODING);<br />        if (null == acceptEncoding) {<br />          return false;<br />        }<br />        return (acceptEncoding.indexOf(CONTENT_ENCODING_GZIP) != -1);<br />    }<br /><br />	    }<br /><br /><br />在初始化时装入spring,在处理请求时调用服务Bean.在客户端调用服务时，与一般的调用没什么区别。</p><p>下面是客户端调用，&nbsp;</p><p><br />	public LoginServiceAsync getLoginService() {<br /><br />		LoginServiceAsync instance = (LoginServiceAsync) GWT<br /><br />		.create(LoginService.class);<br /><br />     /*<br /><br />		if (instance == null) {<br /><br />	         System.out.print(&quot;async instance is null!&quot;);<br /><br />        }<br /><br />		*/<br /><br />        ServiceDefTarget target = (ServiceDefTarget) instance;<br /><br />        System.out.print(GWT.getModuleBaseURL() + LoginService.SERVICE_URI);<br /><br />        target.setServiceEntryPoint(GWT.getModuleBaseURL() + LoginService.SERVICE_URI);<br /><br />        return instance;<br /><br />    }</p><p><br />这个方法得到异步接口，</p><p>				LoginServiceAsync laa = getLoginService();<br /><br />				laa.IsValidateUser(tbUser.getText(), psd.getText(), new AsyncCallback(){<br /><br /><br /><br />					public void onFailure(Throwable arg0) {<br /><br />						// TODO Auto-generated method stub<br /><br />						Window.alert(&quot;Call romote service fail!&quot;);<br /><br />					}<br /><br /><br /><br />					public void onSuccess(Object arg0) {<br /><br />						// TODO Auto-generated method stub<br /><br />						Boolean login = (Boolean) arg0;<br /><br />						if (login.booleanValue()) {<br /><br />							Cookies.setCookie(&quot;loginPMC&quot;, tbUser.getText());<br /><br />							loginPanel.dispose();<br /><br />							createClient();	<br /><br />						} else {<br /><br />							Window.alert(&quot;Login Fail! Username or password is error!&quot;);<br /><br />						}<br /><br />																								}	<br />								});</p><p><br />这样调用就OK，比yongyuan.jiang的例子的客户端调用要简单一点。</p><p>客户端用到了一个第三方包，MyGwt,用它做的客户端要美观一些。可在它的网站上下载：www.mygwt.net</p><p>在Servic类里，调用DAO来检测登录，这里只用了很简单的实现类，也可以把它的实现类用Hibernate来实现。</p><p>附上这个工程的源码。还请大家多多指点。</p>
          <br/>
          <span style="color:red;">
            <a href="http://windflee.javaeye.com/blog/153323#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sat, 05 Jan 2008 12:06:27 +0800</pubDate>
        <link>http://windflee.javaeye.com/blog/153323</link>
        <guid>http://windflee.javaeye.com/blog/153323</guid>
      </item>
      <item>
        <title>GWT的Eclipse开发插件-Cypal Studio for GWT </title>
        <author>windflee</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://windflee.javaeye.com">windflee</a>&nbsp;
          链接：<a href="http://windflee.javaeye.com/blog/137335" style="color:red;">http://windflee.javaeye.com/blog/137335</a>&nbsp;
          发表时间: 2007年11月01日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          用Cypal Studio来开发GWT工程，很方便也很实用。<br />下载地址：http://code.google.com/p/cypal-studio/downloads/list<br />具体的使用方法见：<br />http://www.ibm.com/developerworks/cn/opensource/os-eclipse-ajaxcypal/
          <br/>
          <span style="color:red;">
            <a href="http://windflee.javaeye.com/blog/137335#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Thu, 01 Nov 2007 15:16:37 +0800</pubDate>
        <link>http://windflee.javaeye.com/blog/137335</link>
        <guid>http://windflee.javaeye.com/blog/137335</guid>
      </item>
  </channel>
</rss>