您好,登錄后才能下訂單哦!
一、獲取請求參數(shù)
String getParameter(String name)若請求參數(shù)有多個值(例如CheckBox),該方法只能獲取到第一個提交的值
String[] getParameterValues(String name)根據(jù)請求參數(shù)的名字,返回請求參數(shù)對應(yīng)的字符串數(shù)組
Enumeration getParameterNames()返回參數(shù)名對應(yīng)的enumeration對象,類似于ServletConfig或ServletContext的getInitParameterNames()方法
Map getParameterMap()返回請求參數(shù)的鍵值對
@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的參數(shù)有多個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請求相關(guān)的方法
//先把ServletRequest的對象強制轉(zhuǎn)換成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
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。