ユーザ用ツール

サイト用ツール


servlet:uriurlservletpath

05. getRequestURI と getRequestURL と getServletPath

サーバに通知される URL の情報を取得する似たようなメソッド、

getRequestURI と getRequestURL と getServletPath

それぞれの動作を確認してみた。

@WebServlet("/test.html")
public class FirstServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        res.setContentType("text/html;charset=utf-8");
 
        PrintWriter out = res.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Firstテスト用</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Firstテスト用</h1>");
 
        String reqURI = req.getRequestURI();
        StringBuffer reqURL = req.getRequestURL();
        String servletPath = req.getServletPath();
        out.println("reqURI : " + reqURI + "<br>");
        out.println("reqURL : " + reqURL + "<br>");
        out.println("servletPath : " + servletPath + "<br>");
        out.println("<br>");
 
        out.println("</body>");
        out.println("</html>");
    }
}

http://localhost:8080/FirstServlet/test.html?cmd=aaa&param=bbb

でアクセスして、結果はこうなりました。

reqURI : /FirstServlet/test.html
reqURL : http://localhost:8080/FirstServlet/test.html
servletPath : /test.html

servlet/uriurlservletpath.txt · 最終更新: 2022/10/15 06:55 by hohog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki