HttpServletRequest對象表明客戶端的請求,當客戶端經過HTTP協議訪問服務器時,HTTP請求頭中的全部信息都封裝在這個對象中,開發人員經過這個對象的方法,能夠得到客戶這些信息。html
request經常使用方法java
得到客戶機信息web
getRequestURL方法返回客戶端發出請求時的完整URL。編程
getRequestURI方法返回請求行中的資源名部分。設計模式
getQueryString 方法返回請求行中的參數部分。數組
getRemoteAddr方法返回發出請求的客戶機的IP地址服務器
getRemoteHost方法返回發出請求的客戶機的完整主機名網絡
getRemotePort方法返回客戶機所使用的網絡端口號jsp
getLocalAddr方法返回WEB服務器的IP地址。post
getLocalName方法返回WEB服務器的主機名
getMethod獲得客戶機請求方式
package com.hbsi.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Request1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getRequestURI());
System.out.println(request.getRequestURL());
System.out.println(request.getQueryString());
System.out.println(request.getMethod());
System.out.println(request.getRemoteAddr());
System.out.println(request.getRemoteHost());
System.out.println(request.getRemotePort());
System.out.println(request.getLocalAddr());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
得到客戶機請求頭
getHead(name)方法
getHeaders(String name)方法
getHeaderNames方法
得到客戶機請求參數(客戶端提交的數據)
getParameter(name):獲取指定名稱的參數值。這是最爲經常使用的方法之一。
getParameterValues(String name):獲取指定名稱參數的全部值數組。它適用於一個參數名對應多個值的狀況。如頁面表單中的複選框,多選列表提交的值。
getParameterNames():返回一個包含請求消息中的全部參數名的Enumeration對象。經過遍歷這個Enumeration對象,就能夠獲取請求消息中全部的參數名。
getParameterMap():返回一個保存了請求消息中的全部參數名和值的Map對象。Map對象的key是字符串類型的參數名,value是這個參數所對應的Object類型的值數組。
package com.hbsi.request;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Request2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//String value = request.getHeader("Accept");
//System.out.println(value);
Enumeration e = request.getHeaderNames();
while(e.hasMoreElements()){
String name = (String) e.nextElement();
String value = request.getHeader(name);
System.out.println(name+":"+value);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
request常見應用
各類表單輸入項數據的獲取
text、password、radio、checkbox、
file、select、textarea、 hidden、
image、button給js編程用
下面是request代碼
package com.hbsi.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Reqeust3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("name"));
}
private void test1form(HttpServletRequest request) {
String name = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String city = request.getParameter("city");
String [] likes = request.getParameterValues("likes");
String likeString="";
for(int i=0;likes!=null && i<likes.length;i++){
String like = likes[i];
likeString +=like;
}
String memo = request.getParameter("memo");
System.out.println(name+","+password+","+sex+","+city+","+likeString+","+memo);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
請求參數的中文亂碼問題
URL地址的編碼
package com.hbsi.request;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Request4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value = request.getParameter("name");
System.out.println(new String(value.getBytes("iso8859-1"),"UTF-8"));
}
private void test2(HttpServletRequest request)
throws UnsupportedEncodingException {
//get方式
String name = request.getParameter("username");
name = new String(name.getBytes("iso8859-1"),"UTF-8");
System.out.println(name);
}
private void test1(HttpServletRequest request)
throws UnsupportedEncodingException {
request.setCharacterEncoding("UTF-8");//只對post方式起做用
System.out.println(request.getParameter("username"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
防盜鏈
package com.hbsi.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Request5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String referer = request.getHeader("referer");
if(referer==null || !referer.startsWith("http://localhost")){
response.sendRedirect("/servletdemo/index.jsp");
return;
}
response.setContentType("text/html;charset=utf-8");
String data = "祕密!!!!";
response.getWriter().write(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
request對象實現請求轉發:請求轉發指一個web資源收到客戶端請求後,通知服務器去調用另一個web資源進行處理。
請求轉發
Servlet API中定義了一個RequestDispatcher接口,俗稱請求分派器。它定義了以下兩個方法:
public void forward(ServletRequest request, ServletResponse response)
public void include(ServletRequest request, ServletResponse response)
獲取RequestDispatcher實例的方式主要有兩種:
調用ServletContext接口提供的getRequestDispatcher(String url)方法。
調用ServletRequest接口提供的getRequestDispatcher(String url)方法。
RequestDispatcher
include方法:
RequestDispatcher.include方法用於將RequestDispatcher對象封裝的資源內容做爲當前響應內容的一部分包含進來,從而實現可編程的服務器端包含功能。
被包含的Servlet程序不能改變響應消息的狀態碼和響應頭,若是它裏面存在這樣的語句,這些語句的執行結果將被忽略。
package com.hbsi.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Request6 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/header.html").include(request, response);
response.getOutputStream().write("aaaaaaaaaaaa".getBytes());
request.getRequestDispatcher("/footer.html").include(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}