<目次>
(1) Javaのフォーム認証でオリジナルの(元の)リクエスト情報を取得する方法
(1-1) 構文
(1-2) サンプルプログラム
●JSP(要求ページ)
●JSP(ログインページ)
●カスタム認証サーブレット
(1) Javaのフォーム認証でオリジナルの(元の)リクエスト情報を取得する方法
Javaのフォーム認証において、カスタムのログイン画面を作る際に、要求されたページに遷移させるために「元々要求された画面のURLを知りたい」時に使うメソッドをご紹介します(下図で言う「要求ページ」のURLを取得する方法)。
(図111)

(1-1) 構文
元々の要求URLはリクエストの「javax.servlet.forward.request_uri」という属性にセットされており、HttpServletRequestインターフェイスのgetAttributeメソッド等を利用して値を取得します。
(構文)元々の要求URLの取得方法
[HttpServletRequest型のインスタンス].getAttribute("javax.servlet.forward.request_uri");
(1-2) サンプルプログラム
●JSP(要求ページ)

●JSP(ログインページ)
RequestDispatcher dispatch = request.getRequestDispatcher(forward_success); dispatch.forward(request, response);
//①
request.getAttribute("javax.servlet.forward.request_uri")
//⇒(結果)/LoginTemplate_OrigLimit/form_auth/AfterLogin1.jsp
//②
request.getContextPath()
//⇒(結果)/LoginTemplate_OrigLimit
//①と②の組合せ
request.getAttribute("javax.servlet.forward.request_uri").toString().replace(request.getContextPath(), "")
//⇒(結果)/form_auth/AfterLogin1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>適当なタイトル</title>
</head>
<body>
<form method="POST" action="/CustomAuthentication" name="loginform">
<table border="0">
<tr>
<th align="right">User:</th>
<td><input type="text" name="j_username" /></td>
</tr>
<tr>
<th align="right">Password:</th>
<td><input type="password" name="j_password" /></td>
</tr>
<tr>
<td>
<input type="submit" value="Login" />
<input type="reset" value="Discard" />
</td>
</tr>
</table>
To: <%=request.getAttribute("javax.servlet.forward.request_uri") %><br />
To: <%=request.getContextPath() %><br />
To: <%=request.getAttribute("javax.servlet.forward.request_uri").toString().replace(request.getContextPath(), "") %><br />
<input id="origrequest" type="hidden" name="orig_request" value="<%=request.getAttribute("javax.servlet.forward.request_uri").toString().replace(request.getContextPath(), "") %>"/>
</form>
</body>
</html>
(図132)

●カスタム認証サーブレット
//# 元々の要求画面の情報をLogin1.jspのhiddenフィールドを通じて取得
String forward_success = "."+request.getParameter("orig_request");
(サンプル)カスタム認証サーブレット
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/CustomAuthentication")
public class CustomAuthentication extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//# 遷移先画面
//# 元々の要求画面の情報をLogin1.jspのhiddenフィールドを通じて取得
String forward_success = "."+request.getParameter("orig_request");
//# ログインエラー時の遷移先画面
String forward_error = "/LoginError1.jsp";
//# ログイン画面で入力されたID/パスワードを取得
String user = request.getParameter("j_username");
String passwd = request.getParameter("j_password");
try {
request.login(user, passwd);
//# ログイン成功時の処理
} catch(ServletException e) {
e.printStackTrace();
//# ログイン失敗時の処理
}
}
}
(図133)

(操作動画)
以下の流れを動画にしています。
①「AfterLogin.jsp」を要求 ⇒ ②ログイン画面にて上記URLを取得 ⇒ 認証成功 ⇒ ③要求画面に遷移