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());
}
Tips for writing URLs:
Write '/' first. If it is used by the server, there is no need to write the web application name; if it is used by the browser, just add the web application name;
RequestDispatcher:
//Request forwarding, use the Request domain object to bring data to the forwarding resource;
The difference from redirection: the client only issues one request, the server calls multiple resources, and the client browser address bar does not change;
How to get it:
ServletContext.getRequestDispatcher(String);
ServletContext.getNamedDispatcher(String);
ServletRequest.getRequestDispatcher(String);
ex:request.getRequestDispatcher("/index.jsp").forward(request, response);
Encoder: Garbled code processing
request.setCharacterEncoding("UTF-8"); //Only valid for Post mode
// get method handles garbled characters
String value = request.getParameter("username");
String out = new String(value.getBytes("iso8859-1"), "UTF-8");
System.out.println(out);
//When obtaining request data, it is generally checked first before use.
Get Header:
// String value = request.getHeader("headername");
// Enumeration headers = request.getHeaders("");
// Enumeration headernames = request.getHeaderNames();