localhost/user/user123とlocalhost/user?user=user123はどう違いlocalhost/?user=user123ますか?
サーブレットuser123のURLからパラメータを取得するには?localhost/user/user123
前もって感謝します
HttpServletRequest オブジェクトの getPathInfo() から解析できます。
サンプルコード
String urlPath = request.getPathInfo();
System.out.println("" + urlPath.substring(urlPath.lastIndexOf("/"), urlPath.length()- 1));
localhost/user/user123-このURLはパターンによって処理されます/user/user123localhost/user?user=user123-このURLは/user、userパラメータがuser123(GETリクエストの場合)に設定されたパターンによって処理されますlocalhost/?user=user123-このURLは/、userパラメーターがuser123(GETの場合)に設定されたパターンによって処理されます。user123純粋なサーブレットを使用してURLから取得する方法はわかりませんがlocalhost/user/user123、WebMVCフレームワークを使用すると非常に簡単です。春の例:
@Controller
@RequestMapping("/user")
public class Controller {
@RequestMapping(value = "/{user}")
public String getUser((@PathVariable String user) {
//here variable "user" is available and set to "user123" in your case
}
}
通常、次のようなパラメータを渡します
/localhost/Servlet?parameter1=one
またはJSPの場合
/localhost/mypage.jsp?parameter1=one
サーブレットでは、リクエストオブジェクトを使用してパラメータにアクセスできます。だから一般的にこのように:
String parameter1 = request.getParameter("parameter1");
HttpServletRequestのgetParameterの詳細は次のとおりです
お役に立てれば。
localhost/user/user123 は、リソースを識別する RESTful な方法のように見えます。
他の2つはそうではないと思います。
これらはすべてServlet APIからアクセスできます。HttpServletRequestを確認すると、そこからすべての情報にアクセスできます。
実際の値は、Web アプリケーションのデプロイ方法と異なる場合がありますが、通常は
localhostコンテキストパスです?はクエリ文字列です-使用する場合は、それを解析する必要があります