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();