【JavaServlet・JSP】新入社員研修向け!問題集
Servlet・JSPをご存じないというあなたは↓をどうぞ!
問題
※JSPでの値の表示方法はEL式などを使用してください
※JSPファイルの格納場所は自由とします
【問題1】おみくじプログラムを作成しましょう
<出力結果>
今日の運勢は小吉
<ヒント>
サーブレット
1.ランダムに値(0~2)を生成する
2.0は大吉、1は中吉、それ以外は小吉
3.リクエストスコープに結果を格納
4.JSPへ転送
JSP
1.サーブレットの結果を受け取る
2.表示
【問題2】名前を入力したら、「こんにちは〇〇さん!」と表示される様にしましょう
<入力画面>


<結果画面>
こんにちはaaaさん
【問題3】入力→確認→完了画面を作成しましょう
※セッション機能を使用しましょう
<入力画面>


<確認画面>


<完了画面>

【問題4】ログイン・ログアウトシステムを作成しましょう
<ログイン画面>
ユーザ名に「user1」、パスワードに「pass1]と入力したら、会員ページに遷移


<会員画面>
ログインに成功した場合は、会員画面に遷移

<エラー画面>
ログインに失敗した場合は、ログイン画面を表示し、ユーザIDのみ表示

<ログアウト画面>
会員ページのログアウトのリンクをクリックしたら、ログアウト画面に遷移

【問題5】ログインに成功したとき、会員ページに「こんにちはuser1さん」と入力したユーザ名が表示されるようにしましょう

【問題6】ファストフードショップのシステムを作成しましょう
注文個数は数量が入力できるようにしてください



