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:\SoftWare\apache-tomcat-8.0.44\webapps\amazingclass\images\arrow.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));
        }

 

posted @ 2017-11-03 13:07  newlangwen  阅读(171)  评论(0编辑  收藏  举报