[JAVA] An app that tests timeouts by entering SLEEP values in a form

Introduction

It is a simple Java sample code that SLEEPs the number of seconds and returns a response when you enter a value in a web form and press the submit button. I used it to test the timeout value.

Servlet placement

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 (seconds)</td>
<td><input type="text" size="3" value="" name="sleep"></td>
</tr>
</table>
<input type="submit" name="button1" value="Send">
</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("SLEEPed for seconds.</p>");
    sb.append("</body>");
    sb.append("</html>");
    out.println(new String(sb));
    out.close();
  }
}

Recommended Posts

An app that tests timeouts by entering SLEEP values in a form
How to deploy an app that references a local jar to heroku
[Rails] A memo that created an advanced search form with ransack