加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

用 Gretty 编写一个简单的 HTTP 服务器【Groovy版】

发布时间:2020-12-14 17:09:59 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 /** Copyright 2009-2010 MBTE Sweden AB.** Licensed under the Apache License,Version 2.0 (the "License");* you may not use this file except i

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

/*
* Copyright 2009-2010 MBTE Sweden AB.
*
* Licensed under the Apache License,Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,software
* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package examples

import org.mbte.gretty.httpserver.GrettyServer

def server = new GrettyServer().
    localAddress(new InetSocketAddress(8080)).
    dir("web").
    defaultHandler {
        response.redirect "/"
    }.
    "/" {
        get {
            response.html = template("web/templates/main.gpptl",[title:'Hello,World!',message: 'Hello,Dynamic World!'])
        }
    }.
    webContext("/myapp","myapp")

server.start ()

server.doTest("/nosuchurl") { response ->
    assert response.status.code == 301
    assert response.getHeader("Location") == "/"
}

server.doTest("/") { response ->
    assert response.status.code == 200
    def respBody = response.contentText
    assert respBody.contains("<title>Hello,World!</title>")
    assert respBody.contains("Hello,Dynamic World!(from template)<br>")
    assert respBody.contains("<center>HEADER</center>")
    assert respBody.contains("<center>FOOTER</center>")
}

server.stop ()

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读