- PR -

サーブレットでの画面遷移について

1
投稿者投稿内容
gold_egg
会議室デビュー日: 2004/06/23
投稿数: 3
投稿日時: 2004-11-17 23:50
情報入力画面(サーブレットA)から登録完了画面(サーブレットB)に遷移するにあたって「処理中」htmlを表示させ、バックグラウンドでDBへの登録プロシージャを実行する(BeanのメソッドをサーブレットBに実装)という動きを作りたいと思っていますが、動作が思うように行かず,皆様からちょっとヒントもしくは知恵をお借りしたく投稿しました。具体的には各々のサーブレットではテンプレート(html)を読込んで、リクエストされてきたパラメータを受け取り、DBから必要なデータをやり取りした結果を置換処理して書き出すようにしております。情報入力画面はサブウィンドウで表示をしており、そこから登録ボタンを押下しそのウインドウに「処理中」のメッセージを表示。セッションの値でプロシージャの処理完了を確認,表示という具合です。今,悩んでいるところはサーブレットBでHttpSession#getAttribute()して得たvalueが"success"であれば「処理中」HTMLへ遷移し,逆に"complete"であればサーブレットBにRequestDispatcher#forward()する部分です。いかにコードを掲載しますので,アドバイスが頂けたら幸いです。また情報不足のところがあればご指摘をお願いします。
<遷移順>
サーブレットA→処理中HTML→サーブレットB

