Sas*_*gov 21 java web-applications clojure
最近我一直在研究Clojure,我喜欢这种语言.我想看看我是否可以在其中制作一个小型Web应用程序,只是为了挑战自己.但是,我完全没有设置任何与Java相关的Web应用程序的经验.事实上,我根本没有太多Java经验.我从哪里开始?我有很多Apache和LAMP堆栈的经验,我知道在Apache上我会在大多数情况下使用Fast-CGI,但我不知道Java世界中的等价物(如果有的话).
基本上,我只需要帮助设置服务器并启动它.我(有点)了解如何部署纯Java应用程序,但纯Clojure应用程序呢?这是如何运作的?我想,来自一个所有Web应用程序都是用脚本语言编写的世界,这对我来说都是新的.
哦,顺便说一句,我不想使用像Compojure这样的Clojure框架.这将打败这个学习部分.
提前致谢.
pmf*_*pmf 17
我建议你首先学习Servlet-API,它支持Java世界中与HTTP请求和响应相关的所有事情.HttpServletRequest并HttpServletResponse在这里覆盖了很多地方.码头是一个不错的选择; 有关Clojure和Jetty的详细介绍,请访问http://robert.zubek.net/blog/2008/04/26/clojure-web-server/(使用Jetty 6).
话虽这么说,Compojure的基本模型也非常低级:它只是在Clojure数据结构中包含请求和响应,但您仍然负责所有路由,生成正确的响应代码.生成ETag等等,有时候比LAMP堆栈更低级别的东西.
Yog*_*hos 14
一个非常简单的入门方法是创建一个在Tomcat或类似代码上运行的servlet,例如:
(ns servlet
((:gen-class :extends javax.servlet.http.HttpServlet))
(defn -doGet
[_ request response]
(.setContentType response "text/html")
(let w (.getWriter response)]
(.println w
(str "<html>"
"<head>"
"<title>Hello World!</title>"
"</head>"
"<body>"
"<h1>Hello "
(.getParameter request "Name")
"</h1>"
"</body>"
"</html>"))))
(defn -doPost [_ request response]
(-doGet nil request response))
Run Code Online (Sandbox Code Playgroud)
然后在WEB-INF文件夹中创建一个web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Clojure Servlet</display-name>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Run Code Online (Sandbox Code Playgroud)
将其编译并打包成一个战争,它的行为就像一个普通的Java servlet.要在Tomcat上部署,只需将war放入webapps文件夹并启动tomcat.
这里有一个详细的例子http://github.com/yogthos/clojure-maven-examples
| 归档时间: |
|
| 查看次数: |
6993 次 |
| 最近记录: |