目次へ

解答例 - 実習課題2 - 3.アクション

(実習課題2)

実習課題1を更に改良しなさい。

  • 出力ページにおいて、<jsp:getProperty>を使用するように変更する事。

解答例

/*
 * Animal.java TECHSCORE Java JSP3実習課題2
 * 
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 */
package com.techscore.jsp.chapter3.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 JSP3実習課題2
 * 
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 */
package com.techscore.jsp.chapter3.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/chapter3/exercise2/output.jsp").forward(
                request, response);
    }
}
<!-- input.jsp -->
<!-- TECHSCORE Java JSP 3章 実習課題2 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

<%@page contentType="text/html; charset=Windows-31J" %>
<html>
  <head><title>JSP 3章 実習課題 2</title></head>
<body>
  <h3>JSP 3章 実習課題 2</h3>
  <form action="/mitoma/jsp3-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>
</body>
</html>
<!-- output.jsp -->
<!-- TECHSCORE Java JSP 3章 実習課題2 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

<%@page contentType="text/html; charset=Windows-31J"
  import="com.techscore.jsp.chapter3.exercise2.Animal"
  errorPage="/com/techscore/jsp/chapter3/exercise2/error.jsp" %>
<jsp:useBean id="animal" class="com.techscore.jsp.chapter3.exercise2.Animal" scope="request"/>
  <html>
    <head><title>JSP 3章 実習課題 2</title></head>
    <body>
    <h3>JSP 3章 実習課題 2</h3>
    名前:<jsp:getProperty name="animal" property="name"/><br/>
    身長:<jsp:getProperty name="animal" property="height"/><br/>
    体重:<jsp:getProperty name="animal" property="weight"/><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-2</servlet-name>
    <servlet-class>com.techscore.jsp.chapter3.exercise2.ProcessServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>jsp3-2</servlet-name>
    <url-pattern>/jsp3-2</url-pattern>
  </servlet-mapping>

</web-app>

↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp