解答例 - 実習課題1 - 3.アクション
(実習課題1)
サーブレットの5章実習課題3を改良しなさい。
- 出力ページにおいて、<jsp:useBean>タグを使用するように変更する事。
解答例
/* * Animal.java TECHSCORE Java JSP3実習課題1 * * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter3.exercise1; 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実習課題1 * * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.jsp.chapter3.exercise1; 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/exercise1/output.jsp").forward( request, response); } }
<!-- input.jsp --> <!-- TECHSCORE Java JSP 3章 実習課題1 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" %> <html> <head><title>JSP 3章 実習課題 1</title></head> <body> <h3>JSP 3章 実習課題 1</h3> <form action="/mitoma/jsp3-1"> 名前<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> </body> </html>
<!-- output.jsp --> <!-- TECHSCORE Java JSP 3章 実習課題1 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" import="com.techscore.jsp.chapter3.exercise1.Animal" errorPage="/com/techscore/jsp/chapter3/exercise1/error.jsp" %> <jsp:useBean id="animal" class="com.techscore.jsp.chapter3.exercise1.Animal" scope="request"/> <html> <head><title>JSP 3章 実習課題 1</title></head> <body> <h3>JSP 3章 実習課題 1</h3> 名前:<%= animal.getName() %><br/> 身長:<%= animal.getHeight() %><br/> 体重:<%= animal.getWeight() %><br/> </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-1</servlet-name> <servlet-class>com.techscore.jsp.chapter3.exercise1.ProcessServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>jsp3-1</servlet-name> <url-pattern>/jsp3-1</url-pattern> </servlet-mapping> </web-app>