First, pre-theoretical preparation
1) Purpose:
Dynamically generate CSS statements in the JSP and output them to the browser for parsing and rendering.
2) The browser parses the file according to:
After the page loads, the browser initiates individual requests to download various resources.
such as downloading CSS files, and then according to the CSS parsing rules to parse the document. If the downloaded file Content-type does not match, the browser will automatically block out.
Second, let JSP change the premise of CSS
Knowing the browser's parsing rules, JSP needs to do is to disguise their content-type as "text/css".
Third, directly on the code:
Index.html
<! DOCTYPE html>
css.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "session=" false "%><% //Camouflage Response HTTP header Response.setheader (" Content-type "," text/css "); Output CSS style out.clear (); Out.print ("@charset \" utf-8\ "; \ n"); Out.print ("p{color:red;} \ n ");%>
Iv. Results
Successfully disguised JSP as a CSS file, browser parsing style success!
V. Expansion
According to this camouflage rule, the JSP can be disguised as arbitrary file format, as long as the browser can parse.
For example, disguised as JS, the Content-type is set to "Application/x-javascript"