解答例 - 実習課題2 - 4.ディレクティブ
(実習課題2)
3章の実習課題3のJSPページを改良しなさい。
- ページの先頭に「ヘッダー」、最後に「フッター」をincludeディレクティブを用いて追加する事。ヘッダー・フッターの内容は任意で良い。
解答例
/* * Animal.java TECHSCORE Java JSP4実習課題2 * * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter4.exercise2; public class Animal { private String name = ""; private String height = ""; private String weight = ""; public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } public String getName() { return name; } public void setName(String name) { if (name != null) { this.name = name; } } public String getWeight() { return weight; } public void setWeight(String weight) { this.weight = weight; } }
/* * ProcessServlet.java TECHSCORE Java JSP4実習課題2 * * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter4.exercise2; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ProcessServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String height = request.getParameter("height"); String weight = request.getParameter("weight"); Animal animal = new Animal(); animal.setName(name); animal.setHeight(height); animal.setWeight(weight); request.setAttribute("animal", animal); request.getRequestDispatcher( "/com/techscore/jsp/chapter4/exercise2/output.jsp").forward( request, response); } }
<!-- input.jsp --> <!-- TECHSCORE Java JSP 4章 実習課題2 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" %> <html> <head><title>JSP 4章 実習課題 2</title></head> <body> <jsp:include page="header.jsp" flush="true" /> <h3>JSP 4章 実習課題 2</h3> <form action="/mitoma/jsp4-2"> 名前<input type="text" name="name"/><br/> 身長<input type="text" name="height"/><br/> 体重<input type="text" name="weight"/><br/> <input type="submit" value="送信" method="get"/><br/> </form> <jsp:include page="count.jsp" flush="true" /> <jsp:include page="footer.jsp" flush="true" /> </body> </html>
<!-- output.jsp --> <!-- TECHSCORE Java JSP 4章 実習課題2 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" import="com.techscore.jsp.chapter4.exercise2.Animal" errorPage="/com/techscore/jsp/chapter4/exercise2/error.jsp" %> <jsp:useBean id="animal" class="com.techscore.jsp.chapter4.exercise2.Animal" scope="request"/> <html> <jsp:include page="header.jsp" flush="true" /> <head><title>JSP 4章 実習課題 2</title></head> <body> <h3>JSP 4章 実習課題 2</h3> 名前:<jsp:getProperty name="animal" property="name"/><br/> 身長:<jsp:getProperty name="animal" property="height"/><br/> 体重:<jsp:getProperty name="animal" property="weight"/><br/> <jsp:include page="count.jsp" flush="true" /> <jsp:include page="footer.jsp" flush="true" /> </body> </html>
<!-- header.jsp --> <!-- TECHSCORE Java JSP 4章 実習課題2 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" %> <h2 align="center">Header部</h2>
<!-- footer.jsp --> <!-- TECHSCORE Java JSP 4章 実習課題2 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" %> <h2 align="center">Footer部</h2>
<!-- count.jsp --> <!-- TECHSCORE Java JSP 4章 実習課題2 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" session="true" %> <h3>カウンター部</h3> <% int i; if(session.getAttribute("count")!=null){ i = ((Integer)session.getAttribute("count")).intValue(); }else{ i = 0; } i++; %> Count: <%= i %> <% session.setAttribute("count", new Integer(i)); %> </body> </html>
▼web.xml
<?xml version="1.0" encoding="Windows-31J"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>jsp4-2</servlet-name> <servlet-class>com.techscore.jsp.chapter4.exercise2.ProcessServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>jsp4-2</servlet-name> <url-pattern>/jsp4-2</url-pattern> </servlet-mapping> </web-app>