やり方としては画面表示時に強制的にsubmitButtonを押させています。
基本さえ抑えれば言語はなんでもいけると思います。
サンプルソース
@WebServlet("/Test")
public class Test extends HttpServlet {
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// post先
String postUrl = "postURL";
// html構築
StringBuilder html = new StringBuilder();
html.append("<html>");
html.append(" <head>");
html.append(" </head>");
html.append(" <body onLoad=\"document.getElementById('postButton').click();\">");
html.append(" <form action=\"" + postUrl + "\" method=\"post\" >");
// postするパラメータ
html.append(" <input type=\"hidden\" name=\"param1\" value=\"" + 1 + "\">");
html.append(" <input type=\"hidden\" name=\"param2\" value=\"" + "param2" + "\">");
html.append(" <input id=\"postButton\" type=\"submit\" style=\"visibility: hidden;\" />");
html.append(" </form>");
html.append(" </body>");
html.append("</html>");
// ブラウザへ書き出し
response.getWriter().write(html.toString());
}
}
JavaのなかにHtmlを埋め込んで更にJavaScriptを埋め込む暴挙。小・中規模程度なら有効だと思うので許してくだしあ。
簡単に解説するとパラメタとして渡したいものを隠しフィールドとしてformに埋め込んで、
透明なsubmitボタンをonload時に強制的に押してpostしている感じです。
これで画面表示時にpostUrlへパラメタparam1:2とparam2:"param2"を受け渡して遷移できます。(文字コードの設定は気をつけたほうがいいかも)
以下補足
getとpostについて
get
受信と言いつつサーバへリクエストを送っているので送信もしている。一度に送るデータ量が制限されている。
url末尾に?=...の形式でパラメタが渡される。(故にクライアントサイドでもパラメタを知ることが出来る)
google検索がgetだと考えるとわかりやすいかも。
https://www.google.co.jp/search?q=http+get+post
get
送信と言いつつサーバからリクエストを受け取っているので受信もしている。サーバ側からしかパラメタを見ることができない。
パラメタを知られたくない時に使われる。
いじょ。