以下のクラスを使用してください
public class Item {
private int id;
private String name;
private int price;
private int quantity;
public Item(int id, String name, int price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
解答例
【問題1】おみくじプログラムを作成しましょう
package mondai01;
import java.io.IOException;
import java.util.Random;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/OmikujiServlet")
public class OmikujiServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Random random = new Random();
int randomValue = random.nextInt(3);
String result = "";
if (randomValue == 0) {
result = "大吉";
} else if (randomValue == 1) {
result = "中吉";
} else {
result = "小吉";
}
request.setAttribute("omikuji", result);
RequestDispatcher dispatcher = request.getRequestDispatcher("omikuji.jsp");
dispatcher.forward(request, response);
}
}
src\main\webapp\omikuji.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
今日の運勢は${omikuji}
</body>
</html>
【問題2】名前を入力したら、「こんにちは〇〇さん!」と表示される様にしましょう
package mondai02;
import java.io.IOException;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/InputServlet")
public class InputServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/entry.jsp");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
request.setAttribute("name", name);
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/result.jsp");
dispatcher.forward(request, response);
}
}
src\main\webapp\WEB-INF\jsp\entry.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>名前を入力</h1>
<form action="InputServlet" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
src\main\webapp\WEB-INF\jsp\result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>結果画面</h1>
<p>名前:${name }</p>
</body>
</html>
【問題3】入力→確認→完了画面を作成しましょう
package mondai03;
import java.io.IOException;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/InquiryServlet")
public class InquiryServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/inquiry.jsp");
dispatcher.forward(request, response);
}
}
src\main\webapp\WEB-INF\jsp\inquiry.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>問い合わせ</h1>
<form action="InquiryConfirmServlet" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
package mondai03;
import java.io.IOException;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@WebServlet("/InquiryConfirmServlet")
public class InquiryConfirmServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
HttpSession session = request.getSession(true);
session.setAttribute("name", name);
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/inquiry_confirm.jsp");
dispatcher.forward(request, response);
}
}
src\main\webapp\WEB-INF\jsp\inquiry_confirm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>確認</h1>
<form action="InquiryCompleteServlet" method="post">
名前 :${name} <input type="submit" value="登録">
</form>
</body>
</html>
package mondai03;
import java.io.IOException;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/InquiryCompleteServlet")
public class InquiryCompleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/inquiry_complete.jsp");
dispatcher.forward(request, response);
}
}
src\main\webapp\WEB-INF\jsp\inquiry_complete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>完了</h1>
名前 :${name }
</body>
</html>
【問題4】ログイン・ログアウトシステムを作成しましょう
package mondai04;
import java.io.IOException;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
dispatcher.forward(request, response);
}
}
src\main\webapp\WEB-INF\jsp\login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="MemberPageServlet" method="post">
ユーザ名<input type="text" name="userId" value="${userId}">パスワード <input
type="text" name="password"><br> <input type="submit"
value="送信">
</form>
</body>
</html>
package mondai04;
import java.io.IOException;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@WebServlet("/MemberPageServlet")
public class MemberPageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
String userId = request.getParameter("userId");
String password = request.getParameter("password");
session.setAttribute("userId", userId);
session.setAttribute("password", password);
if (userId.equals("user1") && password.equals("pass1")) {
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/memberPage.jsp");
dispatcher.forward(request, response);
} else {
response.sendRedirect("LoginServlet");
}
}
}
src\main\webapp\WEB-INF\jsp\memberPage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
会員ページ
<a href="LogoutServlet">ログアウト</a>
</body>
</html>
package mondai04;
import java.io.IOException;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);
if (session != null) {
session.invalidate();
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/logout.jsp");
dispatcher.forward(request, response);
}
}
src\main\webapp\WEB-INF\jsp\logout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
ログアウト
</body>
</html>
【問題5】ログインに成功したとき、会員ページに「こんにちはuser1さん」と入力したユーザ名が表示されるようにしましょう
src\main\webapp\WEB-INF\jsp\memberPage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
会員ページ
<p>こんにちは${userId }さん</p>
<a href="LogoutServlet">ログアウト</a>
</body>
</html>
【問題6】ファストフードショップのシステムを作成しましょう
package mondai06;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@WebServlet("/BurgerServlet")
public class BurgerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
//注文画面表示
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
List<Item> itemList = new ArrayList<>();
itemList.add(new Item(1, "バーガー", 250, 0));
itemList.add(new Item(2, "ポテト", 150, 0));
itemList.add(new Item(3, "コーラ", 200, 0));
session.setAttribute("itemList", itemList);
request.getRequestDispatcher("/WEB-INF/jsp/burgershop.jsp").forward(request, response);
}
//注文
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
List<Item> itemList = (List<Item>) session.getAttribute("itemList");
int total=0;
for (int i = 1; i <= itemList.size(); i++) {
int quantity = Integer.parseInt(request.getParameter("quantity" + i));
Item item = itemList.get(i - 1);
item.setQuantity(quantity);
//リストを更新(数量を追加)
itemList.set(i - 1, item);
//小計
total+=item.getQuantity()*item.getPrice();
}
session.setAttribute("itemList", itemList);
request.setAttribute("total", total);
request.getRequestDispatcher("/WEB-INF/jsp/burgershop_result.jsp").forward(request, response);
}
}
src\main\webapp\WEB-INF\jsp\burgershop.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
td input {
width: 50px;
}
th, td {
padding: 5px;
}
</style>
<title>バーガーSHOP</title>
</head>
<body>
<h1>バーガーSHOP</h1>
<form action="BurgerServlet" method="post">
<table>
<th>商品名</th>
<th>注文個数</th>
<th>価格(税込)</th>
<c:forEach var="item" items="${itemList}">
<tr>
<td><c:out value="${item.name}" /></td>
<td><input type="number" name="quantity${item.id}" >個</td>
<td><c:out value="${item.price}"/>円</td>
</tr>
</c:forEach>
</table>
<input type="submit" value="注文">
</form>
</body>
</html>
src\main\webapp\WEB-INF\jsp\burgershop_result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
td input {
width: 50px;
}
</style>
<title>バーガーSHOP</title>
</head>
<body>
<h1>バーガーSHOP</h1>
<p>ご注文内容は以下の通りとなります。</p>
<c:forEach var="item" items="${itemList}">
<p><c:out value="${item.name}" /> × <c:out value="${item.quantity}" />個</p>
</c:forEach>
<p>合計:${total }円</p>
<p></p>
<p></p>
</table>
</body>
</html>
がっつり勉強したいあなたは
↓のリンクから購入されると私にうれしいことが起こります!
Udemyで講座を公開中!
いいなと思ったら応援しよう!
