import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TaxPriceServlet extends HttpServlet { // methd="get"に対応 @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 返信の設定 response.setContentType("text/html; charset=Windows-31J"); PrintWriter out = response.getWriter(); // 送信値の受取り String price_st = request.getParameter("price"); /* * エラーメッセージ用の 文字列変数を用意 */ String error = null; // データチェック // @無入力か否か if (price_st == null || price_st.length() == 0) { error = "金額入力は必須です。"; // エラー表示 out.print(error); return;// メソッド終了 } // 税込み計算 int price; try { price = Integer.parseInt(price_st); // B正の値か否か if (price < 0) { // 負の場合、実行時例外を投げる throw new RuntimeException(); } // 四捨五入する int taxPrice = (int) Math.round(price * 1.05); out.print("税込み金額:" + taxPrice + "円"); // A整数か否か } catch (NumberFormatException e) { error = "整数以外の文字列が入力されました。"; // エラー表示 out.print(error); } catch (RuntimeException e) { error = "負の値は不正です。"; // エラー表示 out.print(error); } }//doGet() }//class