Referer:
public void referer(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String referer = request.getHeader("referer");
if (referer == null || !referer.startsWith("http://localhost")) {
response.sendRedirect("/webTwo/index.jsp");
return;
}
String data = "welcome";
response.getOutputStream().write(data.getBytes());
}
編寫URL技巧:
先寫'/',給服務器用的就不用寫web應用程序名稱;給浏覽器用的就加上web應用程序名稱;
RequestDispatcher:
//請求轉發,使用Request域對象把數據帶給轉發資源;
與重定向的區別:客戶端只發出一次請求,服務器端調用多個資源,客戶端浏覽器地址欄沒改變;
獲取方式:
ServletContext.getRequestDispatcher(String);
ServletContext.getNamedDispatcher(String);
ServletRequest.getRequestDispatcher(String);
ex:request.getRequestDispatcher("/index.jsp").forward(request, response);
Encoder:亂碼處理
request.setCharacterEncoding("UTF-8");//只對Post方式有效
// get方式處理亂碼
String value = request.getParameter("username");
String out = new String(value.getBytes("iso8859-1"), "UTF-8");
System.out.println(out);
// 獲取請求數據時一般都先檢查再使用
獲取Header:
// String value = request.getHeader("headername");
// Enumeration headers = request.getHeaders("");
// Enumeration headernames = request.getHeaderNames();