サーブレットとセッション①


できるだけ1つのメソッドが1画面で見渡せるようにクラスとメソッドを小分けしました。
(スクロールせずに)

public class SimpleInput extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8756088550041746597L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		try {
			// GETの場合、パラメータを読み込まない
			// レスポンスを渡してHTMLを出力する
			CreateHtml ch = new CreateHtml();
			ch
					.printHtml(
							res,
							"<form method=\"POST\" action=\"http://localhost:8080/SimpleTweet/top/\"  autocomplete=\"off\">"
									+ "ID :<br /><input type=\"text\" name=\"uid\"><br />PW :<br /><input type=\"password\" name=\"pwd\" ><br /><br />つぶやく<br /><textarea name=\"subject\" cols=\"40\" rows=\"4\"></textarea><br />"
									+ "<input type=\"submit\" value=\"確認\">"
									+ "</form>");
		} catch (TwitterException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		try {

			// POSTの場合、リクエストからパラメータを読み込む
			String userId = setInputData(req, "uid");
                        String password = setInputData(req, "pwd");
                        String subject = setInputData(req, "sub");

			// セッション開始
			HttpSession session = req.getSession(true);
			// セッションに保存
			session.setAttribute("uid", userId);
			session.setAttribute("pwd", password);
			session.setAttribute("sub", subject);

			// HTMLを出力する
			CreateHtml ch = new CreateHtml();
			ch
					.printHtml(
							res,
							"この内容で投稿しますか?<br /><br />--------------------<br />ID:"
									+ userId
									+ "<br /><br />"
									+ subject
									+ "<br />-----------------------<br /><form  method=\"POST\" action=\"http://localhost:8080/SimpleTweet/confirm/\" ><input type=\"submit\" value=\"はい\"></form>"
									+ "<form  method=\"GET\" action=\"http://localhost:8080/SimpleTweet/top/\" ><input type=\"submit\" value=\"いいえ\"></form>");
		} catch (TwitterException e) {
			e.printStackTrace();
		}
	}

	// リクエストからパラメータを読み込むメソッド
	public String setInputData(HttpServletRequest req, String paramName) {
		if (req.getParameter(paramName) == null) {
			return "";
		}
	}
} // end of class