spring mvc controller中获取request head内容:
@RequestMapping("/{mlid}/{ptn}/{name}")
public String print(@PathVariable Integer mlid, @PathVariable String ptn, @PathVariable String name, HttpSession session, Model model,
@RequestHeader String referer,
@RequestHeader ("Referer") String referer2,
@RequestHeader ("host") String hostName,
@RequestHeader ("Accept") String acceptType,
@RequestHeader ("Accept-Language") String acceptLang,
@RequestHeader ("Accept-Encoding") String acceptEnc,
@RequestHeader ("Cookie") String cookie,
@RequestHeader ("User-Agent") String userAgent,
HttpServletRequest request) {
System.out.println(hostName);
// if (referer == null) {
// return "common/blank";
// }
String header = request.getHeader("Referer");
String header2 = request.getHeader("referer");
System.out.println(header);
System.out.println(header2);
@RequestHeader String referer,就是@RequestHeader ("Referer") String referer2,
变量referer的名字不能随便改,就是找的request header的referer;
如果请求中没有referer,前台会报400异常,需要捕获;后台会有
org.springframework.web.bind.ServletRequestBindingException: Missing request header 'referer3' for method parameter of type String
可以使用request.getHeader("Referer");或者request.getHeader("referer");
这样可以进行null判断。 在桌面建一个文件,用<a>的方式进行文件的打开是没有referer的;同样的文件,放到tomcat服务器里面,就有referer了;