SSブログ

Java課題:教科書準拠の演習課題 [勉強]

黒木メイサのダイワウーマンを見ることができて、役所広司以上に大満足な しゃくれアゴです。

さて・・・職業訓練(Webシステムプログラミング入門科)もJDBCに進んだりしてますが、それとは別にコーディングや講師がその場で出す演習問題をクリアしちゃって時間が余った人向けに出されている課題で、いままでやっつけたモノをいくつかUPしますかねぇ。

課題1:
「以前作成したArrayWordServlet.javaを「entry」プロジェクト直下にJSPへ置き換えて表示しなさい」
20101001_Java_Enshu[00].JPG

・表示する内容は、CHAPTER5で作成したArrayWord.java
・表示する文字に日本語が含まれるため、文字化けしないように
・背景色: lightyellow, 見出し: orange(どちらも「カラー名(140色)」)
・表示URLはhttp://localhost:8080/entry/webword.jsp

 

 

で、私の回答ソース(JSP)は、こちら。
(今後、Eclipseでの作成のため、勝手にタグが記述されていたりします)

<%@ page language="java" contentType="text/html; charset=windows-31j"
    pageEncoding="windows-31j"%>
<%@page import="foo.ArrayWord" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Web用語</title>
</head>
<body bgcolor=lightyellow>
<%
response.setContentType("text/html; charset=Windows-31J");
ArrayWord word = new ArrayWord();
out.println("<center><h1><font color=\"orange\">用語</font></h1></center>");
for (int i = 0; i < 3; i++) {
 out.println("<center><p>");
 out.println(word.getOneWord(i));
 out.println("</p></center>");
}
%>
</body>
</html>

こんな感じですねぇ。
課題2:
「以前作成したサーブレットとJSPへのリンクをもったJSPを「entry」プロジェクトの「sub」フォルダ内に作成」
20101001_Java_Enshu[01].JPG

・リンクをクリックすると、それぞれのページが表示されるように
・見出し: green (「カラー名(基本17色)」の色)
・JSPへのリンク方法は、HTMLのURLを記述するときと同じ

 

で・・・私の回答ソースは、こんな感じ。

<%@ page language="java" contentType="text/html; charset=windows-31j"
    pageEncoding="windows-31j"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>CHAPTER9</title>
</head>
<body>
<center><h2><font color=green>Servlet・JSPへのリンク</font></h2></center>
<center><p><a href="/entry/WordServlet">WordServlet</a></center>
<center><p><a href="/entry/ArrayWordServlet">ArrayWordServlet</a></center>
<center><p><a href="/entry/webword.jsp">webword.jsp</a></center>
</body>
</html>

 

こんな感じですかねぇ。
課題3:
「以下のjspを「basic」プロジェクト直下の「input」フォルダに作成」(画面遷移&入力チェック)
20101001_Java_Enshu[02].JPG

・見出し: navy
・inpmonth.jspから値を受け取り、処理を行う「InpMonthServlet.java」を「basic」プロジェクトの「WEB-INF」-「src」内の「input」パッケージに作成
・その際、必ず定数として以下を記述
 final static String[] MONTHDAY = new String[]{ "1月は31日までです。", "2月は28日か29日までです。", "3月は31日までです。", ・・・・・・, "12月は31日までです。"};
・サーブレットでは上記の配列から入力された月に該当するメッセージを表示
20101001_Java_Enshu[03].JPG
・見出し: blue

で・・・私の回答ソースは、こんな感じです。
まず、inpmonth.jspのソース。

<%@ page language="java" contentType="text/html; charset=windows-31j"
    pageEncoding="windows-31j"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>月指定</title>
</head>
<body>
<h2><font color=navy>1~12の数字を入力してください</font></h2>
<form action="/basic/InpMonthServlet">
 <input type="text" name="month">
 <input type="submit" value="送信">
</form>
<%
String message = (String)request.getAttribute("message");
if (message != null) {
 out.println("<p><font color=red>");
 out.println(message + "</font>");
}
%>
</body>
</html>

