サンプル
http://oshiete.goo.ne.jp/qa/1203828.html
にある <body>タグで「onLoad」イベントを利用して確認ダイアログを表示させる。
■ 画面側
* checkboxを付けてボタン押下すれば確認ダイアログが表示する
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<jsp:useBean id="sampleBean" class="com.sample.SampleBean" scope="request" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
<!--
function showConfirmDialog(){
if(confirm("Are you really sure to delete data?")){
window.location='./ok.html';
return;
}
}
//-->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body
<%if(sampleBean.needToShowConfirmDialog()) { %>
onload="showConfirmDialog()"
<%} %>
>
<form action="<%=request.getContextPath() %>/CheckServlet" method="post">
<input type="checkbox" id="ShowDialog" name="ShowDialog" value="true"><br />
<input type="submit" value="Go To Servlet"><br />
</form>
</body>
</html>
ok.html
* 最終移動先
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sample</title>
</head>
<body>
OK!
</body>
</html>
CheckServlet.java
* Servlet
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.sample.SampleBean;
/**
* Servlet implementation class CheckServlet
*/
public class CheckServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CheckServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String showDialog = request.getParameter("ShowDialog");
SampleBean sampleBean = new SampleBean();
if (this.toBoolean(showDialog)) {
sampleBean.setNeedToShowConfirmDialog(true);
} else {
sampleBean.setNeedToShowConfirmDialog(false);
}
request.setAttribute("sampleBean", sampleBean);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("./Views/main.jsp");
requestDispatcher.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
private boolean toBoolean(String value) {
try {
return Boolean.valueOf(value);
} catch (Exception ex) {
}
return false;
}
}
com.sample.SampleBean.java
* Servletと画面側間のデータ
package com.sample;
import java.io.Serializable;
public class SampleBean implements Serializable {
private static final long serialVersionUID = 1L;
private boolean needToShowConfirmDialog;
public SampleBean() {
this.needToShowConfirmDialog = false;
}
public boolean needToShowConfirmDialog() {
return this.needToShowConfirmDialog;
}
public void setNeedToShowConfirmDialog(boolean needToShowConfirmDialog) {
this.needToShowConfirmDialog = needToShowConfirmDialog;
}
}