您好,登錄后才能下訂單哦!
一、獲取請求參數
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。