次にInpMonthServlet.javaのソースです。
package input;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InpMonthServlet extends HttpServlet {

 final static String[] MONTHDAY = new String[] {
  "1月は31日までです。",
  "2月は28日か29日までです。",
  "3月は31日までです。",
  "4月は30日までです。",
  "5月は31日までです。",
  "6月は30日までです。",
  "7月は31日までです。",
  "8月は31日までです。",
  "9月は30日までです。",
  "10月は31日までです。",
  "11月は30日までです。",
  "12月は31日までです。"
 };

 public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

  response.setContentType("text/html; charset=Windows-31J");
  PrintWriter out = response.getWriter();
  String strMonth = request.getParameter("month");
  int month = 0;
  String error = null;

  if (strMonth == null || strMonth.length() ==0) {
   error = "何か入力してください。";
   request.setAttribute("message", error);
  } else {
   try {
    month = Integer.parseInt(strMonth);
   } catch (NumberFormatException e) {
    error = "数値を入力してください。";
    request.setAttribute("message", error);
   }
  }

  if (error != null) {
   RequestDispatcher dispatcher = request.getRequestDispatcher("/input/inpmonth.jsp");
   dispatcher.forward(request, response);
  } else {
   String resultMonth = null;
   String resultMessage = null;
   RequestDispatcher dispatcher = request.getRequestDispatcher("/reqattr/resultmonth.jsp");
   switch (month) {
   case 1:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 2:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 3:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 4:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 5:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 6:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 7:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 8:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 9:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 10:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 11:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   case 12:
    resultMonth = "<h2><font color=blue>" + month + "月</font></h2>";
    resultMessage = MONTHDAY[month - 1];
    request.setAttribute("month", resultMonth);
    request.setAttribute("message", resultMessage);
    dispatcher.forward(request, response);
    break;
   default:
    error = "1~12の範囲で数字を入力してください。";
    request.setAttribute("message", error);
    dispatcher = request.getRequestDispatcher("/input/inpmonth.jsp");
    dispatcher.forward(request, response);
    break;
   }
  }
 }
}

こんな感じになりましたぁ。ちょっとswitch文使うと長くなりますなぁ。決行処理が重複している部分があるから・・・ループ処理させた方がコードがスッキリして見やすくなってたのかな?
とりあえず、私が思いついて、求められた課題を実現できたのが上記ソースでした。(^-^;

あ、上記サーブレットのソースは、次の課題「作成した「InpMonthServlet.java」に入力チェックを実装」もやった結果になっています。
入力チェックの実装って、どこでどう実現させるのか考えたりしますねぇ。入力エラーを全部確認してからエラー内容をすべて入力画面上に表示させるのか、入力項目の上から順にエラー内容をひとつずつ表示させるのか・・・。
まぁ、入力エラーを全部チェックしてからエラー内容を入力画面上に表示させるのって、なんかどこかの会員登録とかの入力画面っぽいですよねぇ。

 

課題4:
「作成したinpmonth.jspをを呼び出す「CallInpMonthServlet.java」を「basic」プロジェクトの「WEB-INF」-「src」内の「dispatch」パッケージに作成」
20101001_Java_Enshu[04].JPG

私の回答ソースは、こんな感じです。(画面遷移の問題ですな) 

package dispatch;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CallInpMonthServlet extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

  RequestDispatcher dispatcher = request.getRequestDispatcher("/input/inpmonth.jsp");
  dispatcher.forward(request, response);

 }

}

・・・短っ!!

ああ・・・次の課題「作成した「InpMonthServlet.java」を画面遷移を行うように変更」も課題3のjspに記述しちゃってましたねぇ。
・新しく「basic」プロジェクトの「reqattr」フォルダ内に「resultmonth.jsp」を作成し、日数の表示をさせるように
・入力エラーの場合は、入力画面(inpmonth.jsp)に遷移し、エラーメッセージを表示

で・・・resultmonth.jspのソースは、こんな感じです。


<%@ page language="java" contentType="text/html; charset=windows-31j"
    pageEncoding="windows-31j"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
</head>
<body>
<%
String month = (String)request.getAttribute("month");
String message = (String)request.getAttribute("message");
out.println(month);
out.println("<br>");
out.println(message);
%>
</body>
</html>

 

・・・titleタグにタイトル名を設定していないあたりが、素晴らしい?ですよね。

と、教科書準拠の演習課題はこんな感じで、やっつけました。
その後・・・さらに問題がどかぁ~んって追加されていたので・・・まだ挑戦中です。

また、いくつか演習課題をやっつけたら、UPしますね。
JavaソースとかJSPソースで長くなっちゃったけど、いつかどこかの誰かに役立つとイイなぁ・・・なんて思ったりしてね。

 

まぁ、課題内容と一緒に(同じ画面内に)回答ソースUPしている時点であまり役立たないかもしれないけど・・・(^-^;

 

 

今日(10/01:都民の日&映画の日)は、6時限目の途中で出されたJDBCの演習課題に挑戦してたら、居残り限界時間(18:00)までかかってしまいました。
SQLをJavaから使うのは、まだ慣れていないなぁ・・・・・・。

 


nice!(0)  コメント(0)  トラックバック(0) 
共通テーマ:資格・学び

nice! 0

コメント 0

コメントを書く

お名前:[必須]
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

Facebook コメント

トラックバック 0

失敗(BlogPet)心が定まらない ブログトップ

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。