Java Beansを使ったJava ServletとJSPの連携の手順を備忘録。
仕様説明
- 正しく入力した場合→成功ページへ遷移
- 未入力の場合→エラーページへ遷移
- JSPページにinclude()ディレクティブを使い全ページにヘッダとフッタを付ける。
スポンサードリンク
手順スタート
- デフォルトプロジェクトを右クリック→新規→サーブレットをクリック。
- 新規サーブレット ウインドウが開くと下記の通りに記入および選択をする。
Javaパッケージ | クラス名 | スーパークラス |
---|---|---|
servlet | ProductServlet | javax.servlet.http.HttpServlet |
- 完了ボタンを押下
ProductServlet.javaの編集
package servlet; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import beans.ProductBean; /** * 商品計算サーブレット */ public class ProductServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * コンストラクタ */ public ProductServlet() { super(); } /** * doGet * * @param request * @param response */ protected void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException { // エンコーディング response.setContentType("text/html; charset=Windows-31J"); request.setCharacterEncoding("Windows-31J"); // 商品名 String productName = request.getParameter("productName"); // 単価 String unitPrice = request.getParameter("unitPrice"); // 数量 String quantity = request.getParameter("quantity"); // 次に遷移するページ(初期値) String nextPage = "/product/success.jsp"; // 単価と数量が入力状態かチェック if (unitPrice.length() == 0 || quantity.length() == 0) { nextPage = "/product/error.jsp"; } // 商品Beanの定義 ProductBean bean = new ProductBean(); // 設定 bean.setProductName(this.encode(productName)); bean.setQuantity(quantity); bean.setUnitPrice(unitPrice); // 結果実行 bean.result(); // リクエストに設定 request.setAttribute("product", bean); // 表示用のJSPにフォワード RequestDispatcher rd = request.getRequestDispatcher(nextPage); rd.forward(request, response); } /** * エンコード処理 * * @param value 変換文字 * @return */ private String encode(String value) { try { return new String(value.getBytes("8859_1"),"SJIS"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return value; } }
デフォルトプロジェクトを右クリック→新規→JSPをクリック。
新規 JavaServerPage ウインドウが開くと下記の通りに記入する。
<tr>
<th class=des>ファイル名</th>
<td class=des>hedder.jsp</td>
</tr>
</div>
完了ボタンを押下
hedder.jspの編集
<pre class=source>
<%@ page pageEncoding=Windows-31J %>
<div align=center>
<h1>Java Beans 演習</h1>
</div>
<hr>
</pre>
</div>
デフォルトプロジェクトを右クリック→新規→JSPをクリック。
新規 JavaServerPage ウインドウが開くと下記の通りに記入する。
<tr>
<th class=des>ファイル名</th>
<td class=des>fotter.jsp</td>
</tr>
</div>
完了ボタンを押下
fotter.jspの編集
<pre class=source>
<%@ 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>
<%
String title = 未入力です。;
%>
<html>
<head>
<meta http-equiv=Content-Type content=text/html; charset=windows-31j>
<title><%= title %></title>
</head>
<body>
<%@ include file=/product/hedder.jsp %>
<h2><%= title %></h2>
<a href=/HelloServlet/product/input.jsp>こちら</a>から入力して下さい。
<%@ include file=/product/fotter.jsp %>
</body>
</html>
</pre>
</div>
デフォルトプロジェクトを右クリック→新規→JSPをクリック。
新規 JavaServerPage ウインドウが開くと下記の通りに記入する。
<tr>
<th class=des>ファイル名</th>
<td class=des>input.jsp</td>
</tr>
</div>
完了ボタンを押下
input.jspの編集
<%@ page language="java" contentType="text/html; charset=windows-31j" pageEncoding="windows-31j"%> 商品計算(Java Beans 演習) <%@ include file="/product/hedder.jsp" %> <h2>商品名、単価、数量を記入して下さい。</h2> <form action="/HelloServlet/ProductServlet" method="get">商品:<input name="productName" type="text" /> 単価:<input name="unitPrice" type="text" />円 数量:<input name="quantity" type="text" />個 <input type="submit" value="計算" /> </form><%@ include file="/product/fotter.jsp" %>
デフォルトプロジェクトを右クリック→新規→JSPをクリック。
新規 JavaServerPage ウインドウが開くと下記の通りに記入する。
<tr>
<th class=des>ファイル名</th>
<td class=des>success.jsp</td>
</tr>
</div>
完了ボタンを押下
success.jspの編集
<pre class=source>
<pre>
<%@ 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>
<%@ include file=/product/hedder.jsp %>
<h2>計算結果</h2>
<table border=1>
<tr>
<td>商品</td>
<td>単価</td>
<td>数量</td>
<td>価格</td>
</tr>
<tr>
<td>${product.productName}</td>
<td>@¥${product.unitPrice}</td>
<td>${product.quantity}個</td>
<td>${product.result}円</td>
</tr>
</table>
<%@ include file=/product/fotter.jsp %>
</body>
</html>
</pre>
</div>
デフォルトプロジェクトを右クリック→新規→JSPをクリック。
新規 JavaServerPage ウインドウが開くと下記の通りに記入する。
<tr>
<th class=des>ファイル名</th>
<td class=des>error.jsp</td>
</tr>
</div>
完了ボタンを押下
error.jspの編集
<pre class=source>
<%@ 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>
<%
String title = 未入力です。;
%>
<html>
<head>
<meta http-equiv=Content-Type content=text/html; charset=windows-31j>
<title><%= title %></title>
</head>
<body>
<%@ include file=/product/hedder.jsp %>
<h2><%= title %></h2>
<a href=/HelloServlet/product/input.jsp>こちら</a>から入力して下さい。
<%@ include file=/product/fotter.jsp %>
</body>
</html>
</pre>
</div>
Tomcatを立ち上げ、Webブラウザより<a href=http://localhost:8080/HelloServlet/product/input.jsp>http://localhost:8080/HelloServlet/product/input.jsp</a>にアクセスする。
<img src=
入力欄に商品をオレンジ、単価を110、数量を25個と入力し計算ボタンを押下する。
<img src=
成功画面に遷移し、商品、単価、数量、価格が表示されました。
<img src=
再度 Webブラウザより<a href=http://localhost:8080/HelloServlet/product/input.jsp>http://localhost:8080/HelloServlet/product/input.jsp</a>にアクセスする。
入力欄に商品をオレンジ、単価を未入力、数量を未入力し計算ボタンを押下する。
<img src=
エラー画面に遷移しました。
これで Java BeansとJSPとJavaサーブレットで画面の遷移が出来ました。
<span class=small>※ <a href=http://mergedoc.sourceforge.jp/>MergeDoc Project</a>よりEclipse 3.5 Galileo Pleiades All in Oneを使用しています。
</ol>
最新記事 by hayato (全て見る)
- 🎬「ミッション:インポッシブル/デッドレコニング PART ONE」感想: アクションがバチバチ!🎬今年28作品目 - 2023年8月28日
- 💖ディズニー最新作「マイ・エレメント」: 恋に落ちる2人の魅力的なキャラクター!🎞️今年27作品目 - 2023年8月27日
- 🎬 宮崎駿最新作「君たちはどう生きるか」鑑賞✨ 豪華声優陣‼️😊今年26作品目🎉 - 2023年8月16日
コメントを残す