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