Javascriptでリダイレクト&POST送信を行う調査をした時の備忘録となります。ソースはかなり汚いです。気が向いたら、そのうち整えます。
web.xml
テストソースの構成
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- サーブレットの宣言 -->
<servlet>
<servlet-name>ForwardTestServlet</servlet-name>
<servlet-class>sample.ForwardTestServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>RedirectTestServlet</servlet-name>
<servlet-class>sample.RedirectTestServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>RedirectTestServlet2</servlet-name>
<servlet-class>sample.RedirectTestServlet2</servlet-class>
</servlet>
<!-- サーブレットとurlのマッピング -->
<servlet-mapping>
<servlet-name>ForwardTestServlet</servlet-name>
<url-pattern>/forward</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RedirectTestServlet</servlet-name>
<url-pattern>/redirect</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RedirectTestServlet2</servlet-name>
<url-pattern>/redirect2</url-pattern>
</servlet-mapping>
</web-app>
サーブレット(リダイレクト前)
RedirectTestServlet.java
package sample;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RedirectTestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int x = Integer.parseInt(request.getParameter("x"));
int y = Integer.parseInt(request.getParameter("y"));
int z = x + y;
request.setAttribute("z", z);
HttpSession session = request.getSession(true);
session.setAttribute("aaa", "aaaだよ!");
session.setAttribute("bbb", "bbbだよ!");
getServletContext().getRequestDispatcher("/WEB-INF/jsp/redirect.jsp").forward(request, response);
}
}
RedirectTestServlet2.java
package sample;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RedirectTestServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int x = Integer.parseInt(request.getParameter("x"));
int y = Integer.parseInt(request.getParameter("y"));
int z = x + y;
request.setAttribute("z", z);
HttpSession session = request.getSession(true);
session.setAttribute("aaa", "aaaかもね!");
session.setAttribute("bbb", "bbbかもね!");
getServletContext().getRequestDispatcher("/WEB-INF/jsp/redirect2.jsp").forward(request, response);
}
}
リダイレクト処理(JSP)
redirect.jsp
Formを使ってPOST送信する方式の検証用
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<script>
// window.addEventListener('load', function() {
// window.alert('load!');
// })
window.addEventListener('DOMContentLoaded', function() {
// var memo = document.getElementById('memo');
window.alert('DOMContentLoaded!');
var aa='<%= (String) session.getAttribute("aaa") %>';
var bb='<%= (String) session.getAttribute("bbb") %>';
window.alert('aa='+aa);
window.alert('bb='+bb);
const form = document.createElement('form');
// for Tomcat
form.action = 'http://localhost:8080/ContextRoot/forward';
form.method = 'post';
var x1 = document.createElement("input");
x1.type = 'hidden';
x1.name = 'x';
x1.value = '5';
var x2 = document.createElement("input");
x2.type = 'hidden';
x2.name = 'y';
x2.value = '7';
form.appendChild(x1);
form.appendChild(x2);
document.body.appendChild(form);
form.submit();
})
</script>
</head>
<body>
<b>redirect.jsp</b>
<hr/>
x:<%= request.getParameter("x") %><br>
y:<%= request.getParameter("y") %><br>
z:<%= request.getAttribute("z") %><br>
<hr/>
<a href="<%= request.getContextPath() %>">戻る</a><br/>
</body>
</html>
redirect2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Redirect Sample</title>
<script type="text/javascript">
<%@include file="../js/jquery-3.4.1.min.js" %>
</script>
<script>
window.addEventListener('DOMContentLoaded', function() {
// window.alert('DOMContentLoaded!');
// window.alert('aa:<%= (String) session.getAttribute("aaa") %>');
// window.alert('bb:<%= (String) session.getAttribute("aaa") %>');
// for Tomcat
$.post( 'http://localhost:8080/ContextRoot/forward', 'x=5&y=2' )
.done(function(data) {
window.alert('done!');
window.alert("data="+data);
window.alert("data.form="+data.form);
})
// const form = document.createElement('form');
// form.action = 'http://localhost:8080/FileUpload/forward';
// form.method = 'post';
// var x1 = document.createElement("input");
// x1.type = 'hidden';
// x1.name = 'x';
// x1.value = '3';
// var x2 = document.createElement("input");
// x2.type = 'hidden';
// x2.name = 'y';
// x2.value = '4';
// const x1 = document.createElement("x");
// const text1 = document.createTextNode("3");
// x1.appendChild(text1);
// const x2 = document.createElement("y");
// const text2 = document.createTextNode("6");
// x2.appendChild(text2);
// form.appendChild(x1);
// form.appendChild(x2);
// document.body.appendChild(form);
// form.submit();
})
</script>
</head>
<body>
<b>redirect2.jsp</b>
<hr/>
x:<%= request.getParameter("x") %><br>
y:<%= request.getParameter("y") %><br>
z:<%= request.getAttribute("z") %><br>
<hr/>
<a href="<%= request.getContextPath() %>">戻る</a><br/>
</body>
</html>
サーブレット(リダイレクト先)
ForwardTestServlet.java
package sample;
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 ForwardTestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int x = Integer.parseInt(request.getParameter("x"));
int y = Integer.parseInt(request.getParameter("y"));
int z = x + y;
request.setAttribute("z", z);
getServletContext().getRequestDispatcher("/WEB-INF/jsp/forward.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String xs = request.getParameter("x");
String ys = request.getParameter("y");
int x = Integer.parseInt(xs);
int y = Integer.parseInt(ys);
int z = x + y;
request.setAttribute("z", z);
getServletContext().getRequestDispatcher("/WEB-INF/jsp/forward.jsp").forward(request, response);
}
}
リダイレクト先の画面(JSP)
forward.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<body>
<b>forward.jsp</b>
<hr/>
x:<%= request.getParameter("x") %><br>
y:<%= request.getParameter("y") %><br>
z:<%= request.getAttribute("z") %><br>
<hr/>
<a href="<%= request.getContextPath() %>">戻る</a><br/>
</body>
</html>
テスト用リクエストURL例
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<body>
<table>
<tr>
<th>
テスト分類
</th>
<th>
<b>WebLogic</b>
</th>
<th>
<b>Tomcat</b>
</th>
</tr>
<tr>
<td>
ForwardTest<br/>
</td>
<td>
<a href="http://localhost:7001/ContextRoot/forward?x=1&y=2">Go!</a><br/>
</td>
<td>
<a href="http://localhost:8080/ContextRoot/forward?x=1&y=2">Go!</a><br/>
</td>
</tr>
<tr>
<td>
Redirect</td>
<td>
<a href="http://localhost:7001/ContextRoot/redirect?x=2&y=3">Go!</a><br/>
</td>
<td>
<a href="http://localhost:8080/ContextRoot/redirect?x=2&y=3">Go!</a><br/>
</td>
</tr>
<tr>
<td>
Redirect2</td>
<td>
<a href="http://localhost:7001/ContextRoot/redirect2?x=2&y=3">Go!</a><br/>
</td>
<td>
<a href="http://localhost:8080/ContextRoot/redirect2?x=2&y=3">Go!</a><br/>
</td>
</tr>
</table>
</body>
</html>