やり方としては画面表示時に強制的にsubmitButtonを押させています。
基本さえ抑えれば言語はなんでもいけると思います。
サンプルソース
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | @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()); } } |
小・中規模程度なら有効だと思うので許してくだしあ。
簡単に解説するとパラメタとして渡したいものを隠しフィールドとして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
送信と言いつつサーバからリクエストを受け取っているので受信もしている。サーバ側からしかパラメタを見ることができない。
パラメタを知られたくない時に使われる。
いじょ。