解答例 - 実習課題4 - 2.JSPの基本2
(実習課題4)
実習課題2を改良しなさい。
- 「身長」「体重」の入力で、数字以外のものが入力された場合にエラーページが表示されるようにする事。
- エラーページの表示は任意で構わないが、必ず「exception」オブジェクトを使用すること。
解答例
/*
* Animal.java TECHSCORE Java JSP2実習課題4
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.jsp.chapter2.exercise4;
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 JSP2実習課題4
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.jsp.chapter2.exercise4;
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/chapter2/exercise4/output.jsp").forward(
request, response);
}
}
<!-- input.jsp -->
<!-- TECHSCORE Java JSP 2章 実習課題4 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->
<%@page contentType="text/html; charset=Windows-31J" %>
<html>
<head><title>JSP 2章 実習課題 4</title></head>
<body>
<h3>JSP 2章 実習課題 4</h3>
<form action="/mitoma/jsp2-4">
名前<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章 実習課題4 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->
<%@page contentType="text/html; charset=Windows-31J"
import="com.techscore.jsp.chapter2.exercise4.Animal"
errorPage="/com/techscore/jsp/chapter2/exercise4/error.jsp" %>
<%
Animal animal = (Animal) request.getAttribute("animal");
//height又はweightが整数として解釈されない場合に
//NumberFormatExceptionをthrowする
Integer.parseInt((String)animal.getHeight());
Integer.parseInt((String)animal.getWeight());
%>
<html>
<head><title>JSP 2章 実習課題 4</title></head>
<body>
<h3>JSP 2章 実習課題 4</h3>
名前:<%= animal.getName() %><br/>
身長:<%= animal.getHeight() %><br/>
体重:<%= animal.getWeight() %><br/>
</body>
</html>
<!-- error.jsp --> <!-- TECHSCORE Java JSP 2章 実習課題4 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@page contentType="text/html; charset=Windows-31J" isErrorPage="true"%> <html> <head><title>JSP 2章 実習課題 4</title></head> <body> <h3>JSP 2章 実習課題 4</h3> <p><b>例外が発生しました</b> <br>身長と体重は数字で入力してください。</p> <table border="1"><caption>エラーメッセージ</caption><tr><td><%= exception.getMessage() %></td></tr></table> </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-4</servlet-name>
<servlet-class>com.techscore.jsp.chapter2.exercise4.ProcessServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jsp2-4</servlet-name>
<url-pattern>/jsp2-4</url-pattern>
</servlet-mapping>
</web-app>

