当社 の新人エンジニア研修向けのJSP Servletの問題集です。
1 ログイン処理①
あなたは、ログイン処理のプロトタイプを作成している。
まずは、ログイン画面から会員専用ページへの画面遷移の部分を作成した。
login.jspで正しくID,パスワードを入力できた時だけmember_only.jspに遷移し、それ以外の場合は、login.jspに戻される処理を書いた。ソースコード③の空欄①~④を埋めなさい。
なお、IDとパスワードの暗号化は考えなくてもよい。
<ソースコード①> login.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ログイン画面</title> <style> #center { height: 200px; width: 300px; position: absolute; left: 50%; top: 50%; margin-top: -100px; margin-left: -150px; text-align: center; } </style> </head> <body> <div id="center"> <form action="Login" method="post"> ユーザーID:<input type="text" name="id" required><br> パスワード:<input type="password" name="pass" required><br> <input type="submit" value="ログイン"><br> </form> </div> </body> </html> |
<ソースコード②> member_only.jsp
1 2 3 4 5 6 7 8 9 10 11 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>メンバーだけの情報です</title> </head> <body> <h1>メンバーだけの情報です</h1> </body> </html> |
<ソースコード③> Login.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet(urlPatterns = {"/Login"}) public class Login extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String pass = request.getParameter("pass"); if (id./*①*/("imai") && pass./*②*/("password")) { /*③*/("member_only.jsp"); } else { /*④*/("login.jsp"); } } } |
2 ログイン処理②
上記のプログラムを先輩に見せたところ、今のままでは、member_only.jspへの直接アクセスを防げないとの指摘を受けた。
セッション管理を使い、ログインしていないユーザーは、login.jspに戻すようにしたい。
以下のソースコードの①~⑤を埋めなさい。
なお、login.jspに変更はない。
<ソースコード④> Login.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet(urlPatterns = {"/Login"}) public class Login extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String pass = request.getParameter("pass"); if (id.equals("imai") && pass.equals("password")) { /*①*/ session = request.getSession(); session./*②*/("id", id); session./*②*/("pass", pass); /*③*/.getRequestDispatcher("member_only.jps").forward(request, response); } else { /*④*/.sendRedirect("login.jsp"); } } } |
<ソースコード⑤> member_only.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>メンバーだけの情報です</title> </head> <body> <% if (/*⑤*/) { request.getRequestDispatcher("login.jsp").forward(request, response); } %> <h1>メンバーだけの情報です</h1> </body> </html> |
3 ログアウト処理
あなたは、次にログアウト処理を付け加えることにした。
ログアウトボタンをユーザーが押下するとlogin.jspに遷移するようにしたい。
1 2 3 4 5 6 7 8 9 10 11 12 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ログイン画面</title> </head> <body> <p>ログアウトしました。</p> </body> </html> |
そのためmember_only.jspに以下のようなフォームを追加した。
1 2 3 |
<form action = "Logout"> <input type ="submit" value ="ログアウト"> </form> |
Logout.javaの以下の①~②を埋めなさい。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet(urlPatterns = {"/Logout"}) public class Logout extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = /*①*/(); session./*②*/(); response.sendRedirect("login.jsp"); } } |
※なお、session.invalidate()はAPIに「このセッションを無効にし、それにバインドされているオブジェク
4 ブラウザバック対策
あなたが、上記システムを先輩に見せたところ、ログアウト処理をした後にブラウザの戻るボタンをクリックすることでmember_only.jspが見えてしまうことを指摘された。
member_only.jspに以下のコードを書き加えることでブラウザバックの対策をしなさい。
1 2 3 |
response.setHeader("Cache-Control", "no-cache,no-store, must-revalidate");//HTTP1.1をサポートしているブラウザ向けの記述 response.setHeader("Pragma", "no-cache");//HTTP1.0をサポートしているブラウザ向けの記述 response.setHeader("Expires", "0");//念のため過去の日時を入力して強制的にキャッシュを無効化する |
5 データベース認証
上記のログインをデータベースにIDとパスワードがあるユーザーだけに限定するようにソースコードを書き換えなさい。