つい最近ネットに繋がらない環境でシミュレータっぽいものを作成する機会がありました。
ネットに繋がればSpring等のフレームワークを使いたいところでしたが、不幸にもつながりません。
断絶されています。
仕方がないので普通にノーマルに嫌々やりました。
滅多に素のServletでやらないので一応念のため今後使わないことを祈りつつメモします。
なお、今回は特にJSP等のページの作成は行いません。
単純にアクセスしてHTTPステータスを自由に変更できてとか何らかの文字列を返却するのみのシミュレータでしたので。
- 
実行環境
 
- Windows 10 Pro
 - Eclipse 4.7
 - Java8
 
- 
プロジェクト作成
 
パッケージエクスプローラで右クリック→新規→その他→Web→動的Webプロジェクト
以下のように設定します。

次へ

次へ
web.xmlデプロイメント記述子の生成にチェックして完了

- 
サーブレット作成
 
やりたいことは
- HTTPステータスを好きに設定できる
 - レスポンスの文字列も好きに設定できる
 
です。
ということで以下のクラスです。
doGetで設定し、doPostでその設定を反映したレスポンスを返します。
別に逆でもいいけどやりたいことに合わせてやりましょう。
package sim.servlet;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet
public class Simulator extends HttpServlet implements Servlet {
    private static int httpStatus = 200;
    private static String responseString = "";
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        // URLのパラメータ取得
        String status = request.getParameter("httpstatus");
        String responseStr = request.getParameter("responsestring");
        // 返却値を設定
        httpStatus = Integer.parseInt(status);
        responseString = responseStr;
        try {
            response.getOutputStream().write(
                    new String("Setting : " + httpStatus + ", " + responseString).getBytes("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        // レスポンスの設定
        try {
            response.setStatus(httpStatus);
            response.getOutputStream().write(responseString.getBytes("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
- 
web.xml編集
 
ぱぱっと。<servlet>と<servlet-mapping>を追加します。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>WebApplication</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>sim</servlet-name> <servlet-class>sim.servlet.Simulator</servlet-class> </servlet> <servlet-mapping> <servlet-name>sim</servlet-name> <url-pattern>/httpresponse</url-pattern> </servlet-mapping> </web-app>
- 
いざアクセス
 
こんな感じで動きます。
以下のURLをブラウザで開く(HTTP Get)
http://localhost:8080/WebApplication/httpresponse?httpstatus=200&responsestring=SimTest
doGet()が動いてHTTPステータスとレスポンスを設定
アプリとかで以下にアクセス(HTTP Post)
http://localhost:8080/WebApplication/
doPost()が動いて設定したとおりに返却
  
  
  
  
コメント