JavaWeb——request获取相关路径

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        http://localhost:8080/DemoName/AServlet?username=xxx&password=yyy
        request.getScheme();//获取协议,http
        request.getServerName();//获取服务器名,localhost
        request.getServerPort();//获取服务器端口,8080
        request.getContextPath();//获取项目名,/DemoName
        request.getServletPath();//获取Servlet路径,/AServlet
        request.getQueryString();//获取参数部分,即问号后面的部分,username=xxx&password=yyy
        request.getRequestURI();//获取请求URI,即项目名+Servlet路径,/DemoName/AServlet
        request.getRequestURL();//获取请求URL,等于不包括参数的整个路径,http://localhost:8080/DemoName/AServlet
    }

 

你可能感兴趣的:(Java,JavaWeb)