serlvet HttpServletRequest

1.http://localhost/az/servlet/TestResponse

out.print("getServletPath:"+request.getServletPath());  // /servlet/TestResponse
out.print(",getContextPath:"+request.getContextPath());// /az

out.print(",getRequestURI:"+request.getRequestURI());// /az/servlet/TestResponse

out.print(",getRequestURL:"+request.getRequestURL());// http://localhost/az/servlet/TestResponse

this.getServletContext().getRealPath("/images/arrow.png") //E:SoftWareapache-tomcat-8.0.44webappsamazingclassimagesarrow.png

out.print(",getServerPort:"+request.getServerPort);// 80

out.print(",getRemotePort:"+request.getRemotePort);// 50545

 2.参数处理

  

//request对象封装的参数是以Map的形式存储的
        Map<String, String[]> paramMap = request.getParameterMap();
        for(Map.Entry<String, String[]> entry :paramMap.entrySet()){
            String paramName = entry.getKey();
            String paramValue = "";
            String[] paramValueArr = entry.getValue();
            for (int i = 0; paramValueArr!=null && i < paramValueArr.length; i++) {
                if (i == paramValueArr.length-1) {
                    paramValue+=paramValueArr[i];
                }else {
                    paramValue+=paramValueArr[i]+",";
                }
            }
            System.out.println(MessageFormat.format("{0}={1}", paramName,paramValue));
        }
原文地址:https://www.cnblogs.com/newlangwen/p/7777708.html