目次へ

解答例 - 実習課題2 - 2.JSPの基本2

(実習課題2)

サーブレット5章の実習課題3を改良しなさい。

  • サーブレットで作成している「入力ページ」「出力ページ」を、JSPページに変更する事。

解答例

/*
 * Animal.java TECHSCORE Java JSP2実習課題2
 * 
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 */
package com.techscore.jsp.chapter2.exercise2;

public class Animal {
    private String name = "";

    private int height = 0;

    private int weight = 0;

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name != null) {
            this.name = name;
        }
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public boolean isValid() {
        if (!name.equals("") && !(weight == 0) && !(height == 0)) {
            return true;
        }
        return false;
    }
}
/*
 * ProcessServlet.java TECHSCORE Java JSP2実習課題2
 * 
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 */
package com.techscore.jsp.chapter2.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);

        try {
            animal.setHeight(Integer.parseInt(height));
            animal.setWeight(Integer.parseInt(weight));
        } catch (NumberFormatException e) {
            request.getRequestDispatcher("/com/techscore/jsp/chapter2/exercise2/input.jsp").forward(request, response);
            return;
        }
        if (animal.isValid()) {
            request.setAttribute("animal", animal);
            request.getRequestDispatcher("/com/techscore/jsp/chapter2/exercise2/output.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("/com/techscore/jsp/chapter2/exercise2/input.jsp").forward(request, response);
        }
    }
}
<!-- input.jsp -->
<!-- TECHSCORE Java JSP 2章 実習課題2 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

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

<%@page contentType="text/html; charset=Windows-31J"
  import="com.techscore.jsp.chapter2.exercise2.Animal" %>
<%
  Animal animal = (Animal) request.getAttribute("animal");
  %>
<html>
  <head><title>JSP 2章 実習課題 2</title></head>
  <body>
    <h3>JSP 2章 実習課題 2</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>jsp2-2</servlet-name>
    <servlet-class>com.techscore.jsp.chapter2.exercise2.ProcessServlet</servlet-class>
  </servlet>

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

</web-app>

↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp