「Java」HttpServletRequestクラスでサーバ名、クエリパラメータとコンテクストパスなどを取得するサンプル

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,
IOException {
//サーバ名
String serName = req.getServerName();
//サーバポート
int serPort = req.getServerPort();
//サーバレットパス
String serPath = req.getServletPath();
//コンテキストパス
String conPath = req.getContextPath();
//リクエストURI
String reqUri = req.getRequestURI();
String reqUrl = req.getRequestURL().toString();
String pathInfo = req.getPathInfo();
//スキーマ
String schema = req.getScheme();
//クエリパラメータ
String qryString = request.getQueryString();
//処理コード
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //サーバ名 String serName = req.getServerName(); //サーバポート int serPort = req.getServerPort(); //サーバレットパス String serPath = req.getServletPath(); //コンテキストパス String conPath = req.getContextPath(); //リクエストURI String reqUri = req.getRequestURI(); String reqUrl = req.getRequestURL().toString(); String pathInfo = req.getPathInfo(); //スキーマ String schema = req.getScheme(); //クエリパラメータ String qryString = request.getQueryString(); //処理コード }
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException,
            IOException {
      //サーバ名
      String serName = req.getServerName();
      //サーバポート
      int serPort = req.getServerPort();
      //サーバレットパス
      String serPath = req.getServletPath();
      //コンテキストパス
      String conPath = req.getContextPath();
      //リクエストURI
      String reqUri = req.getRequestURI();
      String reqUrl = req.getRequestURL().toString();
      String pathInfo = req.getPathInfo();
      //スキーマ
      String schema = req.getScheme();
      //クエリパラメータ
      String qryString = request.getQueryString();
      //処理コード

}

 

Java

Posted by arkgame