一、获取请求参数
String getParameter(String name)若请求参数有多个值(例如CheckBox),该方法只能获取到第一个提交的值
String[] getParameterValues(String name)根据请求参数的名字,返回请求参数对应的字符串数组
Enumeration getParameterNames()返回参数名对应的enumeration对象,类似于ServletConfig或ServletContext的getInitParameterNames()方法
Map getParameterMap()返回请求参数的键值对
@Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("received request"); String user = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username: " + user + " / password : " + password); String[] lovers = request.getParameterValues("love"); for(String love : lovers) { System.out.println("-->" + love); } Enumeration<String> loverse = request.getParameterNames(); while(loverse.hasMoreElements()) { String name = loverse.nextElement(); String value = request.getParameter(name); System.out.println("name : " + name + " / value : " + value);//当为CheckBox等同一个name的参数有多个value时,只返回第一个 } Map<String , String[] > map = request.getParameterMap(); for(Map.Entry<String, String[]> entry : map.entrySet()) { System.out.println("***" + entry.getKey() + ":" + Arrays.asList(entry.getValue())); } }
二、获取请求URL
三、获取请求方式
四、获取servlet映射路径
HttpServletRequest是servletRequest的子接口,针对于HTTP请求定义,里边包含了大量获取http请求相关的方法
//先把ServletRequest的对象强制转换成HttpServletRequest HttpServletRequest httpServletRequest = (HttpServletRequest) request; String requestURI = httpServletRequest.getRequestURI(); System.out.println("requestURI :" + requestURI);//输出requestURI :/day_0206/TTT System.out.println("requestURL :" + httpServletRequest.getRequestURL());//输出requestURL :http://localhost:8989/day_0206/TTT String method = httpServletRequest.getMethod(); System.out.println(" method :" + method);//输出method :POST System.out.println("queryString :" + httpServletRequest.getQueryString());//输出queryString :null System.out.println("servletPath :" + httpServletRequest.getServletPath());//输出servletPath :/TTT
若method为get则queryString如下
method :GET
queryString :username=fv&password=ttt&love=deft&love=meiko
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。