解答例 - 実習課題3 - 3.アクション
(実習課題3)
実習課題2を改良しなさい。
- 入力ページに2章の実習課題3のカウンタをインクルードする事。
- カウンタページの内容をカスタマイズしても構わない。
解答例
/* * Animal.java TECHSCORE Java JSP3実習課題3 * * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter3.exercise3; 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 JSP3実習課題3 * * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter3.exercise3; 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/chapter3/exercise3/output.jsp").forward( request, response); } }
<!-- input.jsp --> <!-- TECHSCORE Java JSP 3章 実習課題3 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" %> <html> <head><title>JSP 3章 実習課題 3</title></head> <body> <h3>JSP 3章 実習課題 3</h3> <form action="/mitoma/jsp3-3"> 名前<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" /> </body> </html>
<!-- output.jsp --> <!-- TECHSCORE Java JSP 3章 実習課題3 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" import="com.techscore.jsp.chapter3.exercise3.Animal" errorPage="/com/techscore/jsp/chapter3/exercise3/error.jsp" %> <jsp:useBean id="animal" class="com.techscore.jsp.chapter3.exercise3.Animal" scope="request"/> <html> <head><title>JSP 3章 実習課題 3</title></head> <body> <h3>JSP 3章 実習課題 3</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" /> </body> </html>
<!-- count.jsp --> <!-- TECHSCORE Java JSP 3章 実習課題3 --> <!-- 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>jsp3-3</servlet-name> <servlet-class>com.techscore.jsp.chapter3.exercise3.ProcessServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>jsp3-3</servlet-name> <url-pattern>/jsp3-3</url-pattern> </servlet-mapping> </web-app>