***************サーブレットA(登録情報入力画面)**************************
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class BookUpdateServ extends AuthorServlet {
protected static String[] intParameterNames = new String[] {
UID
};

/** HTML template name */
protected static final String TEMPLATE_NAME = "book_regist.html";

private static BookBean info = new BookBean();
private static PublishBean sinfo = new PublishBean();

protected void concreteResponse(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, java.sql.SQLException
{
checkIntParameter(req, intParameterNames);

int userId = Integer.parseInt(req.getParameter(UID));
int bookId = Integer.parseInt(req.getParameter(BOOK_ID));
int publishId = (req.getParameter(PUBLISH_ID) == null) ? 0 : Integer.parseInt(req.getParameter(PUBLISH_ID));
int topPublishId = (req.getParameter(TOP_PUBLISH_ID) == null) ? 0 : Integer.parseInt(req.getParameter(TOP_PUBLISH_ID));

info.lockTableForBookManagemenet(userId, bookId);

String publishPath = info.getPublishPath(publishId);
String bookPath = info.getBookPath(bookId);

int publishClassLvl = (req.getParameter(PUBLISH_ID) == null) ? 0 :sinfo.checkClassLevelByPublishId(Integer.parseInt(req.getParameter(PUBLISH_ID)));

BufferedReader buf = Common.ReadTemplate(TEMPLATE_NAME);
if(buf == null){
return;
}

//HTMLの表示
PrintWriter out = res.getWriter();
try{
String line = null;
while(true){
line = buf.readLine();
if(line == null){ break; }
//置換タグを置換
if(line.indexOf("<!--%%BOOK_PATH%%-->") != -1){
line = Util.replace(line, "<!--%%GROUP_PATH%%-->", bookPath);
}
if(line.indexOf("<!--%%PUBLISH_PATH%%-->") != -1){
line = Util.replace(line, "<!--%%PUBLISH_PATH%%-->", publishPath);
}
if(line.indexOf("<!--%%UNDER%%-->") != -1){
String under = "<input type=\"checkbox\" name=\"includeUnder\" value=\"1\" checked>" + getResource("tels.Book.undersearch");
line = Util.replace(line, "<!--%%UNDER%%-->", under);
}
if(line.indexOf("<!--%%BUTTON%%-->") != -1){
String button = "<Input type=button value=\"" + getResource("tels.add") + "\" onClick=\"goAhead()\" disabled>&nbsp;&nbsp;<Input type=\"button\" value=\"" + getResource("tels.Book.close") + "\" onClick=\"top.window.close()\">";
line = Util.replace(line, "<!--%%BUTTON%%-->", button);
}
if(line.indexOf("<!--%%UID%%-->") != -1){
line = Util.replace(line, "<!--%%UID%%-->", req.getParameter(UID));
}
out.println(line);
}
}catch(Exception e){
out.clear();
}finally{
buf.close();
out.close();
}
//セッションをセット
HttpSession session = req.getSession(true);
session.setAttribute("update_form", "registing");
}

}

*******************サーブレットB(登録完了画面)**************************
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class BookUpdateServ extends AuthorServlet {
/** HTML template name */
protected static final String TEMPLATE_NAME = "book_regist_comp.html";

protected static BookManagementBean info = new BookManagementBean();
protected static PublishManagementBean sinfo = new PublishManagementBean();

protected void concreteResponse(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, java.sql.SQLException
{
int userId = Integer.parseInt(req.getParameter(UID));
int newBookId = Integer.parseInt(req.getParameter(BOOK_ID));
int publishId = Integer.parseInt(req.getParameter(PUBLISH_ID));
int under = Integer.parseInt(req.getParameter("UNDER"));
int designate = Integer.parseInt(req.getParameter("DESIGNATE"));
int classLevel = (req.getParameter(TOP_PUBLISH_ID) != null) ? Integer.parseInt(req.getParameter(TOP_PUBLISH_ID)) : 0;

//セッションをチェックし,遷移先を指定
HttpSession session = req.getSession(false);
String rtn = (String)session.getAttribute("update_form");
if(rtn.equals("registing")){
res.sendRedirect(res.encodeURL("../html/book_regist_msg.html"));
}
if(rtn.equals("complete")){
req.getRequestDispatcher("/servlet/BookSecitonUpdateServ").forward(req, res);
}
//プロシージャ処理
if(req.getParameter(TOP_PUBLISH_ID) == null)
topPublishId = sinfo.getTopPublishIdByUserId(userId, true);

if(under == 1 || under < 2){
info.chkBookClassLevel(publishId, targetBookId, under);
info.updateCopyPublishAndBelogAllUser(publishId, targetBookId, userId, under);
}
}else {
throw new AuthorException(17);
}
//DB関連の処理が終わったのでここでセッションをset
session.setAttribute("update_form", "complete");

//テンプレートの読み込み
BufferedReader buf = Common.ReadTemplate(TEMPLATE_NAME);
if(buf == null){
return;
}

//HTMLの表示
PrintWriter out = res.getWriter();
try{
//ストリームの作成
String line = null;
while(true){
line = buf.readLine();
if(line == null) { break; }
if(line.indexOf("<!--%%BOOKID%%-->") != -1){
line = Util.replace(line, "<!--%%BOOKID%%-->", "" + newBookId);
}
if(line.indexOf("<!--%%CLASSLVL%%-->") != -1){
line = Util.replace(line, "<!--%%CLASSLVL%%-->", ""+ classLevel);
}
out.println(line);
}
}catch(Exception e){
out.clear();
Common.OutputLog(e.toString());
}finally{
out.flush();
buf.close();
out.close();
}
}

}
***************「処理中」HTML **************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Refresh" content="3">
<title>登録中</title>
</head>

<body bgcolor="#ffffff" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">
<p style="font-size: 180%; text-align: center;">現在、登録中です。</p>
<p style="font-size: 120%; text-align: center;">(処理が終わるまでしばらくお待ちください)</p>

</body>
</html>
uk
ぬし
会議室デビュー日: 2003/05/20
投稿数: 1155
お住まい・勤務地: 東京都
投稿日時: 2004-11-18 00:59
具体的にどう「うまくいかない」のか書いてないですが、おそらく処理中画面から次の画面に
切り替わらないのだと思います。リダイレクトで「処理中」HTMLを表示し、その中でリフレッシュ
しても、「処理中」HTMLをリロードするだけですので当然でしょう。METAタグのCONTENT属性
にBookUpdateServサーブレットに遷移するパスを設定すればよいと思いますが。
かつのり
ぬし
会議室デビュー日: 2004/03/18
投稿数: 2015
お住まい・勤務地: 札幌
投稿日時: 2004-11-18 01:17
サーバ側とクライアント側の処理の連携を
イベントドリブンで行うのはHTMLでは難しいです。

今開発中のシステムでも似たような仕組みを行っているのですが、
フレーム(メイン画面フレームとサイズ0の隠れフレーム)や
DHTMLを駆使して作ってます。

例えば、メイン画面フレームで登録操作を行うと、
隠れフレームにサブミットして、メイン画面フレームは処理中に切り替えます。
隠れフレームでサーバ処理をして、処理が完了したら
ウィンドウロードイベントで、メイン画面フレームを処理中から入力待ちに切り替えます。

こんな感じで画面遷移すると、操作する側としては比較的スムーズに感じられます。
ちょっと工夫すると、gold_eggさんのやりたいことができるのではないでしょうか。
gold_egg
会議室デビュー日: 2004/06/23
投稿数: 3
投稿日時: 2004-11-18 14:07
ukさん、かつのりさんレスありがとうございます。
ukさん、自分の勉強不足、説明不足で申し訳ありません。確かに言われる通りCONTENTの属性にBookUpdateのパスを記述する方法も試してみたのですが、別の障害が出てしまいました。
そこで再度質問させてください。
悩んでいるのは次の2点です。
1)サーブレットBでDBからプロシージャを処理をしながら「処理中」HTMLを表示させる場合のHTMLへの遷移処理。
2)プロシージャ処理の完了とともにsessionをセットして完了画面を表示させる場合のサーブレットBへの遷移処理。

strutsを使用してした場合には,struts-config.xmlのaction-mappingsではforwardにnameとpathを記述するだけでしていたサンプルをみたことがあってそれをサーブレットでできないものかと思案しております。よろしければアドバイスを頂きたいと思います。
uk
ぬし
会議室デビュー日: 2003/05/20
投稿数: 1155
お住まい・勤務地: 東京都
投稿日時: 2004-11-18 15:23
1. スレッドを生成してその中でプロシージャ処理を実行。処理が終わったらセッションに登録。
2. 処理スレッド生成後に画面表示用サーブレットにリダイレクト。表示用サーブレットでは、
セッションを参照して「処理中」画面か「完了」画面にフォワード。「処理中」画面は、
画面表示用サーブレットのリロードを繰り返す。

で、どうですか?
gold_egg
会議室デビュー日: 2004/06/23
投稿数: 3
投稿日時: 2004-11-19 23:39
アドバイスありがとうございました。
やっと思い通りの形を作ることができました。
スレッドってカウンタのサンプルを作ったことがあるだけだったのでなんか初めて実用的な使い方ができたことに思わず感動してしまいました。スレッドって面白いですね。
ukさんありがとうございました。
1

スキルアップ/キャリアアップ(JOB@IT)