##はじめに
Webフォームへ値を入力し送信ボタンを押すと、その秒数SLEEPして応答を返すという単純なJavaのサンプルコードです。タイムアウト値をテストする際に使ったものです。
##サーブレット配置
servlet
servlet
+WEB-INF
| +classes
| | +RequestSample1.class
| +web.xml
+formsample.html
##formsample.html
formsample.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" Content="text/html;charset=Shift_JIS">
<title>sleep_sample</title>
</head>
<body>
<p>sleep_sample</p>
<form action="/sample/RequestSample1" method="get">
<table>
<tr>
<td>SLEEP(秒)</td>
<td><input type="text" size="3" value="" name="sleep"></td>
</tr>
</table>
<input type="submit" name="button1" value="送信">
</form>
</body>
</html>
##web.xml
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>RequestSample1</servlet-name>
<servlet-class>RequestSample1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestSample1</servlet-name>
<url-pattern>/RequestSample1</url-pattern>
</servlet-mapping>
</web-app>
##RequestSample1.java
RequestSample1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RequestSample1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
response.setContentType("text/html;charset=Shift_JIS");
PrintWriter out = response.getWriter();
String sleep_sec = request.getParameter("sleep");
try {
Thread.sleep(Long.parseLong(sleep_sec) * 1000);
} catch (InterruptedException e) {
}
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<head>");
sb.append("<title>sleep_sample</title>");
sb.append("</head>");
sb.append("<body>");
sb.append("<p>");
sb.append(sleep_sec);
sb.append("秒SLEEPしました。</p>");
sb.append("</body>");
sb.append("</html>");
out.println(new String(sb));
out.close();
}
}