wywin00 2013-11-11
Gradle - 简单测试驱动Web程序
# 平台 Ubuntu Linux
# 工程名称(顶级目录名)tdd
# 使用到的jar包
servlet-api.jar
org.springframework.core-3.0.4.RELEASE.jar
commons-logging-1.0.4.jar
spring-mock.jar
一、实现一个基于servlet以纯文本回显请求参数的例子。
1)编写EchoServlet代码
因为需要用到gradle来实现自动化构建,所以遵循gradle中java工程的目录约定。
> mkdir -p src/main/java/com/thoughtworks/servlet
> cd src/main/java/com/thoughtworks/servlet
> touch EchoServlet.java
package com.thoughtworks.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class EchoServlet extends HttpServlet{
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Content-Type", "text/plain"); //设置纯文本响应头
PrintWriter writer = response.getWriter();
Enumeration e = request.getParameterNames(); // 获取请求参数名
while(e.hasMoreElements()) {
String name = String.valueOf(e.nextElement());
String[] values = request.getParameterValues(name);
for(String v : values) {
writer.write(name+"="+v);
writer.write("\n");
}
}
writer.close();
}
}
Gradle 的详细介绍:请点这里
Gradle 的下载地址:请点这里
相关阅读: